zoukankan      html  css  js  c++  java
  • Laravel 5.8 做个知乎 17 —— 重构邮件系统

    1 类

    1.1 appMailerMailer.php 基类

    <?php
    /**
     * Created by PhpStorm.
     * User: SUN
     * Date: 2021/8/2
     * Time: 18:23
     */
    namespace AppMailer;
    use NauxMailSendCloudTemplate;
    use IlluminateSupportFacadesMail;
    
    class Mailer
    {
        
        /**
         * @param       $template 邮件模板
         * @param       $email    邮件地址
         * @param array $data    传递参数
         */
       public function sendTo($template,$email, array $data)
       {
           //模板地址
           //https://www.sendcloud.net/email/#/sendAround/template
           $content = new SendCloudTemplate($template,$data);
           Mail::raw($content,function ($message) use ($email){
               $message->from(env('SEND_CLOUD_FROM'),'知乎管理员');
               $message->to($email);
           });
       }
    }
    View Code

    1.2 继承基类  appMailerUserMailer.php

    <?php
    /**
     * Created by PhpStorm.
     * User: SUN
     * Date: 2021/8/2
     * Time: 20:33
     */
    namespace  appMailer;
    use IlluminateSupportFacadesAuth;
    
    class UserMailer extends Mailer
    {
        public function followNotifyEmail($email)
        {
            $data = [
              'url' => url(config('app.url')),
              'name' => Auth::guard('api')->user()->name
            ];
            $this->sendTo('zhihu_app_new_user_follow',$email,$data);
        }
        
        public  function  passwordReset($email,$token)
        {
            $data = [
              'url'=>route('password.reset',['token'=>$token])
            ];
            $this->sendTo('zhihu_app_password_reset',$email,$data);
        }
        
        //https://www.sendcloud.net/email/#/sendAround/template
        public function welcome(User $user)
        {
            $data = [
              'url'=>route('email.verify',['token'=>$user->activation_token])
            ];
            $this->sendTo('test_template_active',$user->email,$data);
        }
    
    
    }
    View Code

    2 使用

    2.1 appNotificationsNewUserFollowNotinfication.php 用户关注发通知

        public function toSendcloud($notifiable)
        {
            //模板地址
            //https://www.sendcloud.net/email/#/sendAround/template
            /*$data = [
              'url' => url(config('app.url')),
              'name' => Auth::guard('api')->user()->name
            ];
            
            //test_template 邮件模板
            $template = new SendCloudTemplate('zhihu_app_new_user_follow',$data);
            Mail::raw($template,function ($message) use ($notifiable){
                $message->from(env('SEND_CLOUD_FROM'),'知乎管理员');
                $message->to($notifiable->email);
            });*/
            (new UserMailer())->sendTo($notifiable->email);
            
        }

    2.2 appHttpControllersAuthRegisterController.php用户注册发通知

        private function sendVerifyEmailTo($user)
        {
            (new UserMailer())->welcome( $user);
        }

    2.3  appUser.php 重置密码

        public function sendPasswordResetNotification($token)
        {
            (new UserMailer())->passwordReset($this->email,$token);
        }
  • 相关阅读:
    【算法】二分图的判定
    【模板】并查集 两种路径压缩写法(类模板和函数模板)
    【模板】堆的结构
    新疆大学OJ(ACM) 1099: 数列有序!
    新疆大学OJ(ACM) 1047: string 字符串排序
    新疆大学(新大)OJ xju 1010: 四个年级 C++ STL map 将4层循环优化成2层循环可解
    新疆大学(新大)OJ xju 1009: 一带一路 prim求最短路径+O(n)素数筛选
    新疆大学(新大)OJ xju 1006: 比赛排名 第二类斯特林数+阶乘
    【算法】第二类斯特林数Stirling
    【复习资料】编译原理中:短语,直接短语,句柄
  • 原文地址:https://www.cnblogs.com/polax/p/15091690.html
Copyright © 2011-2022 走看看