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

rajkumar_zuan's avatar

Laravel Dynamic Custom SMTP

Hey everyone, I'm currently working on a Laravel multivendor application and need some help with implementing custom SMTP settings for each vendor. The goal is to ensure that whenever an email is triggered, it is sent using the specific vendor's SMTP settings.

I’ve also implemented Queues for Jobs and notifications. However, I’m facing an issue where even though I try to change the SMTP configuration dynamically before triggering the email, it still sends emails via the default ENV configuration.

Before sending Email I update config values

Config::set('mail.host', $host);
Config::set('mail.port', $port);
Config::set('mail.username', $username);
Config::set('mail.password', $password);

Has anyone worked on something similar or come across this issue? Any suggestions or examples from similar projects would be greatly appreciated!

Thanks in advance!

0 likes
4 replies
aleahy's avatar

I've never done this before, but I was just looking through the framework code as I was curious.

The Illuminate\Mail\MailServiceProvider registers a singleton MailManager and a Mailer which is built by the MailManager. Since this already exists in the container and has read the config settings by the time it gets to your code, changing the config settings doesn't do anything.

You will need to create a new Mailer for the vendor. And looking through the Notifications documentation, you can customise the mailer being used:

public function toMail(object $notifiable): MailMessage
{
    return (new MailMessage)
                ->mailer('postmark')
                ->line('...');
}

So instead of postmark you would use the Mailer class that you create for your vendor.

As I said, I've never done it before and this is all theory. Hopefully it points you in the right direction.

1 like
puklipo's avatar

Even if you run a queue job after changing the config, the config will be restored.

On-demand changes to the config are only effective during the current request cycle.

If you use Mail directly instead of notifications. Change the config in the queue job and send without using the queue.

// Job

public function handle(): void
{
    Config::set();
    Mail::to()->send();
}

For notifications, do not use the queue.

1 like
Snapey's avatar

you could pass the mailer you want to use into the queued job, and then setup the world how you need it from there.

Btw make sure to ensure each vendor has spf records for your server, or you are wasting your time

1 like
vishal-gadhiya's avatar

You can use the forgetMailers method when you change the mail configuration.

Mail::forgetMailers();

Please or to participate in this conversation.