laravel email邮箱发送

1.生成中间件

 php artisan make:middleware EmailMiddlware

2.下载laravel email 扩展包

     https://packagist.org/
     composer require nette/mail
     扩展包 nette/mai

3.按照需求在中间中添加

use Nette\Mail\SmtpMailer;
public function handle($request, Closure $next)
{
    $res = $next($request);

    if($request->user()){
        $mail = new Message;
        $mail->setFrom('John ')
             ->addTo($request->user()->email)
             ->setSubject('您已成功注册Spicy')
             ->setBody("尽情享受,属于你的时间吧 !!");
        $mailer = new SmtpMailer([
            'host' => 'smtp.163.com',
            'username' => 'jack@example',
            'password' => '*****',
            // 证书
            // 'secure' => 'ssl',
            // 'context' =>  [
            //     'ssl' => [
            //         'capath' => '/path/to/my/trusted/ca/folder',
            //      ],
            // ],
        ]);
        $mailer->send($mail);
    }
    return $res;
}

4.中间建调用

        Route::post('path',
        [
            'middleware' => 'App\Http\Middleware\EmailMiddleware' ,
            'uses'       => 'Auth\AuthController@postRegister'
        ]);

你可能感兴趣的:(laravel)