TheLittleBibi
TheLittleBibi

Reputation: 221

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: '0.0.0.0:3000'}
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 = User.new(user_params)

  respond_to do |format|
    if @user.save
      # Tell the UserMailer to send a welcome Email after save
      UserMailer.welcome_email(@user).deliver_now

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

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

Upvotes: 22

Views: 42053

Answers (7)

stevec
stevec

Reputation: 52907

For anyone clumsy like me, I got this message when I had everything set up perfectly in my app, but I had simply forgotten to run the command that adds the mailing addon in my production environment. In my case, that line was heroku addons:create mailgun:starter.

Upvotes: 1

Gino
Gino

Reputation: 1133

I ran into the same error message well developing my own application. What I discovered is that as I was not actually sending any emails in a development environment I needed to change one of the lines in the configuration file found at: /your_apps_name/config/environments/development.rb

from

config.action_mailer.raise_delivery_errors = true

to

config.action_mailer.raise_delivery_errors = false

This was causing my application to raise errors when emails were not successfully delivered, and I wasn't actually sending emails so of course they were not being delivered successfully.

Upvotes: 3

The Whiz of Oz
The Whiz of Oz

Reputation: 7043

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 https://mailcatcher.me and follow the instructions given.

Upvotes: 2

mrmicrowaveoven
mrmicrowaveoven

Reputation: 185

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.

Upvotes: 2

kimprap
kimprap

Reputation: 151

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

Upvotes: 1

Montells
Montells

Reputation: 6679

replace

config.action_mailer.delivery_method = 'smtp'

with

config.action_mailer.delivery_method = :smtp

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

Upvotes: 6

Topher Hunt
Topher Hunt

Reputation: 4813

First of all, when developing on localhost, it's common to not actually send out mail, rather to treat that as a deployment detail and stick with the Rails default behavior which is to spit out the mail headers and contents into the console STDOUT (where you can verify that the text looks right). Is there a specific reason why you need to test sending messages in the dev environment?

Secondly, you mentioned that you set the SMTP settings in both development.rb and environment.rb. You shouldn't need to set these settings twice; in general I'd use development.rb for settings specific to the dev environment, and environment.rb only for settings that will always apply to all environments (dev, tests, and on the live deployed server). So if you're setting the same settings in both development.rb and environment.rb, I'd start by removing one or the other; redundancy will only make your job harder down the road.

Finally, to troubleshoot this I'd start by asking Rails what its settings are rather than waiting for the mail delivery to fail. Try the following:

  • Start up rails console
  • Enter Rails.configuration.action_mailer.smtp_settings and compare the resulting hash against your expectations. This hash should contain the port and domain settings that are used when sending out all mail (in the current environment), so if ActionMailer is trying the wrong port then I'd expect the port to be wrong here too.

Where are you setting $SMTP_SERVER, $PORT and $MAIL? Is there any reason you aren't using Rails' convention for environment variables, ENV['SMTP_SERVER'] etc.?

Hope some of that helps. Good luck!

Upvotes: 23

Related Questions