Gmail Security Blocking Rails Emails

So as many people, I’ve setup my Rails app to send emails for various things. My particular use case was sending an email when a new user signed up. I used a free gmail account as the email address to send from.  However, I kept running into a problem with the gmail security settings blocking the email because it looked like a suspicious user, because the heroku server happened to be in Virginia, where I don’t live.

So, how to fix this? I searched and found almost nowhere where anyone had described how to fix this. Maybe I’m the only one who had this problem, but it seems like a very common thing.

First step to make sure that your app doesn’t crash if you have this type of email error is to set config.action_mailer.raise_delivery_errors = false in your config/environments/production.rb file. It can be helpful to set this to true in your development or testing environments, but in production you want these to fail silently so they don’t break your app.

Ok, now the trick to fix the security settings in Google. Counterintuitively, you need to turn ON 2-factor authentication. Go to https://security.google.com/settings/security/apppasswords and login with the account you want to send email from in your Rails app.  Scroll to “Signing in to Google”, and turn on 2-Step verification. You need that turned on so that you can then setup “App passwords”. Next click on App passwords and you get this screen.

Screen Shot 2017-08-16 at 7.19.15 AM

Click on Select App, then Custom, and give it a useful name, and click “Generate”. Now you get this popup with your new app password.

Screen Shot 2017-08-16 at 7.19.44 AM

Screen Shot 2017-08-16 at 7.20.14 AM

When this password is used, it will not block it based on location, etc.

Now add this password to your Rails app as the password it uses to login to gmail, rather than your usual password. I use the figaro gem for environment variables, so I updated my GMAIL_PASSWORD environment variable in my application.yml file.

That’s it! Hopefully it was useful!

helpful links:

http://usingname.space/2015/07/25/gmail-smtp-ruby-on-rails-actionmailer-and-you/

Advertisements

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

%d bloggers like this: