Ruby-on-rails 使用设备和 Gmail smtp 服务器发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4232597/
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:40:28  来源:igfitidea点击:

Sending mail with devise and Gmail smtp server

ruby-on-railssmtplocalhostdeviseactionmailer

提问by Jatin Ganhotra

I am using Devise :confirmable and :recoverable module to confirm a user and to let him recover his password if he forgets it. Everything is going fine, the mail gets generated and I can see it in the server log, but then I am facing errors and the mail is not delivered to the mailbox. The SMTP settings for my environment.rb file is :

我正在使用 Devise :confirmable 和 :recoverable 模块来确认用户并让他在忘记密码时恢复他的密码。一切正常,邮件已生成,我可以在服务器日志中看到它,但随后我遇到了错误并且邮件未发送到邮箱。我的 environment.rb 文件的 SMTP 设置是:

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 = {
  :enable_starttls_auto => true,  #this is the important shit!
  :address => 'smtp.gmail.com', #'localhost', 
  :port => 587,
  :tls => true,
  :domain => 'mail.google.com',  # mail.customdomain.com if you use google apps
  :authentication => :login,
  :user_name => '[email protected]',
  :password => '_secret_password'
} 

If the :address is 'smtp.gmail.com' , then i get the following error:

如果 :address 是 'smtp.gmail.com' ,那么我会收到以下错误:

SocketError (getaddrinfo: Name or service not known):

If i set the :address to 'localhost', then i get the following error:

如果我将 :address 设置为“localhost”,则会出现以下错误:

Errno::ECONNREFUSED Connection refused - connect(2)

I don't know what this :address means, a newbie for all this stuff. On running uname -a, i get

我不知道这个 :address 是什么意思,所有这些东西的新手。在运行uname -a 时,我得到

Linux jatin-ubuntu 2.6.32-24-generic #38-Ubuntu SMP Mon Jul 5 09:22:14 UTC 2010 i686 GNU/Linux

In my /etc/hostsfile the entries are :

在我的/etc/hosts文件中,条目是:

127.0.0.1   localhost
127.0.1.1   jatin-ubuntu

*#74.125.93.109   smtp.gmail.com 
#The above entry added by me*

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

When I uncomment the 'smtp.gmail.com' address in the /etc/hosts file, the following error is gone :

当我取消注释 /etc/hosts 文件中的“smtp.gmail.com”地址时,以下错误消失了:

SocketError (getaddrinfo: Name or service not known):

and now the error is :

现在错误是:

Errno::ECONNREFUSED Connection refused - connect(2)

I don't know what is going wrong, googled for the errors and tried everything but nothing came to rescue. I do have the 'tlsmail'gem installed and the 'mail'gem as well, and my application is in development mode. Help me fix this error so that i can happily continue my rails journey and if possible guide me a little over this :address issue in the right direction so that i understand the basics of this. Thanks in advance

我不知道出了什么问题,用谷歌搜索错误并尝试了一切,但没有任何帮助。我确实安装了'tlsmail'gem 和'mail'gem,并且我的应用程序处于开发模式。帮助我修复这个错误,这样我就可以愉快地继续我的 Rails 之旅,如果可能的话,指导我解决这个问题:以正确的方向解决问题,以便我了解这个问题的基础知识。提前致谢

回答by Braden Becker

If you're still having problems with this try using these settings:

如果您仍然遇到此问题,请尝试使用以下设置:

require 'tlsmail'    
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,  
  :address            => 'smtp.gmail.com',
  :port               => 587,
  :tls                  => true,
  :domain             => 'gmail.com', #you can also use google.com
  :authentication     => :plain,
  :user_name          => '[email protected]',
  :password           => '_secret_password'
}

Additionally I would recommend putting these settings in your config/environments/development.rb file instead of environment.rb so that you can specify different mailservers for each environment.

此外,我建议将这些设置放在 config/environments/development.rb 文件中而不是 environment.rb 中,以便您可以为每个环境指定不同的邮件服务器。