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.