我正在开发一个包含联系我们页面的网站,访客用户可以使用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';
}
}