Connection refused - connect(2) for "localhost" port 25 rails

During my training, I'm working on a website and we use Ruby on Rails. We need to send mails to users so I created a mailer.

I have tried to put the smtp in both development.rb and environment.rb

config.action_mailer.default_url_options = {host: ''}
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'

It tells me that the error comes from this method line 6

def create
  @user =

  respond_to do |format|
      # Tell the UserMailer to send a welcome Email after save

      format.html { redirect_to(@user, :notice => 'User was successfully created.') }
      format.json { render :json => @user, :status => :created, :location => @user }
      format.html { render :action => "new" }
      format.json { render :json => @user.errors, :status => :unprocessable_entity }

I have set the port to 587 but i keep getting the error:

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

It looks as if another file was overwriting my settings. I also saw that it might be related to my ssh key not being authorized by the server.

Do know what is wrong?

Thanks in advance

Connection refused - connect(2) for “localhost” port 25 rails. Ask Question Asked 4 years, 11 months ago. Active 1 year, 1 month ago. Viewed 25k times


config.action_mailer.delivery_method = 'smtp'


config.action_mailer.delivery_method = :smtp

Ensure your Rails.configuration.action_mailer.smtp_settings is symbolized keys

trevorwang changed the title Connection refused - connect(2) for "" port 8888 Connection refused - connect(2)

You need to remove config.action_mailer.perform_deliveries = true line.

2. You Have the Wrong Credentials. Although it may seem too simple to be true, it’s possible that you’re just entering the wrong credentials when trying to connect to your server. There are four pieces of information needed to run SSH: Host name. The IP address of the server you’re trying to connect to or your domain name. Username.

I was running into this issue when running Sidekiq::Worker.drain_all in my RSpec tests, and it was driving me crazy because I had config.action_mailer.delivery_method = :test in my config/environments/test.rb.

The solution was to set config.action_mailer.delivery_method = :test in my config/environments/development.rb, which is confusing because the implication is that my config/environments/development.rb is overriding my config/environments/test.rb in my RSpec tests.

Regardless, this might fix the problem for others.

RE: Emal notification - (Connection refused - connect(2)) - Added by hassan matinarfa almost 5 years ago It helped for me too . This character is very important # . we have to use it in first of lines where is not important or where we want, compiler don't check those lines .

The app might be using mailcatcher gem for all outbound emails on development, which you haven't installed or don't have running. At least that was my issue. Check out and follow the instructions given.

Error: Could not request certificate: Connection refused - connect(2) for "puppet" port 8140. edit. ssl. connection. certificate. asked 2016-03-16 06:34:40 -0600.

Failed (Connection refused - Connection refused). Sleeping for 2.0. From that point on, that log contains lines just like the last one. I tried to play with keeping

App staging fails with Connection refused - connect(2) for "" port 8889 Article Number: 7465 Publication Date: October 21, 2019 Author: Anser Arif Feb 26, 2020 • Knowledge Article

Connection refused - connect(2) for “localhost” port 25 rails. 2. Failed to open TCP connection to (connection refused) 0. Failed to open TCP

  • I guess you're supposed to use an email service like SendGrid or Amazon SES, since those work locally. I'm not sure how you're supposed to get the built-in Rails mailers to work at all. I'm certainly not going to deploy something to a server when it doesn't even work locally.
  • Thank you for your answer. I want to test sending messages to understand how it works (I'm new with Ruby on Rails) and I want to be sure that it works correctly because I'm not alone on this project and I don't want to broke everything. I figured out that the problem came from the wifi of the university that didn't allow me access to the SMTP.
  • OK, sounds fair. But just to be clear, my impression is Rails assumes you don't care about sending messages in development mode; it makes it easy to verify the contents of the message but stops short of the SMTP connection, which is generally treated as a deployment & server config detail (not part of the Rails app code itself).
  • My problem is now solved, the person in charge fixed the wifi and then I had another error which was because I wrote "smtp" instead of :smtp.
  • Upvote for opening the console and checking the settings there.
  • or remove it entirely as :smtp is the default now
  • glad I could help :)