laravel 5.7 以队列方法发送邮件(三种场景) | laravel china 社区-380玩彩网官网入口

laravel 5.7 自带了email verification 的功能,使得整个邮件注册激活过程更加简单。注册用户时发送激活邮件、重新发送邮件、密码重置邮件,这些功能几乎都是 out of box的。但默认都是同步发送,太慢了,所以改为队列异步发送!当然,前提是你已经开启了redis队列并且已经理解了5.7的这些邮件发送处理流程。

1. 注册时发送邮件

  • 创建发送注册邮件的job
    php artisan make:job sendregisteruseremail
  • job类的代码如下:

    class sendregisteruseremail implements shouldqueue
    {
        use dispatchable, interactswithqueue, queueable, serializesmodels;
        protected $user;
        /**
            * create a new job instance.
            *
            * @param         $user
            */
        public function __construct($user)
        {
            $this->user = $user;
        }
        /**
            * execute the job.
            *
            * @return void
            */
        public function handle()
        {
            event(new registered($this->user));
        }
    }
  • 新增 \app\http\controllers\auth\registercontroller::register来覆盖\illuminate\foundation\auth\registersusers::register,因为发送邮件是在这个方法中进行的。代码如下:
    /**
    * handle a registration request for the application.
    *
    * @param request $request
    *
    * @return \illuminate\http\response
    */
    public function register(request $request)
    {
        $this->validator($request->all())->validate();
        $user = $this->create($request->all());
        //队列job发送邮件
        dispatch(new sendregisteruseremail($user));
        $this->guard()->login($user);
        return $this->registered($request, $user)?: redirect($this->redirectpath());
    }

2. 重新发送邮件

  • app/http/controllers/auth/verificationcontroller.php 中新增 resend 方法,来复写 trait verifiesemails 中 的方法,从而队列异步发送邮件。代码如下:
    /**
    * resend the email verification notification.
    *
    * @param  \illuminate\http\request  $request
    * @return \illuminate\http\response
    */
    public function resend(request $request)
    {
      if ($request->user()->hasverifiedemail()) {
          return redirect($this->redirectpath());
      }
      //队列job发送邮件
      dispatch(new sendregisteruseremail($request->user()));
      return back()->with('resent', true);
    }

3. 重置密码邮件

  • php artisan make:job sendresetpasswordemail
  • 修改 sendresetpasswordemail中的代码,如下:

    class sendresetpasswordemail implements shouldqueue
    {
      use dispatchable, interactswithqueue, queueable, serializesmodels;
      protected $user;
      protected $token;
      /**
       * create a new job instance.
       *
       * @param user $user
       * @param      $token
       */
      public function __construct(user $user, $token)
      {
          $this->user = $user;
          $this->token = $token;
      }
      /**
       * execute the job.
       *
       * @return void
       */
      public function handle()
      {
          $this->user->notify(new resetpassword($this->token));
      }
    }
  • 修改 \app\models\user::sendpasswordresetnotification 这个方法,代码如下:
    /**
    * send the password reset notification.
    *
    * @param  string $token
    *
    * @return void
    */
    public function sendpasswordresetnotification($token)
    {
      dispatch(new sendresetpasswordemail($this, $token));
    }

4.注意:开启redis队列发送邮件,一定要注意代码缓存问题。详见

all done!

本作品采用《cc 协议》,转载必须注明作者和本文链接
日拱一卒
本帖由系统于 6年前 自动加精
以构建论坛项目 larabbs 为线索,展开对 laravel 框架的全面学习。应用程序架构思路贴近 laravel 框架的设计哲学。
从零开始带你一步步开发一个 go 博客项目,让你在最短的时间内学会使用 go 进行编码。项目结构很大程度上参考了 laravel。
讨论数量: 4

可以试试 notification,一样支持队列

6年前

你应该在handle里面发送邮件,然后把handle里面的事件移到控制器中才对

5年前

(new anonymousnotifiable)
                ->route('mail', $email)
                ->notify(
                    new \app\user\notification\userhasregistered(
                        $event->getuser()
                    )
                );
5年前

不知道为什么发不出去私信,25772111

2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图