Laravel,无法发送电子邮件,使用3个可能的身份验证器无法通过用户名在SMTP服务器上进行身份验证

时间:2019-07-04 10:02:49

标签: laravel email smtp

我正在尝试使用Google电子邮件发送电子邮件,并尝试了许多解决方案,但是没有用,我尝试了php artisan config:cache并打开“允许安全性较低的应用程序”继续并解锁了您的帐户,以便通过其他媒体进行访问/ sites,并尝试启用2种验证方式,但存在相同的错误

.env:

 MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=****@gmail.com
MAIL_PASSWORD=*****
MAIL_ENCRYPTION=tls

mail.php

 'driver' => env('MAIL_DRIVER', 'smtp'),
 'host' => env('MAIL_HOST', 'smtp.gmail.com'),
  'port' => env('MAIL_PORT', 587),
 'from' => ['address' => null, 'name' => null],
 'encryption' => env('MAIL_ENCRYPTION', 'tls'),
 'username' => env('****@gmail.com'),
 'password' => env('****'),
  'sendmail' => '/usr/sbin/sendmail -bs',

控制器

 public function store(Request $request)
  {  

    $data = request()->except(['_token']);

     $check =OfferBooking::create($data);
    $arr = array('msg' => 'Something goes to wrong. Please try again lator', 'status' => false);
    if($check){ 
    $arr = array('msg' => 'Thank you for your booking, we will contact you soon', 'status' => true);
    }
   Mail::send('offer.email',
     [
    'name' => $request->get('name'),
    'email' => $request->get('email'),
    'details' => $request->get('details')
    ], function($message)
     {
    $message->from('ashqahman@gmail.com');
    $message->to('ashqahman@gmail.com')->subject('New Reservation');
});
$response = [
    'status' => 'success',
    'msg' => 'Mail sent successfully',
];

    return Response()->json($arr);

}

3 个答案:

答案 0 :(得分:0)

在您的mail.php更改中

'username' => env('****@gmail.com'),
'password' => env('****'),

'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

请勿直接修改mail.php,因为它将从您的.env文件中获取数据

答案 1 :(得分:0)

在.env文件中进行更改后,请不要忘记运行php artisan config:cache。

答案 2 :(得分:0)

请重新启动服务器,它将正常工作