- 安装邮件功能所依赖的包
composer require illuminate/mail "~5.1.41"
composer require guzzlehttp/guzzle
composer require jeremeamia/superclosure
- 邮件配置
.env
文件
MAIL_DRIVER=smtp
MAIL_HOST=smtp.126.com
MAIL_PORT=994
MAIL_USERNAME=你的126邮箱地址
MAIL_PASSWORD=授权码
MAIL_FROM_ADDRESS=feng469306621@126.com
MAIL_FROM_NAME=纽斯特心理
MAIL_ENCRYPTION=ssl
注意:
1, 申请126邮箱地址
2. 开启smtp
- 注册mailer
在bootstrap/app.php
中,return $app;
之前,增加以下内容:
$app->singleton('mailer', function () use ($app) {
return $app->loadComponent('mail', Illuminate\Mail\MailServiceProvider::class, 'mailer');
});
- 发送邮件
// 模板
$app->get('/mail', function () {
$data = [
'link' => 'http://com-nst.yunfeng365.com',
];
$user = new stdClass();
$user->username = '小红';
$user->email = '469306621@qq.com';
$res = \Illuminate\Support\Facades\Mail::send('mails.recoverpw', $data, function ($m) use($user) {
$m->to($user->email, $user->username)->subject('找回密码!');
});
dd($res);
// 纯文本
$user = new stdClass();
$user->username = '小红';
$user->email = '469306621@qq.com';
try {
\Illuminate\Support\Facades\Mail::raw('测试发送邮件', function ($m) use($user) {
$m->to($user->email, $user->username)->subject('找回密码!');
});
} catch (\Exception $e) {
dd($e->getMessage());
}
});