Laravel邮件错误`StreamBuffer.php第269行中的Swift_TransportException:无法与主机smtp.gmail.com [#0]建立连接

时间:2016-11-09 12:27:28

标签: gmail laravel-5.1

我正在开发一个包含联系我们页面的网站,访客用户可以使用Gmail向我发送电子邮件。但是,只要我尝试使用contact-us表单发送邮件,我就会收到此错误Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]

这是我的mail.php

 return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.gmail.com'),
    'port' => 587,
    'from' => ['address' => '', 'name' => ''],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env(MAIL_PASSWORD),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => env('MAIL_PRETEND', false),
  ];

这是我的.env

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

这是UserController.php

 public function sendMail()
{
    $input = Request::all();

    $data = [
      'name' => $input['name'],
      'email' => $input['email'],
      'subject' => $input['subject'],
      'message' => $input['message']
    ];

    $rules = User::$mailRules;

    $validator = Validator::make($input, $rules);
    if ($validator->fails()) {
      return redirect()->back()->withErrors($validator)->withInput($input);
    } else {
      Mail::send('emails.send', $data, function($message) use ($data) {
        $message->from(Input::get('email'))
                ->to('afecjaped@gmail.com')
                ->subject(Input::get('subject'));
      });
    return 'Email Successfully sent';
    }
}

1 个答案:

答案 0 :(得分:0)

您必须将您的Gmail帐户配置为使用accounts.google.com中的smtp。 检查enter link description here以获取指南