Ruby-on-rails Errno::ECONNREFUSED: 连接被拒绝 - connect(2) for action mailer

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

Errno::ECONNREFUSED: Connection refused - connect(2) for action mailer

ruby-on-railsrubyruby-on-rails-3actionmailer

提问by VenkatK

I have been working with rails since a long. Now I am facing a small issue in the ActionMailer. I want to send an email when user gets registered to confirm his registration. I am able to send email in the development modebut where as notin the production mode.
the exception Errno::ECONNREFUSED: Connection refused - connect(2)is coming everytime when delivermethod is called.
I have written the following code.
My SMTP config looks:
config.action_mailer.default_url_options = { :host => "localhost:3000" }

很长一段时间以来,我一直在使用 Rails。现在我在 ActionMailer 中遇到了一个小问题。我想在用户注册时发送电子邮件以确认他的注册。我能够发送电子邮件中的发展模式,但如果作为没有生产模式。
异常Errno::ECONNREFUSED:连接被拒绝 -每次调用传递方法时都会出现connect(2)
我已经编写了以下代码。
我的 SMTP 配置看起来:
config.action_mailer.default_url_options = { :host => "localhost:3000" }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.smtp_settings = {   
    :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,      
    :ssl => true,
    :enable_starttls_auto => true,  #this is the important stuff!
    :address        => 'smtp.xxxx.xxx',
    :port           => xxx,
    :domain         => 'xxxxxx',
    :authentication => :plain,
    :user_name      => '[email protected]',
    :password       => 'xxxxxxxxx'
  }

In the controller, I have written the following:

在控制器中,我写了以下内容:

def confirm_registration_in_c       
 @user = User.find_by_email([email protected])
 if @user
      UserMailer.confirm_registration(@user).deliver            
 end
end

In my user_mailer.rb :

在我的 user_mailer.rb 中:

class UserMailer < ActionMailer::Base
  default from: "[email protected]"

  def confirm_registration(user)
   @user = user
   @user_name = @user.name       
   email = @user.email 
   mail(:to => email, :subject => "Reset your password")
  end
end

I am able to send email in the development mode in my local host, but I am not able to send the email in the dedicated server.
Can anybody help me please?

我可以在本地主机中以开发模式发送电子邮件,但无法在专用服务器中发送电子邮件。
有人可以帮我吗?

回答by oppih

In my situation, I encountered similar problems when I was trying to making through a sending-email Rails app tutorial, the Heroku logs kept telling my that

在我的情况下,当我尝试通过发送电子邮件的 Rails 应用教程进行制作时遇到了类似的问题,Heroku 日志一直告诉我

......

......

Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):

Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):

......

......

After I compared my code with author's code, I got to find out that I had not setup my ActionMailer configurations in the config/environments/production.rbfile.

在将我的代码与作者的代码进行比较后,我发现我没有在config/environments/production.rb文件中设置我的 ActionMailer 配置。

Then I came to realized that I just had my config/environments/development.rbconfigured for sending-email, but I had not done it for my config/environments/production.rb.

然后我意识到我只是为发送电子邮件配置了我的config/environments/development.rb,但我没有为我的config/environments/production.rb 配置

So you may check it when your app's behavior difers between development and production.

因此,当您的应用程序的行为在开发和生产之间有所不同时,您可以检查它。

回答by Sorry-Im-a-N00b

Be sure you have configured your port correctly. I switched from gmail in development (port 587) to sending from my local server in production and was getting this error till I corrected the port to the one my server uses (port 25).

确保您已正确配置您的端口。我从开发中的 gmail(端口 587)切换到生产中从我的本地服务器发送并收到此错误,直到我将端口更正为我的服务器使用的端口(端口 25)。

回答by Pandurang Waghulde

for production you cant write

对于生产,你不能写

config.action_mailer.default_url_options = { :host => "localhost:3000" }

add production url for host, like,

为主机添加生产网址,例如,

config.action_mailer.default_url_options = { :host => "http://www.yourdomain.com" }

回答by lulalala

My problem is not identical to this question, but I feel many would found this thread via google.

