Read more

How to set up SMTP email delivery with a Gmail account

Jakob Scholz
January 28, 2020Software engineer at makandra GmbH

If you want to make your Rails application be capable of sending SMTP emails, check out the action mailer configuration section Show archive.org snapshot in the Ruby on Rails guide.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

TL;DR you will end up having an smtp_settings hash that looks something like this:

smtp_settings = {
  address: ...,
  domain: ...,
  port: ...,
  user_name: ...,
  password: ...,
  authentication: ...,
  tls: ...,
  enable_starttls_auto: ...,
}

This hash can be set as the delivery_method in your environment or individually applied to an email generated in your mailer:

# production.rb:
Rails.application.configure do
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = smtp_settings
end

# test_mailer.rb
def test_mail
  mail(to: 'mail@example.com', subject: 'Subject').delivery_method.settings.merge!(smtp_settings)
end

Configuring SMTP with a Google account

These is an example configuration you can use to test if mail delivery works in your application:

smtp_settings {
  address: 'smtp.gmail.com',
  domain: 'smtp.gmail.com',
  port: 587,
  user_name: 'your_email@google.com',
  password: 'google_app_password',
  authentication: 'login',
  tls: false,
  enable_starttls_auto: true,
}

If you are using Google with 2FA, you still can deliver mails

The solution for SMTP mail delivery from a google account with two factor authentication is described here Show archive.org snapshot . You basically create a password that bypasses your security efforts. So make sure that you DONT GIVE IT AWAY, Google also suggests you to not write it down, etc.

Posted by Jakob Scholz to makandra dev (2020-01-28 15:36)