Appname::Application.configure do
config.action_mailer.delivery_method = :smtp
#typical smtp_settings for gmail account
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.of.sender.net",
:authentication => "plain"
:user_name => "spencecooley"
:password => "secret"
:enable_starttls_auto => true
}
end
I have two questions about configuring action mailer
Do you know what the :domain symbol is referring to? Is it talking about the domain name of the application? Is it talking about the mail server domain? I saw baci.lindsaar.net written in on a few sites that I googled, but I don't know why people are using that particular domain. List item
I also don't know what the :enable_starttls_auto => true is doing
update:
Ok, so I found this in the docs in reference to question 2
:enable_starttls_auto - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it
Didn't know what STARTTLS was, so I looked it up here http://en.wikipedia.org/wiki/STARTTLS
update:
I found this in the docs, but still don't understand
:domain - If you need to specify a HELO domain, you can do it here.
so I guess the new question is: what is a HELO domain? can't seem to find a clear answer.
The :domain key is set up for HELO checking. You don't need to specify this if you're using GMail.
The STARTTLS call starts an encrypted connection with your mail server, which is required to use GMail's SMTP.
Related
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.
Strange behaviour I have. I'm using gmail to send my emails in my Rails app. I have my gmail configured to accept less secure apps.
However I send one email and then when I go to my google account it prompts me to restore the account via my phone.
After that, my app no longer sends email and I get this in my Heroku logs:
Net::SMTPAuthenticationError (534-5.7.9 Please log in with your web browser and then try again.
My production.rb is set up this way:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'xxx#gmail.com',
:password => 'xxxxxx',
:authentication => "plain",
:enable_starttls_auto => true,
}
Anybody any ideas?
I think you have to set the domain to gmail.com
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "xxxxxxx",
:authentication => :plain,
:enable_starttls_auto => true
}
If it worked then ok, if not try to change the :authentication to :login.
Advice: Check Gmail's security events too and double-check that all security settings are intact.
So I had several problems. The one that was caused by my inattention was that I didn't read Figaro gem docs properly and I wasn't sending out the proper info to Heroku with figaro heroku:set -e production.
Secondly my account would go into lockdown after I created it and sent first email. Steps to do:
enable two-step verifiation
generate app specific keys
put them in your config/application.yml if you're using Figaro
push and run figaro command
you might have to clear CAPTCHA on gmail see here
Follow the coment by #Mohamed
Advice: Check Gmail's security events too and double-check that all security settings are intact.
LINK: https://www.google.com/settings/security/lesssecureapps
I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.
I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error
my environment.rb (i have sendgrid user/pwd in application.yml)
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
in my orders_controller within the order create method, I call the below.
AutoNotifier.orderconf_email(current_user, #order).deliver
auto_notifier.rb
class AutoNotifier < ActionMailer::Base
default from: "Test Email"
def orderconf_email(current_user, order)
#buyer = current_user
#order = order
mail(to: #buyer.email, subject: 'Thank you for your order.')
end
end
What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.
For posterity, here's a working setup for external SMTP in Rails on Heroku:
#config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
:authentication => 'login',
:domain => 'yourdomain.com', # your domain to identify your server when connecting
}
Change default from: "Test Email" to valid email address, even example#example.com.
I would just like to point out, this is for sending emails via SMTP. While this method is totally ok, you should also consider sending via the API.
To do this, you need to specify an interceptor. Luckily, there's a Gem that helps with that. Here's a good article showing how to use it.
https://rubyplus.com/articles/561-Sending-Emails-using-SendGrid-API-in-Rails-4-1
It took us a long time to resolve the issue when we tried to deploy the SMTP relay on heroku. It worked perfectly fine on local but when pushed we received socket errors and time out issues. Eventually got it working.
Important note: Make sure not to use starttls_auto and SSL/or TLS this causes open SSL issue.
I'm not sure if this belongs in server fault or here feel free to move it if it makes more sense somewhere else. I've seen the examples for setting up the smtp settings and using ActionMailer with Gmail and confirmed that they work for me.
Basically it looks like this for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
What I need to do now is to send an email to an address that isn't a plain Gmail account and yet, at it's core, is Gmail. My company uses whatever google's email service that allows you to use gmail but for the addresses to be listed as username#my.company.com rather than #gmail.com. I know for a fact that you can't simply log into our mail at the main gmail site so I assume our domain is different. Or something.
At the moment, when I simply use my own company user/pass I get an error message telling me that the user/pass was wrong. But I'm guessing the issue is that I'm trying to mail from the gmail variant of my username.
I have confirmed that our smtp server, as far as Thunderbird is concerned, is the normal gmail smtp, that our port is still 587, and that we are using TLS. What do I need to change here so that I can send an email to one of these addresses? Thanks.
I have my own domain setup at google for mail, the url to log into that directly is
http://mail.google.com/a/my.company.com
My rails app that sends mail through that account, has
:domain => "my.company.com"
as well as all the other fields you have.
"<user_name>" needs to be the whole email address, not just the user name.
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'busiess.com',
:user_name => 'username#company.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true
I have installed restful_authentcation from technoweenie with activation, and so I see the generated UserMailer < ActionMailer::Base.
However, the instructions don't include how to set it up to work with Google SMTP.
I am guessing that environments/development.rb needs to have the SMTP settings, but still not sure given Google (via Google apps) are all TLS.
Anyone set up activation using restful_authentication?
I currently put into environments.rb the following:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "xxx#mydomain.com",
:password => "mypassword"
}
Thanks!!
As far as I know, ActionMailer doesn't do TLS out of the box (2.3.2). A couple of months ago I had the same issue and found some code on a Japanese page and integrated that. it appears that code has been wrapped up into a plugin now (with english docs yeah!). That's not exactly what I'm using, but it advertises the same effect.
so add this plugin:
http://github.com/openrain/action_mailer_tls/tree/master
and in environments/development.rb or environements.rb you need something like this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "yourdomain.com",
:user_name => "first.last#gmail.com",
:password => "passwd",
:authentication => :plain
}
I see that :enable_starttls_auto => true is now in the docs, but it wasn't when I started. this at least works for me...
Edit: for some reason that link doesn't work if you follow it, but copy paste in the address bar and it's live...
I've never used SMTP from ruby (I have from python), but that looks right. You have the right domain and port (actually, multiple ports are supported, but that's one of them), and you're using starttls and AUTH PLAIN, which Google does use.