Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 220
TheLittleBibi Avatar asked Jun 23 '15 17:06

TheLittleBibi


Video Answer


1 Answers

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!

like image 121
Topher Hunt Avatar answered Sep 24 '22 03:09

Topher Hunt