Connection refused - connect(2) Ruby on Rails Mail Setup
Asked Answered
P

2

8

I have set smtp settings in both config/environments/production.rb and development.rb also i have added the settings in `config/initializers/setup_mail.rb

config.action_mailer.default_url_options = { :host => 'ipaddress' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => '[email protected]',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
}

config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => '[email protected]',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
} 

ActionMailer::Base.default_url_options[:host] = "ipaddress" 

i am getting the error Connection refused - connect(2)

while i have working in localhost with below configuration I haven’t get any error and also mail has been sent.

config/initializers/setup_mail.rb (localhost)

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'localhost',
  :user_name            => '[email protected]',
  :password             => 'pass',
  :authentication       => 'plain',
  :enable_starttls_auto => true
} 

ActionMailer::Base.default_url_options[:host] = "localhost:3000" 

Errors are show below while running in console,

Errno::ECONNREFUSED: Connection refused - connect(2)
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `initialize'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `open'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `tcp_socket'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `block in do_start'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:69:in `timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `do_start'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:520:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:415:in `block in deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `block in instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:413:in `deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
from (irb):28
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

controller

def sendResume
@name =params[:name]
@email_id = params[:email_id]
@mob_no = params[:ph_no]

attachments = params[:resume]
if simple_captcha_valid?

 if params[:resume]
     filename=attachments.original_filename   

    extname = File.extname(filename)[1..-1]
    mime_type = Mime::Type.lookup_by_extension(extname)
    content_type = mime_type.to_s unless mime_type.nil?


        if content_type !="application/pdf"
           flash[:error]= "Only pdf files are allowed"
           redirect_to :action=>"careers"
        else
         File.open(Rails.root.join('tmp', 'uploads', attachments.original_filename), 'w') do |file|
          re = attachments.read
          file.write(re.force_encoding("utf-8"))
          @attached_path = file.path
        end


        begin
          ResumeMailer.sendResume(@name, @email_id, @mob_no, @attached_path, attachments.original_filename).deliver
          flash[:notice] = "Your resume has been submitted successfully"
          redirect_to :action=>"careers"
        rescue Exception => e
          puts e.message

          logger.warn "error sending mail"
          flash[:error]= "Error in submitting resume"
          redirect_to :action=>"careers"
        end

     end
    else
      flash[:error]= "Please upload your resume"
      redirect_to :action=>"careers"
    end
else

  flash[:error]= "Incorrect captcha"
   redirect_to :action=>"careers"
end

end

After installing the postfix the error goes away in console mode and the mails send from console but in graphical mode (in browser) i am getting the error Connection refused - connect(2).

what is wrong,

thanks in advance.

Protease answered 6/5, 2014 at 11:59 Comment(28)
it looks alright , can you post the log ?Smog
log files showing blankProtease
the one under app/log/development.logSmog
both development.log and production.log showing emptyProtease
What all is there in the app now ? I mean is there a user part and so , coz if it's there atleast it would have something . It can't be empty .Smog
@CaffeineCoder in app directory there no log folderProtease
Have you just started development on it ?Smog
no, most of development task has been completedProtease
You won't find it under there , by app i meant your application . You will find a different folder named log outside where gemfile are locatedSmog
yes, in my application it has log folder with 2 files development.log and production.log but both are showing emptyProtease
You can do one thing , try mandrill instead of gmail . Gmail sometimes causes errors . If the error still persists , the problem is in application .Smog
the same file i worked from the localhost the mail send but on live i have this errorProtease
This is your culprit then - config.action_mailer.default_url_options = { :host => 'website.com' } , change it in production.rbSmog
i have changed the host with domain name no useProtease
Helphin, have you tried running it in console? Any errors come up there?Dunleavy
@Dunleavy how to run it in console?Protease
Where are you getting the connection refused from? From trying to send a mail request out? If so, try to do it manually in console.. MyMailer.my_mail(the_paramaters).deliver . I'm not sure what your mailer model, method or parameters would be..Dunleavy
@Dunleavy i have updated the error while run through consoleProtease
OOC, are you working behind a firewall or an intranet?Dunleavy
No, its a application or website on liveProtease
@Trip, Now its working in console but not working in browserProtease
Oh yah? You're getting emails sent to you? Can you post all your relevant controller/model code that delivers the email? Do you have a debugger installed? Can you throw a debugger in right before the delivery call is made and ensure that you have all the accurate information there?Dunleavy
after installing the postfix and i configured it then i set openssl_verify_mode = 'none' after that the mail send through console i have copied the same config into live but luckProtease
i have received the mail which send through consoleProtease
i have not installed debuggerProtease
can you do a telnet from your production system to smtp.gmail.com on port 587? Maybe the provider is interfering.Diaspore
@BooVeMan, can you tell how to do telnetProtease
pls refer this-> #4313677Moonlighting
S
10

I see you have tried identical settings on both servers but are having problems only on your production / non-local host environment.

This points to an issue with the network configuration of the environment itself.

In the command line on your same server as your application, try the following command

telnet smtp.gmail.com 587

You should see something like the following

telnet smtp.gmail.com 587
Trying 173.194.79.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtp

If you do not see this, you will most likely get a connection error. This means your machine does not have access to the gmail server. Likely problems are a) general outbound network connectivity, b) firewalls specifically blocking all outbound connections c) firewalls blocking/allowing connections to particular ports or hosts

If this does not work, also try the following ports in place of 587

telnet smtp.gmail.com 465
telnet smtp.gmail.com 25

If one of these is more successful, change your mail server settings to use accordingly.

Edit: We had quite a bit of trouble using Gmail and then Gmail with our custom domain. One thing that can help is to remove the :domain line from your configuration file, try without it.

For reference, here is my Gmail config in prod using our custom domain hosted by Gmail:

config.action_mailer.default_url_options = { :host => "my.website.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'website.com',
    user_name:            '[email protected]',
    password:             'password',
    authentication:       'plain',
    enable_starttls_auto: true
}

Also, if you do have 2-factor authentication enabled on your Gmail account, you might want to disable it and try this again just to confirm it is not complicating the issue.

Selfeffacement answered 14/5, 2014 at 1:18 Comment(5)
i am geting something like Trying 173.194.79.108... Connected to gmail-smtp-msa.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtpProtease
what to do after getting this..?Protease
@Protease This means the server is open. Can you also try removing the :domain line from everywhere in your config?Selfeffacement
i have tried this many times not worked. after some couple of changes with this i got works, thanks for your kind answer.Protease
@Helphin, I am also getting the same error. How did you resolved this issue can you please tell the steps. Did removing the domain from configuration helped?Dalmatian
P
1

You gave the domain as localhost. The domain should be gmail.com. Please change Your config/initializers/setup_mail.rb to

ActionMailer::Base.smtp_settings = { :address => 'smtp.gmail.com', :port => 587, :domain => 'gmail.com', :user_name => '[email protected]', :password => 'pass', :authentication => 'plain', :enable_starttls_auto => true }

And in production.rb. You don't need to configure for email address. The following lines are enough.

config.action_mailer.default_url_options = { :host => 'app_url' }
config.action_mailer.delivery_method = :smtp
Pluri answered 13/5, 2014 at 11:30 Comment(1)
thanks for your answer but same error i get after updating thisProtease

© 2022 - 2024 — McMap. All rights reserved.