Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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      => 'xxxxxxx@xxx.xxx',
    :password       => 'xxxxxxxxx'
  }

In the controller, I have written the following:

def confirm_registration_in_c       
 @user = User.find_by_email(asdf123@gmail.com)
 if @user
      UserMailer.confirm_registration(@user).deliver            
 end
end

In my user_mailer.rb :

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...