Custom verificarion email in laravel issue

Tags: , , , ,



guys in my laravel application i’m trying to send my users a custom verification email, as i’m using language translations

So as the first step I’ve created following custom email template in my App/Notifications folder CustomVerifyEmailNotification.php

<?php

namespace IlluminateAuthNotifications;

use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
use IlluminateSupportCarbon;
use IlluminateSupportFacadesConfig;
use IlluminateSupportFacadesLang;
use IlluminateSupportFacadesURL;

class CustomVerifyEmailNotification extends Notification
{
    /**
     * The callback that should be used to build the mail message.
     *
     * @var Closure|null
     */
    public static $toMailCallback;

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return IlluminateNotificationsMessagesMailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return (new MailMessage)
            ->subject(Lang::get(''.('sentence.Hello friend. Verify Email Address').''))
            ->line(Lang::get(''.('sentence.If you did not create an account, no further action is required.').''));
    }

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

and following is my User.php

<?php

namespace App;

use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelCashierBillable;
use SpatiePermissionTraitsHasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable,Billable;
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'name','last_name', 'email', 'password','username','mobile','propic','user_roles','user_source',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

How can I inject my custom email template here?

I’m using laravel 6 and following is my MustVerifyEmail.php trait

<?php

namespace IlluminateAuth;

use IlluminateAuthNotificationsVerifyEmail;

trait MustVerifyEmail
{
    /**
     * Determine if the user has verified their email address.
     *
     * @return bool
     */
    public function hasVerifiedEmail()
    {
        return ! is_null($this->email_verified_at);
    }

    /**
     * Mark the given user's email as verified.
     *
     * @return bool
     */
    public function markEmailAsVerified()
    {
        return $this->forceFill([
            'email_verified_at' => $this->freshTimestamp(),
        ])->save();
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    /**
     * Get the email address that should be used for verification.
     *
     * @return string
     */
    public function getEmailForVerification()
    {
        return $this->email;
    }
}

Current verification process works properly but I need to send that customized email to my users.

Answer

Now you have to ovewrite sendEmailVerificationNotification() function in order to use your Notification CustomVerifyEmailNotification.

So, in your User.php you have to write a function sendEmailVerificationNotification like:

<?php

namespace App;

use App/Notifications/CustomVerifyEmailNotification; // use your custom Notification
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelCashierBillable;
use SpatiePermissionTraitsHasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable,Billable;
    use HasRoles; 


    public function sendEmailVerificationNotification() 
    {
        $this->notify(new CustomVerifyEmailNotification);
    }
}

Then you can customize the email in your custom notification



Source: stackoverflow