ProgZi
ProgZi

Reputation: 1267

Laravel send email to (notify) user using another email?

Some users have another email address that is stored as secondary_email column in users table.

How to send Notification using $user->notify() to this secondary email address?

Upvotes: 2

Views: 2867

Answers (4)

Omer Abdelmajeed
Omer Abdelmajeed

Reputation: 364

The documented solution using the routeNotificationForMail method is invalid for all cases, for example, when the email is changed in a specific notification only. Here we do not want to change the default email. as documented here

class User extends Authenticatable
{
    use Notifiable;
 
    /**
     * Route notifications for the mail channel.
     *
     * @return  array<string, string>|string
     */
    public function routeNotificationForMail(Notification $notification): array|string
    {
        // Return email address only...
        return $this->email_address;
 
        // Return email address and name...
        return [$this->email_address => $this->name];
    }
}

Avoiding the need to change the email using the Mailable class as in the selected answer, I have managed to overcome this situation by simply changing the email property of the user I'm sending the email to, as follows:

$user->email = $user->alternative_email;
$user->notify(new AlternativeEmailNotification());

Upvotes: 0

whoacowboy
whoacowboy

Reputation: 7447

I know this post is old but it's new to me.

I found a way to do this that met my needs a little better. In our app, when a user changes their email address I keep their old email address active until they confirm the new one.

I wanted to use the Notification because it's the same email that I send for the original email confirmation.

class EmailUpdate extends Notification
{
    use Queueable, SerializesModels;

    private $email;

    public function __construct($email)
    {
        $this->email = $email;
    }

    /**
     * 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 \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $notifiable->email = $this->email ?? $notifiable->email;
        $url = config('app.site_url'). '/confirm/email/'. $notifiable->activation_code;

        return (new MailMessage())
            ->subject('My subject.')
            ->line('Hey ' . $notifiable->username . ', we received a request to change your email.')
            ->action('Verify Email Address', $url)
            ->line('If you did not create an account, no further action is required.');
    }
}

When I want to send the first confirmation I use

$user->notify(new EmailUpdate);

When I want to notify them with the new email address I use

$user->notify(new EmailUpdate($user->new_email));

Upvotes: 1

developers ukwelys
developers ukwelys

Reputation: 141

you can define a routeNotificationForMail method on the notifiable entity model

  public function routeNotificationForMail($notification)
    {
        // Return email address only...
        return $this->email_address;

        // Return email address and name...
        return [$this->email_address => $this->name];
    }

and then modify the function to suit your need. laravel documentation notifications

Upvotes: 2

Brian Lee
Brian Lee

Reputation: 18197

You would override the toMail method of your notification. For example:

use App\Mail\InvoicePaid as Mailable;

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return Mailable
 */
public function toMail($notifiable)
{
    return (new Mailable($this->invoice))
           ->to($this->user->secondary_email ?? $this->user->email);
}

You can read more about formatting notifications here.

Upvotes: 2

Related Questions