使用devise和Gmail smtp服务器发送邮件

Jat*_*tra 21 smtp ruby-on-rails localhost actionmailer devise

我正在使用Devise:确认和:可恢复模块来确认用户并让他在忘记密码时恢复密码.一切都很好,邮件生成,我可以在服务器日志中看到它,但然后我遇到错误,邮件没有传递到邮箱.我的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 => 'jatinkumar.nitk@gmail.com',
  :password => '_secret_password'
} 
Run Code Online (Sandbox Code Playgroud)

如果:地址是'smtp.gmail.com',那么我得到以下错误:

SocketError (getaddrinfo: Name or service not known):
Run Code Online (Sandbox Code Playgroud)

如果我将:address设置为'localhost',那么我会收到以下错误:

Errno::ECONNREFUSED Connection refused - connect(2)
Run Code Online (Sandbox Code Playgroud)

我不知道这是什么:地址意味着,所有这些东西的新手.在运行uname -a时,我得到了

Linux jatin-ubuntu 2.6.32-24-generic #38-Ubuntu SMP Mon Jul 5 09:22:14 UTC 2010 i686 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

在我的/ 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
Run Code Online (Sandbox Code Playgroud)

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

SocketError (getaddrinfo: Name or service not known):
Run Code Online (Sandbox Code Playgroud)

现在的错误是:

Errno::ECONNREFUSED Connection refused - connect(2)
Run Code Online (Sandbox Code Playgroud)

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

Bra*_*ker 24

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

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          => 'jatinkumar.nitk@gmail.com',
  :password           => '_secret_password'
}
Run Code Online (Sandbox Code Playgroud)

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