【问题标题】:Sending mail with devise and Gmail smtp server使用设计和 Gmail smtp 服务器发送邮件
【发布时间】:2011-05-13 01:37:33
【问题描述】:

我正在使用 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 => 'jatinkumar.nitk@gmail.com',
  :password => '_secret_password'
} 

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

SocketError (getaddrinfo: Name or service not known):

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

Errno::ECONNREFUSED Connection refused - connect(2)

我不知道这个 :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

在我的 /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

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

SocketError (getaddrinfo: Name or service not known):

现在错误是:

Errno::ECONNREFUSED Connection refused - connect(2)

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

【问题讨论】:

    标签: ruby-on-rails smtp localhost devise actionmailer


    【解决方案1】:

    如果您对此仍有疑问,请尝试使用以下设置:

    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'
    }
    

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

    【讨论】:

    • 谢谢伙计...它确实有效。如果我有自己的域和该域的用户名,您能否也记下设置?
    • 只需将 domain 更改为您的 domain 并将 user_name 更改为 jatinkumar.nitk@yourdomain.com... 其他一切都应该相同。
    • 使用此设置,它会从 jatinkumar.nitk@gmail.com 向任何邮寄地址发送电子邮件吗?
    • 显然,:domainoptional,尽管您不会从阅读 docs 中知道这一点。
    • 经过数小时调试神秘的OpenSSL::SSL::SSLError error,这是最终为我完成的唯一解决方案!
    【解决方案2】:

    【讨论】:

    • @Rio Tera:我是所有这些东西的新手,所以无法理解你在引导我做什么。你能详细说明一下吗
    • :domain 可能应该是你的域 'example.com'
    • @Rio tera:我还没有自己的域名,Web 开发的新手。那我该怎么办??拥有自己的域是这样做的先决条件吗?如果是的话,我会尽快购买一个
    • 请尝试 :domain => "gmail.com", :username => "jatinkumar.nitk", :authentication => :plain
    • @Rio tera :尝试了“gmail.com”和你说的其他东西......没用
    猜你喜欢
    • 2016-09-07
    • 2012-06-08
    • 2011-10-25
    • 2011-03-17
    • 2011-10-21
    • 2022-01-09
    • 2013-02-15
    • 2014-10-15
    相关资源
    最近更新 更多