51

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 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 => "mygmailusername@gmail.com",
    :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 => "gmailuser@gmail.com",
    :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!

Community
  • 1
  • 1
ResrieC
  • 583
  • 1
  • 5
  • 6
  • 1
    Did you check your spam folder in gmail account? – Ivan Misic Sep 16 '14 at 15:16
  • 1
    check your gmail account's sent folder first – fengd Sep 16 '14 at 15:23
  • Hey, yes I checked my sent folder and spam folders on both the sending email and receiving emails (also a gmail account) – ResrieC Sep 16 '14 at 16:53
  • 1
    Is it raising a delivery error when you run it? If so, you may need to configure the Gmail account to allow access for less secure apps https://support.google.com/accounts/answer/6010255 – Chris Beck Mar 20 '15 at 15:14
  • Did you ever get this working? I'm having the same issue and have tried the solutions offered here. – Agazoom Dec 10 '15 at 23:48

5 Answers5

59

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:            'xyz@gmail.com',
  password:             'yourpassword',
  authentication:       :plain,
  enable_starttls_auto: true
}
TarunJadhwani
  • 1,151
  • 7
  • 21
  • 2
    Hey, I gave this a shot, tried it with the port 587 in quotes and no quotes, and neither is working. – ResrieC Sep 16 '14 at 16:54
  • 13
    Change the value of `:authentication` key from `:plain` to `:login`. It worked for me like that, using rails 4. – Throoze May 15 '15 at 06:05
  • domain obviously defines the domain-Name that you can use inside or your Mail to link back to the sending client! This Answer should be corrected or deleted! – bastianwegge Feb 06 '17 at 08:40
  • 1
    P.S. You should put this in your development.rb, production.rb or test.rb file in /environments folder. – msdundar May 19 '17 at 14:42
  • 2
    Go to https://myaccount.google.com/lesssecureapps and activate allow less secure apps: ON – Juan Ricardo Sep 28 '18 at 22:12
44

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.

shanet
  • 7,246
  • 3
  • 34
  • 46
Kim Miller
  • 886
  • 8
  • 11
  • Thanks for this suggestion. Google enabled this on my account and emails immediately stopped flowing. This solved the problem. – CodeSmith Feb 15 '16 at 20:48
  • I have enabled less secure passwords and done the https://accounts.google.com/b/0/DisplayUnlockCaptcha option as well. My production app is still rejecting the credentials. I switched from Linode to Heroku and this problem arose when the switch was made. Any ideas as to why it would still occur after enabling less secure apps and doing the DisplayUnlockCaptcha as well? – Matt Jul 07 '17 at 05:08
7

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
Gabriel Sigouin
  • 251
  • 4
  • 5
7

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: "blabla@gmail.com",
    password: "blabla", 
    domain: "smtp.gmail.com",
    openssl_verify_mode: "none",
  }
truongnm
  • 2,311
  • 2
  • 31
  • 48
  • For me it just worked with 2FA activated and a specific App Password. Otherwise I always got an `Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted...)` exception. I also had to enable insecure apps. – Marco Roth Nov 19 '19 at 19:34
  • documentation now states something slightly different `https://support.google.com/a/answer/2956491?hl=en?hl=en` indicates to point to **smtp-relay.gmail.com** on port 587 – Jerome Dec 22 '20 at 12:55
  • Could you please explain what user_name and password should go there? I'm reading the docs but it does not specify whose and what app's account goes into those parameters. – Dan Sep 03 '22 at 18:21
2

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.

DOK
  • 49
  • 1
  • 3
    The author is talking about sending emails via Gmail’s SMTP server, you know? This has nothing to do with OAuth. – idmean Nov 13 '15 at 14:32
  • 2
    And still the answer is relevant. To be able to use SMTP you need to configure to enable "less secure clients". The alternative (from googles standpoint) is to not use SMTP at all. The main intent of the author seems to be to get email delivery working. SMTP is just want *seems* to be neccessary. – NobodysNightmare Feb 06 '17 at 08:56