Ruby-on-rails 连接被拒绝 - “localhost”端口 25 rails 的 connect(2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31009419/
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
Connection refused - connect(2) for "localhost" port 25 rails
提问by TheLittleBibi
During my training, I'm working on a website and we use Ruby on Rails. We need to send mails to users so I created a mailer.
在我的培训期间,我在一个网站上工作,我们使用 Ruby on Rails。我们需要向用户发送邮件,所以我创建了一个邮件程序。
I have tried to put the smtp in both development.rband environment.rb
我试图将 smtp 放在两个development.rb和environment.rb
config.action_mailer.default_url_options = {host: '0.0.0.0:3000'}
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = 'smtp'
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
adress: $SMTP_SERVER,
port: $PORT,
from: $MAIL,
enable_starttls_auto: true
#authentication: 'login'
}
It tells me that the error comes from this method line 6
它告诉我错误来自此方法第 6 行
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
# Tell the UserMailer to send a welcome Email after save
UserMailer.welcome_email(@user).deliver_now
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.json { render :json => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
I have set the port to 587but i keep getting the error:
我已将端口设置为,587但我不断收到错误消息:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
Errno::ECONNREFUSED: 连接被拒绝 - “localhost”端口 25 的 connect(2)
It looks as if another file was overwriting my settings. I also saw that it might be related to my ssh key not being authorized by the server.
看起来好像另一个文件正在覆盖我的设置。我还看到这可能与我的 ssh 密钥未被服务器授权有关。
Do know what is wrong?
知道有什么问题吗?
Thanks in advance
提前致谢
回答by Topher Hunt
First of all, when developing on localhost, it's common to notactually send out mail, rather to treat that as a deployment detail and stick with the Rails default behavior which is to spit out the mail headers and contents into the console STDOUT (where you can verify that the text looks right). Is there a specific reason why you need to test sending messages in the dev environment?
首先,在 localhost 上开发时,通常不实际发送邮件,而是将其视为部署细节并坚持 Rails 默认行为,即将邮件标头和内容吐出到控制台 STDOUT(在那里您可以验证文本看起来是否正确)。是否有特定原因需要在开发环境中测试发送消息?
Secondly, you mentioned that you set the SMTP settings in both development.rb and environment.rb. You shouldn't need to set these settings twice; in general I'd use development.rbfor settings specific to the dev environment, and environment.rbonly for settings that will always apply to all environments (dev, tests, and on the live deployed server). So if you're setting the same settings in both development.rb and environment.rb, I'd start by removing one or the other; redundancy will only make your job harder down the road.
其次,您提到您在 development.rb 和 environment.rb 中都设置了 SMTP 设置。您不需要两次设置这些设置;一般来说,我将development.rb用于特定于开发环境environment.rb的设置,并且仅用于始终适用于所有环境(开发、测试和实时部署的服务器上)的设置。因此,如果您在 development.rb 和 environment.rb 中设置相同的设置,我会首先删除其中一个;裁员只会让你以后的工作更加艰难。
Finally, to troubleshoot this I'd start by asking Rails what its settings are rather than waiting for the mail delivery to fail. Try the following:
最后,为了解决这个问题,我首先询问 Rails 它的设置是什么,而不是等待邮件传递失败。请尝试以下操作:
- Start up
rails console - Enter
Rails.configuration.action_mailer.smtp_settingsand compare the resulting hash against your expectations. This hash should contain the port and domain settings that are used when sending out all mail (in the current environment), so if ActionMailer is trying the wrong port then I'd expect the port to be wrong here too.
- 启动
rails console - 输入
Rails.configuration.action_mailer.smtp_settings并将结果哈希与您的期望进行比较。这个散列应该包含发送所有邮件时使用的端口和域设置(在当前环境中),所以如果 ActionMailer 尝试错误的端口,那么我希望这里的端口也是错误的。
Where are you setting $SMTP_SERVER, $PORTand $MAIL? Is there any reason you aren't using Rails' convention for environment variables, ENV['SMTP_SERVER']etc.?
你在哪里设置$SMTP_SERVER,$PORT和$MAIL?您是否有任何理由不使用 Rails 的环境变量约定ENV['SMTP_SERVER']等?
Hope some of that helps. Good luck!
希望其中一些有所帮助。祝你好运!
回答by Montells
replace
代替
config.action_mailer.delivery_method = 'smtp'
with
和
config.action_mailer.delivery_method = :smtp
Ensure your Rails.configuration.action_mailer.smtp_settings is symbolized keys
确保您的 Rails.configuration.action_mailer.smtp_settings 是符号键
回答by Kimprap
You need to remove config.action_mailer.perform_deliveries = trueline.
您需要删除config.action_mailer.perform_deliveries = true行。
回答by The Whiz of Oz
The app might be using mailcatchergem for all outbound emails on development, which you haven't installed or don't have running. At least that was my issue. Check out https://mailcatcher.meand follow the instructions given.
该应用程序可能正在将mailcatchergem 用于开发中的所有出站电子邮件,这些电子邮件您尚未安装或未运行。至少那是我的问题。查看https://mailcatcher.me并按照给出的说明进行操作。
回答by mrmicrowaveoven
I was running into this issue when running Sidekiq::Worker.drain_allin my RSpec tests, and it was driving me crazy because I had config.action_mailer.delivery_method = :testin my config/environments/test.rb.
我Sidekiq::Worker.drain_all在 RSpec 测试中运行时遇到了这个问题,这让我发疯,因为config.action_mailer.delivery_method = :test我的config/environments/test.rb.
The solution was to set config.action_mailer.delivery_method = :testin my config/environments/development.rb, which is confusing because the implication is that my config/environments/development.rbis overriding my config/environments/test.rbin my RSpec tests.
解决方案是config.action_mailer.delivery_method = :test在 my 中设置config/environments/development.rb,这令人困惑,因为这意味着config/environments/development.rbmyconfig/environments/test.rb在我的 RSpec 测试中覆盖了 my 。
Regardless, this might fix the problem for others.
无论如何,这可能会解决其他人的问题。

