Rails : Common SMTP settings

Gmail

# config/initializers/smtp_settings.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :user_name            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

 

SendGrid

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings   = {
    :address              => 'smtp.sendgrid.net',
    :port                 => '587',
    :user_name            => ENV['smtp_username'],
    :password             => ENV['smtp_password'],
    :authentication       => 'plain',
    :enable_starttls_auto => true
}

Many hosting providers and ISPs block port 25 as a default practice. When trying to connect to smtp.sendgrid.net remember that ports 25, 2525, 587, and 465 are all available for use.

We recommend port 587 to avoid any rate limiting that your server host may apply.

Setting attributes and their meaning

Allows detailed configuration for :smtp delivery method:

  • :address – Allows you to use a remote mail server. Just change it from its default "localhost" setting.
  • :port – On the off chance that your mail server doesn’t run on port 25, you can change it.
  • :domain – If you need to specify a HELO domain, you can do it here.
  • :user_name – If your mail server requires authentication, set the username in this setting.
  • :password – If your mail server requires authentication, set the password in this setting.
  • :authentication – If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain (will send the password in the clear), :login (will send password Base64 encoded) or :cram_md5 (combines a Challenge/Response mechanism to exchange information and a cryptographic Message Digest 5 algorithm to hash important information)
  • :enable_starttls_auto – Detects if STARTTLS is enabled in your SMTP server and starts to use it. Defaults to true.
  • :openssl_verify_mode – When using TLS, you can set how OpenSSL checks the certificate. This is really useful if you need to validate a self-signed and/or a wildcard certificate. You can use the name of an OpenSSL verify constant (‘none’, ‘peer’, ‘client_once’, ‘fail_if_no_peer_cert’) or directly the constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER, …).

 

Sources

http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration

https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

https://cbabhusal.wordpress.com/2014/09/29/easiest-way-to-send-email-via-rails-action-mailer/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s