How to customize the email verification email from Laravel 5.7?

前端 未结 8 1527
感动是毒
感动是毒 2020-12-03 02:59

I just upgraded to Laravel 5.7 and now I am using the built in Email Verification. However there is 2 things I have not been able to figure out and the primary issue is how

8条回答
  •  既然无缘
    2020-12-03 03:40

    In Route File

    Auth::routes(['verify' => true]);
    

    In AppServiceProvider.php File

    namespace App\Providers;
    use App\Mail\EmailVerification;
    use Illuminate\Support\ServiceProvider;
    use View;
    use URL;
    use Carbon\Carbon;
    use Config;
    use Illuminate\Auth\Notifications\VerifyEmail;
    use Illuminate\Notifications\Messages\MailMessage;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            // Override the email notification for verifying email
            VerifyEmail::toMailUsing(function ($notifiable){        
                $verifyUrl = URL::temporarySignedRoute('verification.verify',
                \Illuminate\Support\Carbon::now()->addMinutes(\Illuminate\Support\Facades 
                \Config::get('auth.verification.expire', 60)),
                [
                    'id' => $notifiable->getKey(),
                    'hash' => sha1($notifiable->getEmailForVerification()),
                ]
            );
            return new EmailVerification($verifyUrl, $notifiable);
    
            });
    
        }
    }
    

    Now Create EmailVerification With Markdown

    php artisan make:mail EmailVerification --markdown=emails.verify-email
    

    Edit The EmailVerrification as you want and the blade file

    class EmailVerification extends Mailable
    {
        use Queueable, SerializesModels;
        public $verifyUrl;
        protected $user;
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct($url,$user)
        {
            $this->verifyUrl = $url;
            $this->user = $user;
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            $address = 'mymail@gmail.com';
            $name = 'Name';
            $subject = 'verify Email';
            return $this->to($this->user)->subject($subject)->from($address, $name)->
            markdown('emails.verify',['url' => $this->verifyUrl,'user' => $this->user]);
        }
    }
    

    in the blade file change the design as you want and use verifyUrl to display the verification link and $user to display user information

    thanks, happy coding :)

提交回复
热议问题