我的问题与这个问题不同,但我觉得很多人会通过谷歌找到这个线程。

If you use external SMTP service like sendgrid and has set up ActionMailer accordingly, yet it still gives this error:

如果您使用像 sendgrid 这样的外部 SMTP 服务并相应地设置了 ActionMailer,但它仍然会出现此错误:

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25

Errno::ECONNREFUSED: 连接被拒绝 - “localhost”端口 25 的 connect(2)

You may be passing config hash with String key, which are ignored. Keys must be symbols!

您可能正在使用 String 键传递配置哈希,这些键被忽略。键必须是符号

This may happen if it is de-serialized, what I did is to ensure keys are symbols:

如果反序列化可能会发生这种情况,我所做的是确保键是符号:

config.action_mailer.smtp_settings = get_smtp_setting.symbolize_keys

回答by Taika

There is another reason for this error:

此错误还有另一个原因:

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25

It should be looked at SENDMAIL service on your server:

应该查看您服务器上的 SENDMAIL 服务:

  • Is SENDMAIL installed?
  • Is SENDMAIL running?
  • 是否安装了 SENDMAIL?
  • SENDMAIL 正在运行吗?

I had this error due to the stopped SENDMAIL.

由于停止了 SENDMAIL,我遇到了这个错误。

Good luck!

祝你好运!

回答by Ken Prince

I just tracked down a similar problem while trying to deploy wordpress with Capistrano.

我刚刚在尝试使用 Capistrano 部署 wordpress 时发现了一个类似的问题。

cap aborted! Errno::ECONNREFUSED: Connection refused - connect(2) for "{my-ip-address}" port {my-ssh-port}

cap aborted! Errno::ECONNREFUSED: Connection refused - connect(2) for "{my-ip-address}" port {my-ssh-port}

I would also get this similar error:

我也会得到这个类似的错误:

Tasks: TOP => git:create_release (See full trace by running task with --trace) The deploy has failed with an error: #<Errno::ECONNREFUSED: Connection refused - connect(2) for "my-ip-address" port {my-port}>

Tasks: TOP => git:create_release (See full trace by running task with --trace) The deploy has failed with an error: #<Errno::ECONNREFUSED: Connection refused - connect(2) for "my-ip-address" port {my-port}>

It turns out it was an issue with concurrent SSH sessions as my server runs Fail2Ban. To solve that I simply did the following:

事实证明这是并发 SSH 会话的问题,因为我的服务器运行 Fail2Ban。为了解决这个问题,我只是做了以下事情:

  1. Edit the jail that contains SSH configurations

    $ sudo nano /etc/fail2ban/jail.local

  2. look for [SSH] and set enabled = false

  3. then find [ssh-ddos] and set enabled = false

  4. Remember to restart Fail2Ban after your changes and open-ssh (if thats what your using)

  1. 编辑包含 SSH 配置的 jail

    $ sudo nano /etc/fail2ban/jail.local

  2. 寻找 [SSH] 并设置 enabled = false

  3. 然后找到 [ssh-ddos] 并设置 enabled = false

  4. 请记住在更改后重新启动 Fail2Ban 并打开 ssh(如果那是您使用的)

$ sudo service fail2ban reload

$ sudo service fail2ban reload

$ sudo /etc/init.d/ssh reload

$ sudo /etc/init.d/ssh reload

Its worth noting that the connection would be refused at different steps (tasks) in the deployment. For example after a reboot and a quick bundle exec cap production deploy:checkeverything appeared fine. Then I tried to deploy and received the same error, but during the execution of a different task. I also use UFW which I disabled and reenabled without issues. UFW was not the cause of the above problem.

值得注意的是,连接将在部署的不同步骤(任务)中被拒绝。例如,在重新启动和快速后bundle exec cap production deploy:check一切看起来都很好。然后我尝试部署并收到相同的错误,但在执行不同的任务期间。我也使用 UFW,我禁用并重新启用它没有问题。UFW 不是上述问题的原因。

I had a similar problem after I solved this. It was an issue with the currentdirectory permissions. That's over here.

解决这个问题后,我遇到了类似的问题。这是current目录权限的问题。就到这里了。