The mta-settings gem enables transparent MTA (mail transport agent)
configuration from the environment for both ActionMailer and Mail,
based on either an explicit MTA_URL variable or popular conventions for
Sendgrid, Mandrill, Postmark, and Mailgun (as provided by Heroku addons, for
example).
Add this line to your application's Gemfile:
gem 'mta-settings'
If mta-settings is required (which Bundler does automatically by default),
ActionMailer configuration is fully automatic. With Rails, be aware that
config.action_mailer assignments will take precedence, so you might want to
strip those out of your apps config/environments/ files.
Mail.defaults do
delivery_method *MtaSettings.from_env
# delivery_method *MtaSettings.from_url(ENV['MTA_URL'])
end
Configuration will happen based on the presence of the following environment variables, in order of decreasing precedence:
MTA_PROVIDER: points to another environment variable containing an MTA URLMTA_URL: See belowSENDGRID_USERNAME: SendgridMANDRILL_APIKEY: MandrillPOSTMARK_API_TOKEN: PostmarkMAILGUN_SMTP_LOGIN: MailgunMAILTRAP_API_TOKEN: Mailtrap (for development)
If no supported environment variable is found, the configuration is left blank. This enables easy defaulting:
ActionMailer::Base.delivery_method ||= :letter_opener
The scheme of an MTA URL is used to set the delivery method. The user,
password, host, port, and path portions are used to populate the user_name,
address, port, and location settings of the chosen delivery method.
Query parameters are then merged in.
- The
sendmailandfileadapters both respect the ActionMailerlocationdefaults, so you can just givesendmail:///orfile:///. - If a path is given in an
smtpURL, it will be used asdomainrather thanlocation(minus the leading slash). - If
domainis set, the default from address will be set tonoreplyat that domain.
Here's an example for Gmail:
smtp://username%40gmail.com:password@smtp.gmail.com:587/
Using an MTA URL is highly recommended even when your SMTP provider is supported out of the box. MTA URLs are much easier to copy between environments or try out locally for debugging.