dtwxt88240 2014-07-28 18:52
浏览 53
已采纳

在Laravel中发送电子邮件

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?

  • 写回答

1条回答 默认 最新

  • dongzhan2461 2014-07-28 20:11
    关注

    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');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库