0sxmrw
Last Updated: March 18, 2019
·
2.954K
· kylewelsby

Rails ActionMailer SMTP Settings with Environment Variables

When debugging Rails ability of sending email via SMTP with Mandrill it became apparent that you have to be strict.

Reading their docs you'll see.

config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 25, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => "MANDRILL_USERNAME",
    :password  => "MANDRILL_PASSWORD", # SMTP password is any valid API key
    :authentication => 'login', # Mandrill supports 'plain' or 'login'
    :domain => 'yourdomain.com', # your domain to identify your server when connecting
  }

so you will have to make-sure in your app if you are using environment variables is just like this.

config.action_mailer.smtp_settings = {
    :port           => ENV['SMTP_PORT'].to_i,
    :address        => ENV['SMTP_HOST'],
    :user_name      => ENV['SMTP_USERNAME'],
    :password       => ENV['SMTP_PASSWORD'],
    :domain         => 'example.com',
    :enable_starttls_auto => true,
    :authentication => 'login',
  }

happy mailing

1 Response
Add your response

29771

To have less env variables can pass it in URL format:

if ENV["SMTP_URL"]
  smtp_url = URI.parse(ENV["SMTP_URL"])
  self.smtp_settings = {
    address:   smtp_url.host,
    port:      smtp_url.port,
    user_name: smtp_url.user ? URI.decode_www_form_component(smtp_url.user) : nil,
    password:  smtp_url.password,
    enable_starttls_auto: Rack::Utils.parse_nested_query(smtp_url.query)['tls'].in?(['true', '1', 1, true, 'True', 'TRUE'])
  }
end
12 months ago ·