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

NickSmithTech's avatar

Fallback SMTP Server

This seems like a simple, obvious need every robust application requires, but I can't seem to find a recommended solution...

SMTP services go down, and when they go down email still needs to happen. We use Mandrill and would like to have Mailgun and Sendgrid as backups.

Is there a prescribed way to fallback to other SMTP services?

This way when our notifications are sent, if there is an exception (like a timeout) the system would try the backup service?

Thanks!

0 likes
5 replies
LaryAI's avatar
Level 58

One solution to this problem is to use Laravel's built-in mail driver fallback feature. This allows you to specify multiple mail drivers in your configuration file, and if the primary driver fails, Laravel will automatically try the next driver in the list.

Here's an example configuration file that uses Mandrill as the primary driver, with Mailgun and Sendgrid as fallbacks:

'driver' => 'mandrill',
'host' => 'smtp.mandrillapp.com',
'port' => 587,
'from' => ['address' => '[email protected]', 'name' => 'Example'],
'encryption' => 'tls',
'username' => 'your-mandrill-username',
'password' => 'your-mandrill-password',

'backup' => 'mailgun',
'mailgun' => [
    'driver' => 'mailgun',
    'host' => 'smtp.mailgun.org',
    'port' => 587,
    'from' => ['address' => '[email protected]', 'name' => 'Example'],
    'encryption' => 'tls',
    'username' => 'your-mailgun-username',
    'password' => 'your-mailgun-password',
],

'sendgrid' => [
    'driver' => 'sendgrid',
    'host' => 'smtp.sendgrid.net',
    'port' => 587,
    'from' => ['address' => '[email protected]', 'name' => 'Example'],
    'encryption' => 'tls',
    'username' => 'your-sendgrid-username',
    'password' => 'your-sendgrid-password',
],

In this example, if Mandrill fails to send the email, Laravel will automatically try to send it using Mailgun. If Mailgun also fails, it will try Sendgrid.

To use this configuration, simply set the MAIL_DRIVER environment variable to smtp:

MAIL_DRIVER=smtp

And that's it! Laravel will now automatically fallback to your backup mail drivers if the primary driver fails.

NickSmithTech's avatar

That sounds great—would love to be able to verify this somewhere.

Is there a link to official (any?) documentation discussing this feature so I can be sure this isn't an AI hallucination? I can't find anything.

NickSmithTech's avatar

Thanks a ton!

I knew I had seen this somewhere, but just couldn't seem to locate it.

Please or to participate in this conversation.