app/config/mail.php
return [
'driver' => 'smtp',
'host' => 'smtp.qq.com',
'port' => 465,
'from' => [
'address' => 'QQ 邮箱账号',
'name' => 'leng',
],
'encryption' => 'ssl',
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
*/
'username' => 'QQ 邮箱账号',
'password' => 'QQ邮箱 - 邮箱设置 - 账户
- POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 - 生成授权码',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
为了便于说明,这里并没有使用 env() 辅助函数读取配置文件,直接把配置写在 mail.php 文件中了。生产环境下,配置是写在 .env 文件中,然后从该文件读取配置信息。
在配置过程中踩了几个坑,排坑也花了几个小时,其实都是非常基础的问题,百度时发现其他很多人也遇到过同样的问题,比较零散,就把自己遇到的问题列出来。
QQ 邮箱帮助中心 - 常用邮件客户端软件设置
这里提到了两个配置的参数
port: 465
encryption: ssl
SMTP 服务器设置
如果加密方式不对,会踩坑
'encryption' => 'ssl'
如果端口不对,会踩坑
'port' => 465
发送邮件附件
Mail::send(
'welcome',
['name' => $name, 'image' => $image],
function($message) use ($name) {
$to = '354480205@qq.com';
$attachment = storage_path('app/public/qq.jpg');
$message->to($to)->subject('hello, xiao qing')
->attach($attachment);
});
if (count(Mail::failures())) {
dd(Mail::failures());
} else {
return 'success';
};
邮件附件