Configure ActionMailer or Mail delivery settings based on the environment
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.
sendmail and file adapters both respect the ActionMailer locationsendmail:/// or file:///.smtp URL, it will be used as domain rather thanlocation (minus the leading slash).domain is set, the default from address will be set to noreply atHere’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.
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.