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

mistre83's avatar

Sending email from different SMTP configuration

Hi to all, i need to send emails from different SMTP settings.

For example, confirm order email should be sent from SMTP setting 1, and user welcome should be sent from SMTP config 2.

In .env i can configure multiple email settings (are just keys), but how i can pick it in Mailable classes ?

I'm using a queue to send emails.

Thanks!

0 likes
3 replies
bobbybouwmann's avatar

You can override the config during runtime

// In some controller
public function sendEmail(Request $request)
{
    Config::set('mail.host', 'smtp1.sample.com');

    Mail::to($request->user())->send(new OrderShipped($order));

    Config::set('mail.host', 'smtp2.sample.com');

    Mail::to($request->user())->send(new OrderShipped($order));
}

In the end you can write a service that actually handles this for you, for example

$config = [
    'driver' => 'smtp',
    'host' => 'smtp1.example.com',
    'username' => foo',
    'password' => 'bar'
]

Config::set('mail', $config);
margaridaguerra's avatar

I have tested this scenario and it is not working.

I am using Laravel 5.5 and config settings are not being set at runtime. It always use the settings on app\mail.php

Any help?

1 like

Please or to participate in this conversation.