Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Probably a noob question but here we go.

I'm using laravel and the messages bundle to send emails to verify users. But how do I get access to my $new_user object from within my send method?

    // Get inputdata and insert into table users
    $new_user = User::create(array(
        'username' => Input::get('username'), 
        'email' => Input::get('email'), 
        'password' => Hash::make(Input::get('password')),
        'hash' => hash('sha512', uniqid()) 
    ));

    Message::send(function($message)
    {
        $message->to('[email protected]');
        $message->from($new_user->email, $new_user->username);

        $message->subject('new account');
        $message->body('

            Bedankt voor het registreren!

            Uw account is aangemaakt, u kan inloggen met de volgende gegevens nadat u u account heeft geactiveerd met de url hier beneden.

            ------------------------
            Username: '.$new_user->username.'
            Password: '.$new_user->password.'
            ------------------------

            Klik op deze link om uw account te activeren.

            http://blablabla.dev/verify?email='.$new_user->email.'&hash='.$new_user->hash.' 

        ');
    });

Doing this obviously doesn't work since I don't have access to the object.

Hopefully I've phrased this right.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can use the use keyword here

$new_user = User::create(array(
    'username' => Input::get('username'), 
    'email' => Input::get('email'), 
    'password' => Hash::make(Input::get('password')),
    'hash' => hash('sha512', uniqid()) 
));

Message::send(function($message) use ($new_user)
{
    $message->to('[email protected]');
    $message->from($new_user->email, $new_user->username);
    // ...
}
share|improve this answer

$message instanceof Message? and what params should be passed to the Message::send()? May be you better do this:

$message = new Message;
$message->to('[email protected]');
$message->from($new_user->email, $new_user->username);
$message->subject('new account');
$message->body('...'.$new_user->email.'...');

If you have public (not static) method send() in your Message class, and it looks like this:

public function send(){
     @mail($this->to, $this->subject, $this->body, $this->header, $this->params);
}

Then do

$message->send();

Else, if you have only static send() method in your Message class, and it defined as

static function send(Message $message){...}

call

Message::send($message);

Also I recommend you to validate user input before adding it to database.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.