如何在 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 教程】【完结】 →
评论:
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('测试邮箱验证!');
});
2016-09-08 09:12