在rails中使用邮件表单进行联系:Errno :: ECONNREFUSED:连接被拒绝 - connect(2) [英] Using mail form in rails for contact: Errno::ECONNREFUSED: Connection refused - connect(2)

查看:188
本文介绍了在rails中使用邮件表单进行联系:Errno :: ECONNREFUSED:连接被拒绝 - connect(2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你问之前,我已连接到互联网。我已经尝试使用我的herokuapp上的窗体和我的本地主机上的窗体,都没有工作。他们应该发送到我的电子邮件。我也尝试在控制台上使用它...



c = Contact.new(:name =>'Jose',:email = >'bob@bob.com',:message =>'blah')
c.deliver



这是我得到这个的地方...

  Errno :: ECONNREFUSED:Connection拒绝 - 从/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/smtp.rb:540:in`initialize'$ b连接(2)
$ b 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:来自/System/Library/Frameworks/Ruby.framework/Versions/的`tcp_socket'
2.0 / usr / lib / ruby​​ / 2.0.0 / net / smtp.rb:550:在`block in do_start'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ ruby / 2.0.0 / timeout.rb:66:在`timeout'
从/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:在'start'中
从/Library/Ruby/Gems/2.0.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb :112:在`deliver!'
/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 from'从/Library/Ruby/Gems/2.0提交
。 0 / gems / actionmailer-4.0.3 / lib / action_mailer / base.rb:456:在`block in delivery_mail'
从/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/ lib / active_support / notifications.rb:159:在`block in instrument'中
从/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notifications/instrumenter.rb: 20:在`instrument'中
从/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notifications.rb:159:in`instrument'
从/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从/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:从/library/Ruby/Gems/2.0的`deliver'
(irb):2
。 0 / gems / railties-4.0.3 / lib / rails / commands / console.rb:90:在`start'$ b $ / from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.3/ lib / rails / commands / console.rb:9:在'start'$ b $ / from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.3/lib/rails/commands.rb:62:在'< top(required)>'bin / rails中的
:4中的'require'
中的bin / rails:4:in< main>'irb(main):003: 0 GT;

我在Contact.rb中的代码

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

#声明电子邮件标题。它接受ActionMailer接受的任何邮件方法
#。
def headers
{
:subject => 有人在您的网站上与您联系!,
:to => myemail@gmail.com,
:from => %(#{name}<#{email}>)
}
结束
结束

我在haml中的联系表单

  .row 
.large-8 .small-centered.columns
%div {align:center}
%h2发送消息给我们
= form_for @contact,:html => {:class => 'form-horizo​​ntal'}} do | f |
.row
.large-12.columns
%h2名称
= f.text_field:name,:required => true,:placeholder => 'John Smith'
.row
.large-12.columns
%h2电子邮件
= f.text_field:email,:required => true,:placeholder => 'You@example.com'
.row
.large-12.columns
%h2消息
= f.text_area:message,:as => :text,:required => true,:size => 30x10
.hidden
= f.text_field:昵称,:hint => '将此栏留空!'
%div
= f.submit'发送信息'

健身房也已安装

  gem'mail_form'
gem'simple_form'


$ b

routes.rb

c'match'/ contacts',to:'contacts#new',via:'get'
resourcescontacts,only:[:new,:create]

联络人控制器

  class ContactsController< ApplicationController 
def new
@contact = Contact.new
end
$ b def创建
@contact = Contact.new(params [:contact])
@ contact.request =请求$ b $如果@ contact.deliver
redirect_to(root_path,:notice =>感谢您与我联系,我会尽快回复!)
else
flash.now [:error] ='无法发送信息'
渲染:新建
结束
结束
结束

我的开发配置(用于本地主机)

  config.action_mailer.raise_delivery_errors = true 

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


config.action_mailer.delivery_method =:smtp

My Production(For 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 = {
地址:smtp.gmail.com,
端口:587,
域名:ENV [ GMAIL_DOMAIN],
身份验证:plain,
enable_starttls_auto:true,
user_name:ENV [GMAIL_USERNAME],
密码:ENV [GMAIL_PASSWORD]

我真的不知道为什么会这样做,而这个错误的其他解释在这里不适合我。

解决方案

我能够解决这个问题。愚蠢的我忘了添加Gmail邮件。我没有意识到我需要邮件形式。我使用的教程从未放入这种类型的信息。

  config.action_mailer.delivery_method =:smtp 
config。 action_mailer.smtp_settings = {
地址:'smtp.gmail.com',
端口:587,
域名:'gmail.com',
user_name:'myname @ gmail。 com',
password:'mypassword',
authentication:'plain',
enable_starttls_auto:true}


Before you ask, I am connected to the internet. I have tried using the form on my herokuapp and the form on my localhost and neither work. They should be sending to my email. Also I tried using it with the console...

c = Contact.new(:name => 'Jose', :email => 'bob@bob.com', :message => 'blah') c.deliver

This is where I get this...

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> 

My code in 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

My contact form in 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'

The gym is installed as well

gem 'mail_form'
gem 'simple_form'

routes.rb

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

contacts controller

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

My Development Configuration(for localhost)

 config.action_mailer.raise_delivery_errors = true

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


  config.action_mailer.delivery_method = :smtp

My Production(For 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"]
  }

I really don't know why it is doing this and the other explanations of this error on here do not work for me.

解决方案

I was able to fix this issue. Stupid me forgot to add the gmail mailer. I didn't realize I needed it with mail form. The tutorial I used never put in that type of information.

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  }

这篇关于在rails中使用邮件表单进行联系:Errno :: ECONNREFUSED:连接被拒绝 - connect(2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