Rails邮件程序连接被拒绝 - localhost

时间:2015-06-01 12:21:40

标签: ruby-on-rails actionmailer

ruby​​版本2.2.0
Rails 4.1.4
actionmailer- 4.1.4
邮件 - 2.5.4

在rails c(开发)中输入“Mailer.method.deliver”时,我遇到了错误日志

for (auto fn : vector)
    fn();

当然,我将mailer配置(如下所示)添加到development.rb

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:541:in `initialize'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:541:in `open'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:541:in `tcp_socket'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:551:in `block in do_start'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/timeout.rb:99:in `call'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/timeout.rb:99:in `timeout'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:550:in `do_start'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/2.2.0/net/smtp.rb:520:in `start'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/mail-2.5.4/lib/mail/message.rb:2129:in `do_delivery'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/mail-2.5.4/lib/mail/message.rb:232:in `block in deliver'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionmailer-4.1.4/lib/action_mailer/base.rb:527:in `block in deliver_mail'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb:159:in `block in instrument'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.1.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb:159:in `instrument'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionmailer-4.1.4/lib/action_mailer/base.rb:525:in `deliver_mail'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/mail-2.5.4/lib/mail/message.rb:232:in `deliver'
from (irb):4
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'

我调试后......

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { 
  :address => "smtp.gmail.com",
  :domain => "gmail.com",
  :port => 587,
  :authentication => :plain,
  :user_name => "email",
  :password => "password",
  :enable_starttls_auto => true
}

我可以看到设置哈希有环境/ development.rb添加的默认变量(:symbol)和自定义变量(:string)。

# mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb 
# before line: 112  

raise settings.to_s  #added
smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp_obj|
...
...

2 个答案:

答案 0 :(得分:1)

对不起。 我忘记了一条让你知道的信息。

我在stackoverflow回答中看到了关于邮件程序配置的以下设置。

# config/initializer/setup_mail.rb

if Rails.env != 'test'
  email_settings = YAML::load(File.open("#{Rails.root}/config/smtp.yml"))
  ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil? 
end

# I say this [A] code

[A]代码导致此问题描述上方的错误。
(在第112行引出符号和字符串之前,添加&#34;将settings.to_s&#34;提升到mail-2.5.4 / lib / mail / network / delivery_methods / smtp.rb。)

我认为[A]代码发生此错误的原因是smtp_settings(哈希)合并!在类SMTP中初始化方法(mail-2.5.4 / lib / mail / network / delivery_methods / smtp.rb)。

class SMTP
  include Mail::CheckDeliveryParams

  def initialize(values)
    self.settings = { :address              => "localhost",
                      :port                 => 25, 
                      :domain               => 'localhost.localdomain',
                      :user_name            => nil,
                      :password             => nil,
                      :authentication       => nil,
                      :enable_starttls_auto => true,
                      :openssl_verify_mode  => nil,
                      :ssl                  => nil,
                      :tls                  => nil 
                    }.merge!(values)
  end
  ...

测试ruby哈希合并方法

default_setting = {:address => "localhost", :port => 25}
custom_setting = {:address => "smtp.gmail.com", :port => 587} # or {"address" => "smtp.gmail.com", "port" => 587}
default_setting.merge(custom_setting) # result in default_setting = {:address => "localhost", :port => 25, "address" => "smtp.gmail.com", "port" => 587}

所以我决定用ENV [&#34; SMTP_EMAIL&#34;],ENV [&#34; SMTP_PASSWORD&#34;]

将smtp_setting添加到application.rb

度过美好的一天!

答案 1 :(得分:-1)

我们有时会使用Google地址进行申请或测试。您可能需要登录Google帐户并确保已允许外部应用程序。这将是帐户安全。