Errno::ECONNREFUSED: Connection refused - connect(2) for action mailer

90,443

Solution 1

In my situation, I encountered similar problems when I was trying to making through a sending-email Rails app tutorial, the Heroku logs kept telling my that

......

Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):

......

After I compared my code with author's code, I got to find out that I had not setup my ActionMailer configurations in the config/environments/production.rb file.

Then I came to realized that I just had my config/environments/development.rb configured for sending-email, but I had not done it for my config/environments/production.rb.

So you may check it when your app's behavior difers between development and production.

Solution 2

Be sure you have configured your port correctly. I switched from gmail in development (port 587) to sending from my local server in production and was getting this error till I corrected the port to the one my server uses (port 25).

Solution 3

My problem is not identical to this question, but I feel many would found this thread via google.

If you use external SMTP service like sendgrid and has set up ActionMailer accordingly, yet it still gives this error:

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25

You may be passing config hash with String key, which are ignored. Keys must be symbols!

This may happen if it is de-serialized, what I did is to ensure keys are symbols:

config.action_mailer.smtp_settings = get_smtp_setting.symbolize_keys

Solution 4

for production you cant write

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

add production url for host, like,

config.action_mailer.default_url_options = { :host => "http://www.yourdomain.com" }

Solution 5

There is another reason for this error:

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25

It should be looked at SENDMAIL service on your server:

  • Is SENDMAIL installed?
  • Is SENDMAIL running?

I had this error due to the stopped SENDMAIL.

Good luck!

Share:
90,443

Related videos on Youtube

VenkatK
Author by

VenkatK

I am a ruby on rails developer

Updated on July 09, 2022

Comments

  • VenkatK
    VenkatK almost 2 years

    I have been working with rails since a long. Now I am facing a small issue in the ActionMailer. I want to send an email when user gets registered to confirm his registration. I am able to send email in the development mode but where as not in the production mode.
    the exception Errno::ECONNREFUSED: Connection refused - connect(2) is coming everytime when deliver method is called.
    I have written the following code.
    My SMTP config looks:
    config.action_mailer.default_url_options = { :host => "localhost:3000" }

      config.action_mailer.delivery_method = :smtp
      config.action_mailer.perform_deliveries = true
      config.action_mailer.raise_delivery_errors = true
    
      config.action_mailer.smtp_settings = {   
        :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,      
        :ssl => true,
        :enable_starttls_auto => true,  #this is the important stuff!
        :address        => 'smtp.xxxx.xxx',
        :port           => xxx,
        :domain         => 'xxxxxx',
        :authentication => :plain,
        :user_name      => '[email protected]',
        :password       => 'xxxxxxxxx'
      }
    

    In the controller, I have written the following:

    def confirm_registration_in_c       
     @user = User.find_by_email([email protected])
     if @user
          UserMailer.confirm_registration(@user).deliver            
     end
    end
    

    In my user_mailer.rb :

    class UserMailer < ActionMailer::Base
      default from: "[email protected]"
    
      def confirm_registration(user)
       @user = user
       @user_name = @user.name       
       email = @user.email 
       mail(:to => email, :subject => "Reset your password")
      end
    end
    

    I am able to send email in the development mode in my local host, but I am not able to send the email in the dedicated server.
    Can anybody help me please?

  • Inaccessible
    Inaccessible almost 11 years
    did you restart the server after you make change?
  • VenkatK
    VenkatK almost 11 years
    Yes I have restarted the server after the changes made. But same error.
  • Rajendran_R
    Rajendran_R almost 11 years
    login into your gmail account(user_name => '[email protected]'). then restart the server
  • Rajendran_R
    Rajendran_R almost 11 years
    Is above mailer settings production.rb or ..?
  • VenkatK
    VenkatK almost 11 years
    I have written that mailer setting in the /config/application.rb file which is common for all.
  • VenkatK
    VenkatK almost 11 years
    And I have tried to put that mailer setting in the production.rb but the same issue is coming.
  • wetjosh
    wetjosh over 8 years
    I made the same silly mistake. I had added the line config.action_mailer.smtp_settings = { address: "xxxxxxx.xxxxxxx.com" } in app/config/environments/development.rb but forgot to add it to app/config/environments/production.rb.
  • fguillen
    fguillen over 5 years
    Even if this was not my problem it put me in the right direction: my configuration was not being loaded, a console execution of this ActionMailer::Base.smtp_settings was showing me that the values were the default ones
  • Roger Perez
    Roger Perez about 5 years
    @fguillen how did you fix it? Code?
  • Phillip Longman
    Phillip Longman over 4 years
    I don't believe this person is using Sendmail, so this is not relevant.
  • Taika
    Taika over 4 years
    I agree with you. But it had solved my problem, so it could be helpful to others. :-)
  • Francisco Quintero
    Francisco Quintero over 2 years
    This helped me go in the right direction. My issue was I had set address: 'localhost' instead of address: '127.0.0.1'.