Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how-to-customize-the-email-verification-email-from-laravel-8

Step: 1

Create a new notification UserVerifyNotification class. It should extend the VerifyEmail class from the library IlluminateAuthNotificationsVerifyEmail;

Code :
use IlluminateAuthNotificationsVerifyEmail;
    ...
    class UserVerifyNotification extends VerifyEmail implements ShouldQueue
    {
        use Queueable;
        public $user;            //you'll need this to address the user
    
        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct($user='')
        {
            $this->user =  $user ?: Auth::user();         //if user is not supplied, get from session
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            return ['mail'];
        }
    
        /**
         * Get the mail representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return IlluminateNotificationsMessagesMailMessage
         */
        public function toMail($notifiable)
        {
            $actionUrl  = $this->verificationUrl($notifiable);     //verificationUrl required for the verification link
            $actionText  = 'Click here to verify your email';
            return (new MailMessage)->subject('Verify your account')->view(
                'emails.user-verify',
                [
                    'user'=> $this->user,
                    'actionText' => $actionText,
                    'actionUrl' => $actionUrl,
                ]);
        }
    
        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toArray($notifiable)
        {
            return [
                //
            ];
        }
        
    }
Step:2

Create the blade view for the email (user-verify.blade.php) inside resourcesviewsemails
Step:3

Add the following method inside the User model
class User extends Authenticatable implements MustVerifyEmail
   {
    use HasFactory, Notifiable;

    ...
    ...
    ...

    public function sendEmailVerificationNotification()
    {
        $this->notify(new AppNotificationsUserVerifyNotification($this->getAttributes()));  //pass the currently logged in user to the notification class
    }

}

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
2+8 =