Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cuartas15's avatar

How to change the email configuration for Laravel notifications on the fly?

I'm using Laravel email notifications to send my emails because that's enough for my needs.

Now, I need to use a new email configuration for a specific type of email I pretend to send, so the mailgun config I have on my .env file cannot be used, I've found guides but using mailable directly, I sill wanna keep using notifications for consistency.

This is basically the layout for each type of message I'm sending throughout my application:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SmeClientMailContact extends Notification
{
    use Queueable;

    /**
     * Create a notification instance.
     *
     * @param  string  $message
     * @param  string  $email
     * @param  string  $phone
     * @param  string  $contactName
     * @return void
     */
    public function __construct($message, $email, $phone, $contactName)
    {
        $this->message = $message;
        $this->email = $email;
        $this->phone = $phone;
        $this->contactName = $contactName;
    }

    /**
     * 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)
    {
        return (new MailMessage)
            ->from(config('app.contact_email'), config('app.contact_email_name'))
            ->subject('Su Médico Especialista - Mensaje de contacto de ' . $this->contactName)
            ->greeting('Mensaje de contacto de ' . $this->contactName)
            ->line('Este cliente acaba de enviar un correo de contacto a sumedicoespecialista.')
            ->line('Nombre: ' . $this->contactName)
            ->line('Correo electrónico: ' . $this->email)
            ->line('Teléfono: ' . $this->phone)
            ->line('Mensaje: ' . $this->message);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

What's the way to manually set the mail driver, host, port, etc for this notification specifically for example.

Thanks in advance

0 likes
4 replies
bobbybouwmann's avatar

You have two options here. Setting the config during runtime and replace it with another driver or create a custom notification channel which is the same as mail, but uses something else.

To override the config during runtime you can do something like this

Config::set('mail.driver', 'sparkpost');

// Do your notification stuff here. 

Config::set('mail.driver', 'mailgun');

You can put this logic inside your notification class or around the part where you figure the notification. It doesn't really matter in the end.

cuartas15's avatar

Yeah, that'll do the trick, but I have a question, what if there's more than one operation of sending an email at the same time? am I directly messing with the .env file by doing this? is this gonna affect other notifications that need the default configuration instead of the one modified on runtime?

thewebartisan7's avatar

I have similar issue years ago, and I found this https://github.com/molteber/puz-dynamic-mail

Not sure if still work but you can try.

The solution proposed by bobby may not work because you must call:

(new \Illuminate\Mail\MailServiceProvider(app()))->register();

Or that was years ago, I didn't tested recently.

But changing config on runtime will not change your .env file, just during the execution time, you can change config, and will be reset at the end of execution.

You can just change back config after you use new one, in case works.

Please or to participate in this conversation.