005-Lumen5.1 使用Mail邮件

  1. 安装邮件功能所依赖的包
composer require illuminate/mail "~5.1.41"
composer require guzzlehttp/guzzle
composer require jeremeamia/superclosure
  1. 邮件配置.env文件
MAIL_DRIVER=smtp
MAIL_HOST=smtp.126.com
MAIL_PORT=994
MAIL_USERNAME=你的126邮箱地址
MAIL_PASSWORD=授权码
[email protected]
MAIL_FROM_NAME=纽斯特心理
MAIL_ENCRYPTION=ssl

注意:

1, 申请126邮箱地址
2. 开启smtp
005-Lumen5.1 使用Mail邮件_第1张图片
126开启smp设置.png

005-Lumen5.1 使用Mail邮件_第2张图片
163|126邮件地址
  1. 注册mailer在bootstrap/app.php中, return $app;之前,增加以下内容:
$app->singleton('mailer', function () use ($app) {
    return $app->loadComponent('mail', Illuminate\Mail\MailServiceProvider::class, 'mailer');
});
  1. 发送邮件
// 模板
$app->get('/mail', function () {
    $data = [
        'link' => 'http://com-nst.yunfeng365.com',
    ];
    $user = new stdClass();
    $user->username = '小红';
    $user->email = '[email protected]';

    $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 = '[email protected]';
    try {
        \Illuminate\Support\Facades\Mail::raw('测试发送邮件', function ($m) use($user) {
            $m->to($user->email, $user->username)->subject('找回密码!');
        });

    } catch (\Exception $e) {
        dd($e->getMessage());
    }
});

你可能感兴趣的:(005-Lumen5.1 使用Mail邮件)