簡體   English   中英

在Laravel中發送電子郵件

[英]Sending emails in Laravel

我正在嘗試使用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');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM