连接被拒绝 - 连接(2)"localhost"端口25轨道

The*_*ibi 12 ruby email ruby-on-rails

在我的培训期间,我正在一个网站上工作,我们使用Ruby on Rails.我们需要向用户发送邮件,因此我创建了一个邮件程序.

我试图把SMTP均development.rbenvironment.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'
}
Run Code Online (Sandbox Code Playgroud)

它告诉我错误来自这个方法第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
Run Code Online (Sandbox Code Playgroud)

我已将端口设置为587但我一直收到错误:

Errno :: ECONNREFUSED:连接被拒绝 - 连接(2)为"localhost"端口25

看起来好像另一个文件覆盖了我的设置.我还看到它可能与我的ssh密钥没有被服务器授权有关.

知道出了什么问题吗?

提前致谢

Top*_*unt 12

首先,在localhost上进行开发时,通常不会实际发送邮件,而是将其作为部署细节处理,并坚持使用Rails默认行为,即将邮件头和内容吐出到控制台STDOUT(您所在的位置)可以验证文本是否正确).您是否需要在开发环境中测试发送消息的具体原因?

其次,您提到在development.rb和environment.rb中都设置了SMTP设置.您不需要两次设置这些设置; 通常,我将development.rb用于特定于开发环境environment.rb的设置,并且仅用于始终适用于所有环境(开发,测试和实时部署的服务器)的设置.因此,如果您在development.rb和environment.rb中设置相同的设置,我首先要删除其中一个; 冗余只会让你的工作更加艰难.

最后,要解决这个问题,我首先要问Rails它的设置是什么,而不是等待邮件传递失败.请尝试以下方法:

  • 启动 rails console
  • 输入Rails.configuration.action_mailer.smtp_settings并比较结果哈希与您的期望.此哈希应包含发送所有邮件时使用的端口和域设置(在当前环境中),因此如果ActionMailer尝试错误的端口,那么我也希望端口在这里也是错误的.

你在哪里设置$SMTP_SERVER,$PORT$MAIL?您是否有任何理由不将Rails的约定用于环境变量ENV['SMTP_SERVER']等?

希望其中一些有所帮助.祝好运!

  • 我的问题现在解决了,负责人修复了wifi,然后又出现了另一个错误,因为我写了"smtp"而不是`:smtp`. (2认同)

mon*_*lls 5

代替

config.action_mailer.delivery_method = 'smtp'
Run Code Online (Sandbox Code Playgroud)

config.action_mailer.delivery_method = :smtp
Run Code Online (Sandbox Code Playgroud)

确保您的 Rails.configuration.action_mailer.smtp_settings 是符号键