如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)
Laravel 提供了非常简单的邮件发送 API,但是文档却不是太清晰,再加上它采用传递闭包(回调函数)的方式调用,导致邮件发送的使用门槛偏高。
Laravel 4 和 Laravel 5 的邮件发送使用方式完全一致。Laravel 5 的邮件发送中文文档在:http://laravel-china.org/docs/5.0/mail
本文中,我将以 163 邮箱为例,展示如何用 Laravel 内置的邮件发送类来发送邮件。
配置
修改邮件发送配置。4.2 在 app/config/mail.php,5 在 config/mail.php,修改以下配置:
'host' => 'smtp.163.com', 'port' => 25, 'from' => array('address' => '***@163.com', 'name' => 'TestMail'), 'username' => '***@163.com', // 注意,这里必须和上一行配置里面的邮件地址一致 'password' => '****',
发送
在控制器或者模型里,调用以下代码:
$data = ['email'=>$email, 'name'=>$name, 'uid'=>$uid, 'activationcode'=>$code]; Mail::send('activemail', $data, function($message) use($data) { $message->to($data['email'], $data['name'])->subject('欢迎注册我们的网站,请激活您的账号!'); });
邮件视图为 views/activemail.blade.php:
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> </head> <body> <a href="{{ URL('active?uid='.$uid.'&activationcode='.$activationcode) }}" target="_blank">点击激活你的账号</a> </body> </html>
搞定!
← 给 UILabel 中的文字增加 line-through / Strikethrough (删除线)样式
Laravel 5 系列入门教程(四)【最适合中国人的 Laravel 教程】【完结】 →
评论:
猪猪侠
2019-08-23 16:41
Swift_TransportException: Connection could not be established with host smtp.qq.com [No route to host #113] in file /www/wwwroot/www.syswz.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 269
报这个错 不知道 怎么解决
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=XXXXXXXX@qq.com
MAIL_PASSWORD=XXXXXXXXX
MAIL_ENCRYPTION=SSL
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'xxxxxxxx@qq.com'),
'name' => env('MAIL_FROM_NAME', '项目'),
],
2019-08-23 16:41

报这个错 不知道 怎么解决
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=XXXXXXXX@qq.com
MAIL_PASSWORD=XXXXXXXXX
MAIL_ENCRYPTION=SSL
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'xxxxxxxx@qq.com'),
'name' => env('MAIL_FROM_NAME', '项目'),
],
Sunshaolei
2019-08-09 23:14
2019-08-09 23:14
邮件发送不出去是什么问题导致的,邮箱接收不到邮件
我的配置是这样配置的:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_FROM_ADDRESS=ssl15081228231@163.com
MAIL_FROM_NAME=CoreBlog
MAIL_PORT=465
MAIL_USERNAME=ssl15081228231@163.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl
我的配置是这样配置的:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_FROM_ADDRESS=ssl15081228231@163.com
MAIL_FROM_NAME=CoreBlog
MAIL_PORT=465
MAIL_USERNAME=ssl15081228231@163.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl
imooto_cc
2017-12-11 14:28
2017-12-11 14:28
Swift_TransportException thrown with message "Expected response code 250 but got code "553", with message "553 authentication is required,163 smtp14,EsCowAA3T3YLJS5ajXK1Dg--.35823S2 1512973579
这个是什么原因呢?换了qq 163 都是提示这个错误

这个是什么原因呢?换了qq 163 都是提示这个错误


