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 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