Configuring ActionMailer and Gmail

Techie     October 2022

Introduction

When ActionMailer and Gmail settings are properly configured, we can easily send emails from controller actions by generating and setting up mailers.

The set up is really easy. Let’s have a look.


Part A: Setting Up Gmail App Passwords

ActionMailer will use your gmail account to send mail, but you can not use your normal gmail password with external applications. Gmail has a nifty feature called App passwords that lets you sign in to your Google Account from apps on devices that don’t support 2-Step Verification. Click here to set up App passwords. On the page, select ‘Mail’ under the ‘select app’ tab, then enter a custom name under the ‘select device’ tab.

Click on the ‘Generate’ button to generate the password. Just like your normal password, this app password grants complete access to your Google Account. Copy 16-character password shown, you will need it for the next step.


Part B: Setting Up Environment Variables For the Email Credentials

1 . In your rails project, create a ruby hidden file in /config directory that will contain the ENV variables. Prepend a dot . in the file name to make it hidden.

e.g .my_secret.rb, which will contain this:

# /config/.my_secret.rb

ENV['EMAIL_USER_ID'] = 'your_id@gmail.com'
ENV['EMAIL_PASSWORD'] = 'the_gmail_app_password'

NB: press Ctrl + Hto view hidden files on linux.


2 . You want the rails app to load .my_secret.rb file as soon as the server fires up. To do that, you will use the File and load methods inside the /config/environment.rb file in the rails project. Make sure to place this code just above the line: Rails.application.initialize!

# /config/environment.rb

# load my_secret.rb
app_credentials = File.join(Rails.root, 'config', '.my_secret.rb')
load(app_credentials) if File.exist?(app_credentials)

# Initialize the Rails application.
Rails.application.initialize!


3 . Ensure the .my_secret.rb file is not submitted to github whenever you commit your project to a github repository. To do that, open the .gitignore file and add the path to the file as shown below:

# .gitignore

/config/.my_secret.rb


With the these steps you are done setting up the environment variables for your database. Now if you open the rails console and enter this ENV[‘EMAIL_USER_ID’], it should output the content of that variable:

 

$ ENV['EMAIL_USER_ID']

$ your_id@gmail.com

$ ENV['EMAIL_PASSWORD'] 

$ the_gmail_app_password


Part C: Configure ActionMailer


1 . Configure ActionMailer for the development environment in config/environments/development.rb.

# config/environments/development.rb
# ...
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false
  
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com", 
    :port => 587,   
    :domain => "gmail.com",
    :tls => true,          
    :enable_starttls_auto => true,
    :authentication => :login,
    :user_name => ENV['EMAIL_USER_ID'],
    :password => ENV['EMAIL_PASSWORD']
  }

That’s all!


Thanks for reading, see you in the next one!