How to Configure Gmail App Password for Sending Emails in a Ruby on Rails Application

BharteeSahareTechRoR
3 min readSep 14, 2023

Sending emails from your Ruby on Rails application is a common task, and Gmail is a popular choice for SMTP (Simple Mail Transfer Protocol) services. However, to enhance security, Google requires you to use an “App Password” instead of your account password when accessing your Gmail account from third-party applications. In this guide, we will walk you through the steps to create and use an App Password for sending emails from your Rails application.

Step 1: Creating an App Password:

Log in to your Google Account:

Start by logging in to the Gmail account you want to use for sending emails from your Rails application.

Go to Security Settings:

Click on your profile picture in the upper-right corner and select “Manage your Google Account.

Access Security Settings:

In the left-hand menu, click on “Security.”

App Passwords:

Scroll down to the “Signing in to Google” section and click on “App passwords.”

Generate App Password:

Choose the app you want to generate the password for. If your app is not listed, select “Other (Custom name)” and enter a name for your app. Then click “Generate.”

App Password:

A 16-character App Password will be generated. Copy this password to your clipboard, as you will need it later. Keep this password secure, as it’s equivalent to your account password.

Step 2: Configuring Your Rails Application:

Now that you have your App Password, you can configure your Rails application to use it for sending emails via SMTP. Open your `production.rb` file and make the following changes:

config.action_mailer.default_url_options = { host: ENV[‘HOST’] } #here for local you can set localhost:3000 and for production you can set actual your host i.e website.com
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ‘smtp.gmail.com’,
port: 587,
domain: ‘website.com’, # Remove trailing ‘/’
user_name: ‘your_email@gmail.com’, # Replace with your Gmail email
password: ‘your_app_password’, # Replace with the App Password you generated
authentication: ‘plain’,
enable_starttls_auto: true,
open_timeout: 5,
read_timeout: 5
}

Make sure to replace `’your_email@gmail.com’` with your Gmail email and `’your_app_password’` with the App Password you generated in Step 1.

Output Production

In summary,

You have successfully set up an App Password for your Gmail account and configured your Rails application to use it for sending emails via SMTP. This enhances the security of your Gmail account while allowing your Rails app to send emails seamlessly.

Remember to keep your App Password secure, and if you ever suspect it has been compromised, you can easily revoke and regenerate it in your Google Account settings.

Happy emailing from your Rails application! 🚀 Please follow for more updates BharteeTechRoR

--

--

BharteeSahareTechRoR

Ruby on Rails Developer || React Js || Rspec || Node Js