Question

I'm trying to send a basic verification email using Laravel and an account on mailgun.com. I've followed the instructions here and am getting errors. here's my code:

Route::post('/register', array('before'=>'reverse-auth', function(){
    $data=Input::all();

    if ($data['password'] != $data['confirm-password']){
        return Redirect::to('/register');
    }

    $user = new User;

    $user->email=$data['email'];
    $user->password=Hash::make($data['password']);
    $user->first=ucfirst($data['first']);
    $user->last=ucfirst($data['last']);
    $user->address=$data['street'].", ".$data['city'].", ".$data['state']." ".$data['zip'];
    $user->phone=$data['phone'];
    $user->confirmation=Str::random(32);
    $user->confirmed=0;

    $user->save();

    Mail::send('emails.verify', $user->toArray(), function($message){
        global $user;

        $message->to($user['email'], $user['first']." ".$user['last']);
        $message->from('noreply@localhost', 'Do Not Reply');
    });
}));

My error looks like this:

Client error response [url] https://api.mailgun.net/v2/sandboxa666975e4b514342a58e4d7d3e6c2366.mailgun.org/messages.mime [status code] 400 [reason phrase] BAD REQUEST

I have no idea what I'm doing wrong.

As an aside, why is $user not an object within the function? Shouldn't the global keyword force it to be the same as the $user object outside the function?

Was it helpful?

Solution

You're confusing an object with an array inside of your anonymous function.
Also, to pass a variable to an anonymous function, you can use the use construct.

Mail::send('emails.verify', $user->toArray(), function($message) use ($user){
    //  Now you can use $user anywhere in the function without using global
    //  global $user;

    //  $user is an object not an array
    //  $message->to($user['email'], $user['first']." ".$user['last']);
    $message->to($user -> email, $user -> first." ".$user -> last) ->('You also want a subject here');
    $message->from('noreply@localhost', 'Do Not Reply');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top