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 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的