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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集