I'm trying to add e-mail delivery with Gmail SMTP to my app. I've already done the "less secure apps" way before but I don't want to use this option in this project.
I've tried to look into Google's documentation or some gem to make it work, but to no avail. Everyone just sends some code (like below, which is usually the same I have) or tells me to try 'less secure app'.
My current action mailer configuration on production.rb is:
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DOMAIN_NAME'] }
config.action_mailer.asset_host = ENV['DOMAIN_NAME']
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => ENV['USERNAME'],
:password => ENV['PASSWORD'],
:enable_starttls_auto => true
}
Some people say I'd need ":domain => 'gmail.com'" but with the 'less secure app' option it works, so my guess is that the problem is not that simple. Also, people talk about changing 'authentication: :plain' to :login.
Also, I realize that in the official Rails documentation it says:
Note: As of July 15, 2014, Google increased its security measures and now blocks attempts from apps it deems less secure. You can change your gmail settings here to allow the attempts. If your Gmail account has 2-factor authentication enabled, then you will need to set an app password and use that instead of your regular password. Alternatively, you can use another ESP to send email by replacing 'smtp.gmail.com' above with the address of your provider.
(From http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail)
But I'm not sure if this solution still requires enabling the 'less secure app' option, which is not what I need.
Has anyone solved this problem without resorting to 'less secure app'?
Thanks in advance!
Okay, so after some time I finally did it.
What I had to do is:
1) Make a 2-step verification on my gmail account, which you can enable here: https://myaccount.google.com/security
2) Create an app-specific password here: https://support.google.com/accounts/answer/185833
It is a string with a format of 16 small case letters. They appear separated in groups of 4 but it's all in the same string. All you have to do is add this app password in the password field inside the block.
...
:user_name => 'my#gmail.com',
:password => 'abcdefghijklmnop',
...
All other settings worked without change.
config.read_encrypted_secrets = true
config.action_mailer.default_url_options = { :host =>
"domain.com" }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "username",
:password => "password",
:enable_starttls_auto => true
}
Try this config.
Related
I used G-suite email setting for sending emails from Rails application. This is my client G-suite email. Generally I used my gmail settings and it's work but G-suite email settings not work. Below code:
config.action_mailer.smtp_settings = {
:address => "smtp-relay.gmail.com",
:port => 587,
:user_name => "info#XXXX.com",
:password => "XXXXXX",
:authentication => "plain",
:domain => "gmail.com",
:enable_starttls_auto => true
}
I used address: "smtp.gmail.com" as well but not working.
I got error: SMTP: authentication failed and sometimes nothing but not received email.
Anyone have idea or same experience solutions.
Thanks
there is this configuration in gmail that COULD be the cause of the problem. I am not sure it would result with an error: SMTP: authentication failed
this is the guide to allow the unsafe apps
Change your settings to allow less secure apps into your account.
We don't recommend this option because it can make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:
Go to the "Less secure apps" section of my Account.
Turn on Allow less secure apps. (Note: If your administrator has locked less secure app account access, this setting is hidden.)
also I found similar post for this problem, for example a similar issue was solved in this so question
These settings worked for me:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'info#example.com', # Change this to the email you're sending from
:password => 'your_password_goes_here', # Change this to your password
:authentication => "plain",
:enable_starttls_auto => true
}
One extra tip. By default: config.action_mailer.raise_delivery_errors = false set to true so you can see any error messages in your browser/console
NOTE: in order for the mailer to work with any google email account you must have 'Allow less secure apps' turned on.
To do this, first go to the G-Suite admin console and turn on Security -> Advanced Security Settings and check the radio button: "Allow users to manage their access to less secure apps"
Now login to your G-Suite user account, go to My Account -> Sign-in & security. At the bottom you'll see Allow less secure apps. Turn that on.
You're good to go!
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!
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
}
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.
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
Go to the 2-Step Verification page. You might have to sign in to your Google Account.
In the "2-Step Verification" box on the right, select Start setup.
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
Visit your App passwords page. You may be asked to sign in to your Google Account.
At the bottom, click Select app and choose the app you’re using.
Click Select device and choose the device you’re using.
Select Generate.
Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
Select Done
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",
}
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.
I am sending email from my Rails application. It works well on development environment, but fails on staging. I get the following error:
Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/ContinueSignIn?plt=AKgnsbtdF0yjrQccTO2D_6)
Note, that my I don't have a domain name for my staging.
Here are my settings in staging.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here:80" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80'
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => 'login'
}
Please, help.
Edit.
After adding :tls => true option I get
OpenSSL::SSL::SSLError (Unrecognized SSL message, plaintext connection?)
And then I changed port to 25 and now I get this (with 30 seconds delay):
Timeout::Error (execution expired)
I had the same problem: emails were sent from development, but not from production (where I was getting Net::SMTPAuthenticationError).
This drove me to conclusion that the problem was not with my app's configuration, but with Google.
Reason: Google was blocking access from unknown location (app in production)
Solution: Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps).
After this my app in production started sending emails ;)
Solved!
I simply changed password for my gmail account and somehow errors disappeared.
After dozen of changes, the final settings I ended up with are:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80',
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => :plain,
:enable_starttls_auto => true
}
This solution is working for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'localhost:3000',
:user_name => "xyz#gmail.com",
:password => "password",
:authentication => :plain,
:enable_starttls_auto => true
}
It's true that Google will block your sign-in attempt but
You can change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
go to following link and turn on
https://www.google.com/settings/security/lesssecureapps
The above solution provided the correct settings (which I already had) but did not solve the issue. After continued attempts, I kept getting the same error. Turns out, I had to "clear the CAPTCHA" from the web. See the gmail documentation for details.
You can also jump right to the "clear the CAPTCHA" page here.
I had the same problem.
Solution:
You can turn on less secure apps option (at here).
https://myaccount.google.com/lesssecureapps
And unlock Captcha (link):
https://accounts.google.com/DisplayUnlockCaptcha
A lot later but just in case it helps anyone.. Just called Google Apps Help Center and they instructed to change the lesssecureapps setting (as everyone else) but also to change the port to 465.
In my case, that did the trick!
To resolve this issue:
If you see: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.), then you need to allow less secure apps to login your google account.
To enable less secure apps login follow: https://myaccount.google.com/lesssecureapps?. But will allow all apps to login.
If you want to customize it refer : https://support.google.com/a/answer/6260879?hl=en
Then may be possible you will get Net::SMTPAuthenticationError (534-5.7.14), so to resolve this refer: pli=1http://www.google.com/accounts/DisplayUnlockCaptcha. After that click the continue from the page you get redirected. It will verify your Captcha and your app will get verified to use your google account to send emails.
NOTE: Please make sure you'r using correct credentials of your gmail account.
If you'r not willing to allow all the apps please refer: https://support.google.com/a/answer/6260879?hl=en. From the link go to Use alternatives to less secure apps, this will guide you to an alternative way to Allow Less Secure apps access to your google account.
The accepted answer seems very old, I don't know if at that time the followin (better) solution was existing:
Go to https://myaccount.google.com/u/0/apppasswords
Generate a new password for you app
Replace the personal password with the generated password in config.action_mailer.smtp_settings
Now, sending emails works perfectly!
Hello this also worked for me and so if someone is still having a problem try this out.
Make sure you have figaro in your gemfile.
To save sensitive information such as username and password as environment variables
gem 'figaro'
And in your config/environments/development.rb , paste the codes below
using smtp as method delivery
config.action_mailer.delivery_method = :smtp
SMTP settings for gmail
config.action_mailer.smtp_settings =
{
:address=> "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password=> ENV['gmail_password'],
:authentication=> "plain",
:enable_starttls_auto=>true
}
config.action_mailer.default_url_options = { host: "locahost:3000" }
In your config directory create a file called application.yml
and add the codes below.
gmail_username: 'example#gmail.com'
gmail_password: "your_example_email_password_here"
You must use your email and password for authentication in the file.
I also faced the problem, and after some research in Gmail setting, I found the solution:
In gmail, go to settings.
Select "Forwarding and POP/IMAP" tab.
In the IMAP access section, select "Enable IMAP".
I had the same problem and after some trial and errors, have come to this resolution which is an option to be enabled in google:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable 'Access for less secure apps' here by logging in with the email address you have provided in smtp configuration.
I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!
I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.
I have tried a bunch of different ways but they generally amount to configuring the following settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb
I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.
Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have
config.action_mailer.raise_delivery_errors = true
set in my config/development.rb but the development log still just shows the same as I see in the terminal.
For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.
Many thanks
Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.
The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following
if Rails.env != 'test'
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end
I then added config/email.yml containing the details of the dev and production email accounts
development:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
production:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers
I have the following in config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
The actual mail-configuration, config.actionmailer.* i have placed in config\application.rb.
Hope this helps :)
Try using 'sendmail' instead of 'smtp'.
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
Three things.
First, the port is an integer and does not need quotes, as in your first example. (But I think a string should still work.)
Second, don't forget to restart your server each time you modify this (or any) initializer file. This could explain why you didn't see an error after adding:
config.action_mailer.raise_delivery_errors = true
Without having that error message, it's hard to determine why the mail wasn't going but now is. One possiblity is your use of double quotes around the password. If you were using a strong password and had a token in your password that wasn't escaped it could have been reinterpreted. (i.e. "P#ssw\0rd" would become P#ssrd). For just this reason, I always use single quotes in my code unless I specifically need the syntactic sugar.
Lastly, enable_starttls_auto: true is the default and unnecessary.
ActionMailer::Base.delivery_method = :sendmail
and
config.action_mailer.perform_deliveries = true
were the two necessary steps that got me over this issue
Just put all config to:
config/environments/development.rb
I mean
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
and
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
It worked for me.
In addition to, your gmail username does not alias.
Ref: https://support.google.com/mail/answer/12096?hl=en
My two pennies worth:
I had those exact same symptoms with Rails 5.1: Nothing happened, the settings in my development.rb file were utterly ignored...
Then I remembered to restart the machine! (which solved magically the issue)
This had been pointed out by a couple of previous comments.
The issue is tricky however because you do not expect this behavior. In my view, the default comments in development.rb are, in this respect, misleading:
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since *you don't have to restart the web server when you make code changes*.
I am attempting to add email capabilities to my app (forgotten password, notifications, etc.) and I am using EngineYard for hosting. I have successfully configured email in my test environment but upon uploading to EY it seems to error out in Production. I don't pay for their support and the only resource is a bit vague (or beyond me).
I am curious to know if there is any specific file additions, server set up etc. that is needed when using email on EY. I am using Google apps so I thought it would be as easy as adding the same code block for test in production but doesn't seem to be the case.
Here's my config for Google apps, in .../config/environments/production.rb:
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:domain => 'example.com',
:authentication => :plain,
:user_name => "sender#example.com",
:password => 'tr1ckypwd!'
}
Note, for the security minded out there, I actually keep the password in a separate file and have code to patch it into the settings on launch, but I figured that would distract from the meat of the response.
Hope that helps.