MultiSMTP
Email delivery is a critical component of many web applications. Occasionally distributed third-party services can experience temporary downtime. We can achieve automatic failover by overriding the default email delivery method with MultiSMTP.
MultiSMTP takes an array of (1..N) SMTP providers and will itterate over each provider untill the email is successfully sent.
Installation
Add this line to your application's Gemfile:
gem "multi_smtp"
And then execute:
$ bundle
Configuration
Set the delivery method to :multi_smtp
for each environment that should use
the automatic failover.
# config/environments/{staging,production}.rb
ExampleApp::Application.configure do
config.action_mailer.delivery_method = :multi_smtp
end
In an initializer configure the MultiSMTP class with an array of (1..N) SMTP Providers.
# config/initializers/multi_smtp.rb
sendgrid_settings = {
address: 'smtp.sendgrid.net',
authentication: :plain,
domain: 'example.com',
password: ENV['SENDGRID_PASSWORD'],
port: '587',
user_name: ENV['SENDGRID_USERNAME'],
}
other_smtp_settings = {
# Other SMTP settings
}
MultiSMTP.smtp_providers = [sendgrid_settings, other_smtp_settings]
Error Notifications
If all SMTP providers fail the default behavior is to re-raise the original exception. However, we can also specify custom notifications.
# config/initializers/multi_smtp.rb
require "multi_smtp/notifiers/airbrake"
MultiSMTP.error_notifier = MultiSMTP::Notifiers::Airbrake
If there is another type of notification you'd like to receive, you can create a
new notifier that implements the class method .notify
.
class MyCustomNotifier
def self.notify(mail)
# do something special
end
end
MultiSMTP.error_notifier = MyCustomNotifier
See the Airbrake Notifier for more details.
Contributing
- Fork it ( https://github.com/[my-github-username]/multi_smtp/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request