Rails 4, how to correctly configure smtp settings (gmail)

Ruby on-RailsEmailRuby on-Rails-4Smtp

Ruby on-Rails Problem Overview


I am trying to create a contact form in Rails 4. I did some digging around here and was able to get most of the stuff to work. (followed @sethfri's work here https://stackoverflow.com/questions/20769772/contact-form-mailer-in-rails-4)

Right now I am able to fill out my form box and hit send. In my rails server it says the mail was outbound to my email address, but I don't receive anything in my gmail box, so I think my smtp settings aren't right. My smtp settings are:

...config/environments/development.rb

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "mydomain.net",
    :user_name => "[email protected]",
    :password => "myGmailPassword",
    :authentication => "plain",
    :enable_starttls_auto => true
  } 

Also I added in .../config/initializers/smtp_settings.rb

ActionMailer::Base.smtp_settings = {
	:address => "smtp.gmail.com",
	:port => "587",
	:domain => "mydomain.net",
	:user_name => "[email protected]",
	:password => "gmailPassword",
	:authentication => "plain",
	:enable_starttls_auto => true
}

What am I missing/doing wrong? I've played around with a couple things (changed default_url to port 1025, changed :port => "587" to :port => 587) with no success.

Thanks for the help!

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You have to set the domain correctly. Currently in the code posted its "mydomain.net". Change it to gmail.com if you want to sent it via gmail.

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            '[email protected]',
  password:             'yourpassword',
  authentication:       :plain,
  enable_starttls_auto: true
}

Solution 2 - Ruby on-Rails

If you run into errors like Net::SMTPAuthenticationError while using gmail for sending out emails (common for Google Apps accounts), visit your gmail settings and enable less secure apps to get the application working.

Solution 3 - Ruby on-Rails

After few hours to search how to make this working for me, i find a way to make it work. For myself, i needed to make 2-Step Verification and use Gmail application password

> When you enable 2-Step Verification (also known as two-factor authentication), you add an extra layer of security to your account. You sign in with something you know (your password) and something you have (a code sent to your phone).

Set up 2-Step Verification

  1. Go to the 2-Step Verification page. You might have to sign in to your Google Account.
  2. In the "2-Step Verification" box on the right, select Start setup.
  3. Follow the step-by-step setup process.

>An App password is a 16-digit passcode that gives an app or device permission to access your Google Account. If you use 2-Step-Verification and are seeing a “password incorrect” error when trying to access your Google Account, an App password may solve the problem. Most of the time, you’ll only have to enter an App password once per app or device, so don’t worry about memorizing it

How to generate an app password

  1. Visit your App passwords page. You may be asked to sign in to your Google Account.
  2. At the bottom, click Select app and choose the app you’re using.
  3. Click Select device and choose the device you’re using.
  4. Select Generate.
  5. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
  6. Select Done

Solution 4 - Ruby on-Rails

2020, Rails 6 updated answer:

  config.action_mailer.perform_deliveries = true
  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,
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "[email protected]",
    password: "blabla", 
    domain: "smtp.gmail.com",
    openssl_verify_mode: "none",
  }

Solution 5 - Ruby on-Rails

Google recommends to use OAuth 2.0 for the login process. This configuration is "not so safe" for google, but they tolerate it. You have to allow "less safe connections" in your Google account settings or use the OAuth-way. https://developers.google.com/identity/protocols/OAuth2

Their library for ruby is still alpha. There seem to be some gems extending ActionMailer for OAuth, but I never used them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionResrieCView Question on Stackoverflow
Solution 1 - Ruby on-RailsTarunJadhwaniView Answer on Stackoverflow
Solution 2 - Ruby on-RailsKim MillerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsGabriel SigouinView Answer on Stackoverflow
Solution 4 - Ruby on-RailstruongnmView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDOKView Answer on Stackoverflow