Ruby-on-rails Rails 4、如何正确配置smtp设置(gmail)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25872389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:55:12  来源:igfitidea点击:

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

ruby-on-railsemailruby-on-rails-4smtp

提问by ResrieC

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)

我正在尝试在 Rails 4 中创建一个联系表单。我在这里进行了一些挖掘,并且能够使大部分内容正常工作。(遵循@sethfri 在此处的工作,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:

现在我可以填写我的表单并点击发送。在我的 rails 服务器中,它说邮件已出站到我的电子邮件地址,但我的 gmail 邮箱中没有收到任何内容,所以我认为我的 smtp 设置不正确。我的 smtp 设置是:

...config/environments/development.rb

...配置/环境/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

我还添加了 .../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.

我错过了什么/做错了什么?我尝试了几件事(将 default_url 更改为端口 1025,将 :port => "587" 更改为 :port => 587)但没有成功。

Thanks for the help!

谢谢您的帮助!

回答by TarunJadhwani

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

您必须正确设置域。目前在代码中发布了它的“mydomain.net”。如果您想通过 gmail 发送,请将其更改为gmail.com

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
}

回答by Kim Miller

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

如果您在使用 Gmail 发送电子邮件(常见于 Google Apps 帐户)时遇到 Net::SMTPAuthenticationError 等错误,请访问您的Gmail 设置并启用安全性较低的应用程序以使应用程序正常工作。

回答by Gabriel Sigouin

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

经过几个小时的搜索,如何让它对我来说有效,我找到了一种让它工作的方法。对于我自己,我需要进行两步验证并使用 Gmail 应用程序密码

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 Verificationpage. 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.
  1. 转至两步验证页面。您可能需要登录您的 Google 帐户。
  2. 在右侧的“两步验证”框中,选择开始设置。
  3. 按照分步设置过程进行操作。

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

应用密码是一个 16 位密码,可让应用或设备访问您的 Google 帐户。如果您使用两步验证并在尝试访问您的 Google 帐户时看到“密码不正确”错误,应用密码可能会解决问题。大多数情况下,您只需为每个应用程序或设备输入一次应用程序密码,因此不必担心记住它

How to generate an app password

如何生成应用密码

  1. Visit your App passwordspage. 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
  1. 访问您的应用密码页面。系统可能会要求您登录自己的 Google 帐户。
  2. 在底部,单击选择应用程序并选择您正在使用的应用程序。
  3. 单击选择设备并选择您正在使用的设备。
  4. 选择生成。
  5. 按照说明在您的设备上输入应用程序密码(黄色栏中的 16 个字符代码)。
  6. 选择完成

回答by truongnm

2019, Rails 5 updated answer:

2019 年,Rails 5 更新了答案:

  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",
  }

回答by DOK

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

Google 建议在登录过程中使用 OAuth 2.0。这种配置对谷歌来说“不太安全”,但他们可以容忍。您必须在 Google 帐户设置中允许“安全性较低的连接”或使用 OAuth 方式。 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.

他们的 ruby​​ 库仍然是 alpha 版本。似乎有一些 gems 为 OAuth 扩展了 ActionMailer,但我从未使用过它们。