title: Using mail form in rails for contact: Errno::ECONNREFUSED: Connection refused - connect(2)
body: <p>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...</p>
  
  <p><code>c = Contact.new(:name =&gt; 'Jose', :email =&gt; 'bob@bob.com', :message =&gt; 'blah')</code>
  <code>c.deliver</code></p>
  
  <p>This is where I get this...</p>
  
  <pre><code>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 `&lt;top (required)&gt;'
      from bin/rails:4:in `require'
      from bin/rails:4:in `&lt;main&gt;'irb(main):003:0&gt; 
  </code></pre>
  
  <p>My code in Contact.rb</p>
  
  <pre><code>class Contact &lt; MailForm::Base
    attribute :name,      :validate =&gt; true
    attribute :email,     :validate =&gt; /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
    attribute :message
    attribute :nickname,  :captcha  =&gt; true
  
    # Declare the e-mail headers. It accepts anything the mail method
    # in ActionMailer accepts.
    def headers
      {
        :subject =&gt; "Someone contacted you on your website!",
        :to =&gt; "myemail@gmail.com",
        :from =&gt; %("#{name}" &lt;#{email}&gt;)
      }
    end
  end
  </code></pre>
  
  <p>My contact form in haml</p>
  
  <pre><code>.row
    .large-8.small-centered.columns
      %div{align: "center"}
        %h2 Send A message to Us
        = form_for @contact, :html =&gt; {:class =&gt; 'form-horizontal' } do |f|
          .row
            .large-12.columns
              %h2 Name
              = f.text_field :name, :required =&gt; true, :placeholder =&gt; 'John Smith'
          .row
            .large-12.columns
              %h2 Email
              = f.text_field :email, :required =&gt; true, :placeholder =&gt; 'You@example.com'
          .row
            .large-12.columns
              %h2 Message
              = f.text_area :message, :as =&gt; :text, :required =&gt; true, :size =&gt; "30x10" 
          .hidden
            = f.text_field :nickname, :hint =&gt; 'Leave this field blank!'
          %div
            = f.submit 'Send message'
  </code></pre>
  
  <p>The gym is installed as well</p>
  
  <pre><code>gem 'mail_form'
  gem 'simple_form'
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>  match '/contacts', to: 'contacts#new', via: 'get'
    resources "contacts", only: [:new, :create]
  </code></pre>
  
  <p>contacts controller</p>
  
  <pre><code>class ContactsController &lt; 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 =&gt; "Thank you for contacting me. I will reply shortly!")
      else
        flash.now[:error] = 'Cannot send message.'
        render :new
      end
    end
  end
  </code></pre>
  
  <p>My Development Configuration(for localhost)</p>
  
  <pre><code> config.action_mailer.raise_delivery_errors = true
  
    config.action_mailer.default_url_options = { :host =&gt; 'localhost:3000' }
  
  
    config.action_mailer.delivery_method = :smtp
  </code></pre>
  
  <p>My Production(For Heroku)</p>
  
  <pre><code>config.action_mailer.default_url_options = { :host =&gt; '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 =&gt; "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"]
    }
  </code></pre>
  
  <p>I really don't know why it is doing this and the other explanations of this error on here do not work for me.</p>