Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中发送电子邮件_Php_Email_Laravel_Mailgun - Fatal编程技术网

Php 在Laravel中发送电子邮件

Php 在Laravel中发送电子邮件,php,email,laravel,mailgun,Php,Email,Laravel,Mailgun,我正在尝试使用Laravel和mailgun.com上的帐户发送一封基本的验证电子邮件。我已经按照说明进行了操作,并且出现了错误。这是我的密码: Route::post('/register', array('before'=>'reverse-auth', function(){ $data=Input::all(); if ($data['password'] != $data['confirm-password']){ return Redirect

我正在尝试使用Laravel和mailgun.com上的帐户发送一封基本的验证电子邮件。我已经按照说明进行了操作,并且出现了错误。这是我的密码:

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');
    });
}));
我的错误如下所示:

Client error response [url] https://api.mailgun.net/v2/sandboxa666975e4b514342a58e4d7d3e6c2366.mailgun.org/messages.mime [status code] 400 [reason phrase] BAD REQUEST
我不知道我做错了什么


另外,为什么
$user
不是函数中的对象?
global
关键字不应该强制它与函数外部的
$user
对象相同吗?

您将对象与匿名函数内部的数组混淆了。
此外,要将变量传递给匿名函数,可以使用
use
构造

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');
});

@是的,我的错