Action Mailer Configuration for Gmail

牧云@^-^@ 提交于 2019-12-06 10:02:59

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/48285870/action-mailer-configuration-for-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!