在Rails中使用邮件表单进行联系:Errno::ECONNREFUSED:连接被拒绝- connect(2)

3
在您询问之前,我已连接到互联网。我尝试使用我的herokuapp和本地主机上的表单,但两者都无法正常工作。它们应该发送到我的电子邮件。此外,我尝试在控制台中使用它...
``` c = Contact.new(:name => 'Jose', :email => 'bob@bob.com', :message => 'blah') c.deliver ```
这是我获得的内容...
Errno::ECONNREFUSED: Connection refused - connect(2)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:540:in `initialize'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:540:in `open'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:540:in `tcp_socket'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:550:in `block in do_start'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/timeout.rb:66:in `timeout'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:549:in `do_start'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
    from /Library/Ruby/Gems/2.0.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
    from /Library/Ruby/Gems/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:2129:in `do_delivery'
    from /Library/Ruby/Gems/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:232:in `block in deliver'
    from /Library/Ruby/Gems/2.0.0/gems/actionmailer-4.0.3/lib/action_mailer/base.rb:456:in `block in deliver_mail'
    from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notifications.rb:159:in `block in instrument'
    from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notifications.rb:159:in `instrument'
    from /Library/Ruby/Gems/2.0.0/gems/actionmailer-4.0.3/lib/action_mailer/base.rb:454:in `deliver_mail'
    from /Library/Ruby/Gems/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:232:in `deliver'
    from /Library/Ruby/Gems/2.0.0/gems/mail_form-1.5.0/lib/mail_form/delivery.rb:151:in `deliver!'
    from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/callbacks.rb:386:in `_run__1162424905731419627__deliver__callbacks'
    from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /Library/Ruby/Gems/2.0.0/gems/mail_form-1.5.0/lib/mail_form/shim.rb:49:in `deliver'
    from (irb):2
    from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.3/lib/rails/commands/console.rb:90:in `start'
    from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.3/lib/rails/commands/console.rb:9:in `start'
    from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.3/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'irb(main):003:0> 

我的Contact.rb代码如下:

class Contact < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message
  attribute :nickname,  :captcha  => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Someone contacted you on your website!",
      :to => "myemail@gmail.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

我的联系表单使用的是Haml语言

.row
  .large-8.small-centered.columns
    %div{align: "center"}
      %h2 Send A message to Us
      = form_for @contact, :html => {:class => 'form-horizontal' } do |f|
        .row
          .large-12.columns
            %h2 Name
            = f.text_field :name, :required => true, :placeholder => 'John Smith'
        .row
          .large-12.columns
            %h2 Email
            = f.text_field :email, :required => true, :placeholder => 'You@example.com'
        .row
          .large-12.columns
            %h2 Message
            = f.text_area :message, :as => :text, :required => true, :size => "30x10" 
        .hidden
          = f.text_field :nickname, :hint => 'Leave this field blank!'
        %div
          = f.submit 'Send message'

健身房也已安装好。
gem 'mail_form'
gem 'simple_form'

routes.rb

  match '/contacts', to: 'contacts#new', via: 'get'
  resources "contacts", only: [:new, :create]

联系人控制器

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      redirect_to(root_path, :notice => "Thank you for contacting me. I will reply shortly!")
    else
      flash.now[:error] = 'Cannot send message.'
      render :new
    end
  end
end

我的开发配置(本地)

 config.action_mailer.raise_delivery_errors = true

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


  config.action_mailer.delivery_method = :smtp

我的生产环境(适用于Heroku)

config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default :charset => "utf-8"

  config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: ENV["GMAIL_DOMAIN"],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
  }

我真的不知道为什么会出现这种情况,这里其他关于此错误的解释对我都没有用。


1
你有邮件发送配置吗? - zishe
发现更改 ActionMailer :: Base.delivery_method =:smtpActionMailer :: Base.delivery_method =:sendmail,但真正的结果就像用大锤去敲小坚果一样。 - zishe
没有考虑添加那个配置。我会把它放在那里。 - Taylor Mitchell
更改为sendmail没有起作用。 - Taylor Mitchell
3个回答

2

我已经解决了这个问题。我太蠢了,忘记添加Gmail邮件发送器。我没有意识到在邮件表单中需要它。我使用的教程从未提供这种类型的信息。

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            'myname@gmail.com',
  password:             'mypassword',
  authentication:       'plain',
  enable_starttls_auto: true  }

2

执行 rake db:migrate 命令时,我花了一个小时的时间,但出现了以下错误:Errno::ECONNREFUSED: Connection refused - connect(2)

之后,当我再次运行 rake db:migrate 命令并重新启动服务器时,问题得以解决。


1
我不能直接回复或点赞nour在上面的解决方案,但运行rails db:migrate对我非常有帮助。最近升级到Rails 7.0后,我在Heroku上运行的Rails应用程序(通过Sendgrid发送电子邮件)遇到了电子邮件问题。不知何故,即使我的应用程序中没有未决的迁移,"Errno::ECONNREFUSED (Connection refused - connect(2) for 127.0.0.1:25)"错误消息消失了,并且电子邮件再次开始工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接