如何在 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。
2019-10-23 15:48
//判断发送情况
if (Mail::failures()){
\Log::debug('Mail not sent');
return false;
}else{
\Log::debug('Mail sent successfully.');
return true;
}
可是这个拦截不到这个错误。