Project

multi_smtp

0.0
No commit activity in last 3 years
No release in over 3 years
Email failover in Rails with MultiSMTP
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
>= 0.10.1, ~> 0.10
~> 10.0
>= 3.1.0, ~> 3.1

Runtime

>= 2.6.3, ~> 2.6
 Project Readme

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

  1. Fork it ( https://github.com/[my-github-username]/multi_smtp/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request