winnie
2017-11-01 17:35
2017-11-01 17:35
===============mail.php文件
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.163.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'xxxh@163.com', 'name' => 'Test'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', null),
'password' => env('MAIL_PASSWORD', null),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
=============
.env文件配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=xxxh@163.com
MAIL_PASSWORD=xxxh
这样配置后发邮件出现 Expected response code 250 but got code "553", with message "553 Local user only,163 smtp14,EsCowAB3_akEk_lZvAjPBg--.25694S2 1509528324 " 错误,如果是按照博主的来配置port=25会出现“Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required”
163的邮箱已经开启了smtp服务,而且使用的是客户端授权码作为MAIL_PASSWORD 本机可以ping通smtp.163.com
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.163.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'xxxh@163.com', 'name' => 'Test'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', null),
'password' => env('MAIL_PASSWORD', null),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
=============
.env文件配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=xxxh@163.com
MAIL_PASSWORD=xxxh
这样配置后发邮件出现 Expected response code 250 but got code "553", with message "553 Local user only,163 smtp14,EsCowAB3_akEk_lZvAjPBg--.25694S2 1509528324 " 错误,如果是按照博主的来配置port=25会出现“Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required”
163的邮箱已经开启了smtp服务,而且使用的是客户端授权码作为MAIL_PASSWORD 本机可以ping通smtp.163.com
悦宝宝
2017-03-21 20:08
2017-03-21 20:08
Connection could not be established with host stmp.163.com [php_network_getaddresses: getaddrinfo failed: ��֪������������ #0]
求解这个是因为什么阿 就是提示连不上163主机。 不知道怎么解决~

求解这个是因为什么阿 就是提示连不上163主机。 不知道怎么解决~
icymoon
2017-01-04 16:26
2017-01-04 16:26
Expected response code 220 but got code \"\", with message \"\"",
。。不知道怎么解决。。配置完全一样, 163的客户端授权码也开了
。。不知道怎么解决。。配置完全一样, 163的客户端授权码也开了
自恋狂
2017-01-04 11:10
2017-01-04 11:10
用的5.1,学习的时间不久,现在发送普通邮件是正常的,但是发送有view视图的邮件时就一直超时了,一直搞不明白!
Whoops, looks like something went wrong.
1/1
Swift_TransportException in StreamBuffer.php line 269:
Connection could not be established with host smtp.163.com [Connection timed out #110]
in StreamBuffer.php line 269
at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
at Swift_Transport_StreamBuffer->initialize(array('protocol' => '', 'host' => 'smtp.163.com', 'port' => '25', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113
at Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 374
at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 196
at Mailer->send('notifications::email', array('level' => 'info', 'subject' => null, 'greeting' => null, 'introLines' => array('You are receiving this email because we received a password reset request for your account.'), 'outroLines' => array('If you did not request a password reset, no further action is required.'), 'actionText' => 'Reset Password', 'actionUrl' => 'http://test.z-love.com/password/reset/dafb5d7b21597b7ede8cedab5df7c6ef196c2ac1d298235052308773b8020f0c', 'message' => object(Message)), object(Closure)) in MailChannel.php line 81
Whoops, looks like something went wrong.
1/1
Swift_TransportException in StreamBuffer.php line 269:
Connection could not be established with host smtp.163.com [Connection timed out #110]
in StreamBuffer.php line 269
at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
at Swift_Transport_StreamBuffer->initialize(array('protocol' => '', 'host' => 'smtp.163.com', 'port' => '25', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113
at Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 374
at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 196
at Mailer->send('notifications::email', array('level' => 'info', 'subject' => null, 'greeting' => null, 'introLines' => array('You are receiving this email because we received a password reset request for your account.'), 'outroLines' => array('If you did not request a password reset, no further action is required.'), 'actionText' => 'Reset Password', 'actionUrl' => 'http://test.z-love.com/password/reset/dafb5d7b21597b7ede8cedab5df7c6ef196c2ac1d298235052308773b8020f0c', 'message' => object(Message)), object(Closure)) in MailChannel.php line 81
kaka
2016-12-21 14:37
2016-12-21 14:37
Connection could not be established with host smtp.qq.com [ #0] 这是什么意思呀 ,,,有没有解决的办法
白
2016-12-02 20:10
2016-12-02 20:10
Expected response code 250 but got code "530", with message "530 5.7.1 Authe 博主您好!你能帮我看看这是怎么回事么?我很急!谢谢!!
function sendMail()
{
// $name = '学院君';
// $flag = Mail::send('Login.active',['name'=>$name],function($message){
// $to = '1298528156@qq.com';
// $message ->to($to)->subject('测试邮件');
// });
// if($flag){
// echo '发送邮件成功,请查收!';
// }else{
// echo '发送邮件失败,请重试!';
// }
Mail::raw('水水水水',function ($message){
$message->subject('提醒邮件');
$message->to('1298528156@qq.com');
});
}
上面是控制器代码
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.163.com'),
'port' => env('MAIL_PORT', 25),
'encryption' => env('MAIL_ENCRYPTION', null),
'username' => env('13521107990@163.com'),
'password' => env('bai123'),
'from' => ['address' => '13521107990@163.com', 'name' => '白'],
'sendmail' => '/usr/sbin/sendmail -bs',
这是mail.php 各种方法都试了 都是这个错
function sendMail()
{
// $name = '学院君';
// $flag = Mail::send('Login.active',['name'=>$name],function($message){
// $to = '1298528156@qq.com';
// $message ->to($to)->subject('测试邮件');
// });
// if($flag){
// echo '发送邮件成功,请查收!';
// }else{
// echo '发送邮件失败,请重试!';
// }
Mail::raw('水水水水',function ($message){
$message->subject('提醒邮件');
$message->to('1298528156@qq.com');
});
}
上面是控制器代码
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.163.com'),
'port' => env('MAIL_PORT', 25),
'encryption' => env('MAIL_ENCRYPTION', null),
'username' => env('13521107990@163.com'),
'password' => env('bai123'),
'from' => ['address' => '13521107990@163.com', 'name' => '白'],
'sendmail' => '/usr/sbin/sendmail -bs',
这是mail.php 各种方法都试了 都是这个错
JohnLui
2016-12-02 20:19
2016-12-02 20:19
@白:'driver' => env('MAIL_DRIVER', 'smtp'), 这个函数的意思是先从 .env 里面读 MAIL_DRIVER 的值,如果没有,就赋值为 smtp,所以,你需要检查一下你的 .env。
you件
2016-09-08 09:12
Failed to authenticate(鉴定) on SMTP server with username "13141224138@163.com" using 2 possible authenticators(认证者) 怎么弄。。
2016-09-08 09:12

333
2016-08-24 10:41
2016-08-24 10:41
发送邮件提示: ErrorException in SimpleMimeEntity.php line 83: getmypid() has been disabled for security reasons
因为是在服务器上,空间买的,无法更改php.ini设置,请问这个函数被屏蔽了,怎么发邮件呢。
因为是在服务器上,空间买的,无法更改php.ini设置,请问这个函数被屏蔽了,怎么发邮件呢。
CanoeChow
2016-08-02 09:52
2016-08-02 09:52
您好博主! 我出现了问题.
我采用的框架版本是 laravel5.2
以下是我config/mail.php 的配置
<?php
return [
'driver' => 'smtp',
'host' => 'smtp.qq.com',
'port' => 465,
'from' => ['address' => '285549199@qq.com', 'name' => 'Welcome'],
'encryption' => null,
'username' => '285549199@qq.com',
'password' => 'xxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
];
app/Http/routes.php采用如下代码
Route::get('mail', function () {
$data = ['name' => 'CanoeChow'];
Mail::send('emails.hello', $data, function ($message) {
$message->to('a17855827493@icloud.com')->subject('Welcome!');
$message->attach(public_path().'img/pic.jpg');
});
return 'OK!';
});
结果:
Swift_TransportException in AbstractSmtpTransport.php line 404:
Connection to smtp.qq.com:465 Timed Out
求您解答~谢谢~
我采用的框架版本是 laravel5.2
以下是我config/mail.php 的配置
<?php
return [
'driver' => 'smtp',
'host' => 'smtp.qq.com',
'port' => 465,
'from' => ['address' => '285549199@qq.com', 'name' => 'Welcome'],
'encryption' => null,
'username' => '285549199@qq.com',
'password' => 'xxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
];
app/Http/routes.php采用如下代码
Route::get('mail', function () {
$data = ['name' => 'CanoeChow'];
Mail::send('emails.hello', $data, function ($message) {
$message->to('a17855827493@icloud.com')->subject('Welcome!');
$message->attach(public_path().'img/pic.jpg');
});
return 'OK!';
});
结果:
Swift_TransportException in AbstractSmtpTransport.php line 404:
Connection to smtp.qq.com:465 Timed Out
求您解答~谢谢~
wuzhyong
2016-06-16 10:38
2016-06-16 10:38
很简单的问题,账号和密码问题,
( ! ) Fatal error: Uncaught exception 'Swift_TransportException' with message ' in C:\wamp\www\news\lib\mail\classes\Swift\Transport\AbstractSmtpTransport.php on line 383
( ! ) Swift_TransportException: Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user " in C:\wamp\www\news\lib\mail\classes\Swift\Transport\AbstractSmtpTransport.php on line 383
这个错误导致原因很简单,发件人不对
setFrom账号要与setUsername统一
( ! ) Fatal error: Uncaught exception 'Swift_TransportException' with message ' in C:\wamp\www\news\lib\mail\classes\Swift\Transport\AbstractSmtpTransport.php on line 383
( ! ) Swift_TransportException: Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user " in C:\wamp\www\news\lib\mail\classes\Swift\Transport\AbstractSmtpTransport.php on line 383
这个错误导致原因很简单,发件人不对
setFrom账号要与setUsername统一
小新
2016-01-29 11:41
2016-01-29 11:41
老师,您好:
config/mail.php 配置了'from' => ['address' => '***@163.com', 'name' => 'TestMail'],
.env 配置了
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=*******@163.com
MAIL_PASSWORD=*******
发送代码:
$data = ['email'=>'******@qq.com', 'name'=>'小新', 'uid'=>'100', 'activationcode'=>'123456'];
Mail::send('activemail', $data, function($message) use($data)
{
$message->to($data['email'], $data['name'])->subject('欢迎注册我们的网站,请激活您的账号!');
});
为什么发出去的邮件的发件人显示的是 “手机邮箱”
config/mail.php 配置了'from' => ['address' => '***@163.com', 'name' => 'TestMail'],
.env 配置了
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=*******@163.com
MAIL_PASSWORD=*******
发送代码:
$data = ['email'=>'******@qq.com', 'name'=>'小新', 'uid'=>'100', 'activationcode'=>'123456'];
Mail::send('activemail', $data, function($message) use($data)
{
$message->to($data['email'], $data['name'])->subject('欢迎注册我们的网站,请激活您的账号!');
});
为什么发出去的邮件的发件人显示的是 “手机邮箱”
curry
2016-01-13 17:12
2016-01-13 17:12
Address in mailbox given [823232600@qq.com ] does not comply with RFC 2822, 3.6.2. 这个怎么解决

发不了邮件
2015-10-30 17:57
2015-10-30 17:57
博主, 为什么发不了邮件, 报错 Connection could not be established with host smtp.163.com, 开启了ssl,
Lisam
2015-10-22 16:34
2015-10-22 16:34
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
"

博主又遇到了这个错误了,但是邮箱那半边smtp已开启 


Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
"






亲爱的楼主
2015-09-24 18:49
2015-09-24 18:49
Failed to authenticate on SMTP server with username "xxxxxx@163.com" using 2 possible authenticators
这个是什么错误啊
这个是什么错误啊
Lisam
2015-09-05 10:08


博主 Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
出现上面的问题了 我把'driver' => env('MAIL_DRIVER', 'smtp'),换成'driver' => 'smtp'还是一样的情况
2015-09-05 10:08




出现上面的问题了 我把'driver' => env('MAIL_DRIVER', 'smtp'),换成'driver' => 'smtp'还是一样的情况
huangdr
2015-08-26 22:36
2015-08-26 22:36
按照手册配置以后用下面的发送程序,既没有报错也收不到邮件,楼主求救
$email = '981902663@qq.com';
$name = 'test';
$uid = '123';
$code = 'test123';
$data = ['email'=>$email, 'name'=>$name, 'uid'=>$uid, 'activationcode'=>$code];
Mail::send('activemail', $data, function($message) use($data)
{
$message->to($data['email'], $data['name'])->subject('测试邮箱验证!');
});

$email = '981902663@qq.com';
$name = 'test';
$uid = '123';
$code = 'test123';
$data = ['email'=>$email, 'name'=>$name, 'uid'=>$uid, 'activationcode'=>$code];
Mail::send('activemail', $data, function($message) use($data)
{
$message->to($data['email'], $data['name'])->subject('测试邮箱验证!');
});
周周
2015-07-26 22:26
2015-07-26 22:26
Laravel 5.1 全局配置文件如下(.env):
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=zhuxmei520@163.com
MAIL_PASSWORD=198****
MAIL_ENCRYPTION=null
哪里错了,发不出邮件!
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=zhuxmei520@163.com
MAIL_PASSWORD=198****
MAIL_ENCRYPTION=null
哪里错了,发不出邮件!
redstone
2015-04-27 18:35
2015-04-27 18:35
恩,确实是一句惊醒梦中人,可以,我也是高兴有点早了,我加了之后,还是出错来了,Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 220 but got code "", with message "",
Expected response code 220 but got code "", with message "",
redstone
2015-04-27 18:26
2015-04-27 18:26
博主,你好,我想请教一下,如果我的$Email,等变量直接赋值的话,邮件可以发送,但是如果从input获取值,,就失败了,TokenMismatchException in compiled.php line 2486。这个怎么办呢?
Sara
2015-04-23 22:05
2015-04-23 22:05
@JohnLui:博主,试了您提供的方法,遇到点问题,能否解惑啊。
1 设置'host' => 'smtp.163.com', 但是公司电脑上smtp.163.com, ping 不通,连接不了服务器端。请问该怎么办呢?
2 在自己笔记本上能ping通,但是返回
“Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required”
网上说 SMTP服务器不允许从外部域发送邮件,即使是通过验证的,发送邮件必须使用同一个域。不太理解。
谢谢了。

1 设置'host' => 'smtp.163.com', 但是公司电脑上smtp.163.com, ping 不通,连接不了服务器端。请问该怎么办呢?
2 在自己笔记本上能ping通,但是返回
“Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required”
网上说 SMTP服务器不允许从外部域发送邮件,即使是通过验证的,发送邮件必须使用同一个域。不太理解。
谢谢了。
2019-10-23 15:48
//判断发送情况
if (Mail::failures()){
\Log::debug('Mail not sent');
return false;
}else{
\Log::debug('Mail sent successfully.');
return true;
}
可是这个拦截不到这个错误。