This project is archived and is in readonly mode.

#1731 ✓resolved
José Valim

Make enable_starttls_auto opt-in in ActionMailer

Reported by José Valim | January 11th, 2009 @ 10:14 AM

This commit:

http://github.com/rails/rails/co...

Enabled automatically STARTTLS in SMPT server making some applications break.

This patch makes enable_starttls_auto opt-in.

Comments and changes to this ticket

  • Pratik

    Pratik January 11th, 2009 @ 02:52 PM

    • Milestone cleared.
    • Assigned user set to “Pratik”
  • Pratik

    Pratik January 18th, 2009 @ 04:18 AM

    Pushed with default to true.

  • Repository

    Repository January 18th, 2009 @ 04:19 AM

    • State changed from “new” to “resolved”

    (from [c2e7851fb20d24f49b55b5276cc3056082721dc4]) Add ActionMailer::Base#enable_starttls_auto option for enabling/disabling STARTTLS. [#1731 state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

  • José Valim

    José Valim January 18th, 2009 @ 08:02 AM

    Pratik,

    I just let it false because some applications were breaking.

    I wanted to avoid the situation that someone is complaining that something is not working and then we had to reply that he have to set something to false in order to make everything work.

  • jts

    jts February 5th, 2009 @ 01:50 AM

    • Tag set to getaddrinfo: name or service not known, 2.2, actionmailer, smtp, socketerror

    hi, em, I'm just trying to get my website to use actionmailer. I've not been successful. The error I get is always getaddrinfo: Name or service not known.

    the 500 error I get throws the following information into the logfile:

    SocketError (getaddrinfo: Name or service not known):

    /usr/lib/ruby/1.8/net/protocol.rb:206:in `initialize'
    /usr/lib/ruby/1.8/net/protocol.rb:206:in `new'
    /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open'
    /usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
    /usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
    /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open'
    /usr/lib/ruby/1.8/net/smtp.rb:392:in `do_start'
    /usr/lib/ruby/1.8/net/smtp.rb:377:in `start'
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:671:in `perform_delivery_smtp'
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `__send__'
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `deliver!'
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `method_missing'
    /app/controllers/contact_controller.rb:26:in `contact_me'
    
    

    I did a search on base.rb:392:in method_missing' and found this site.

    my smtp settings in production.rb are as follows: config.action_mailer.smtp_settings = {

        :domain         => 'domain.com',
        :address        => 'localhost',
        :port           => 25,
        :user_name      => 'user',
        :password       => 'passwd',        
        :authentication => :login
    
    

    }

    my production smtp server doesn't require tls..

    Will the diff patch fix my problem?

    How do I apply it?

    thanx! L

  • José Valim

    José Valim February 5th, 2009 @ 09:23 AM

    jts,

    The patch was already applied into core.

    Try changing your configurations to:

    config.action_mailer.smtp_settings = {

    :domain               => 'domain.com',
    :address              => 'localhost',
    :port                 => 25,
    :user_name            => 'user',
    :password             => 'passwd',        
    :authentication       => :login,
    :enable_starttls_auto => false
    
    

    }

    The problem is that enable_starttls_auto comes true by default.

    Tell us if that worked! :)

  • jts

    jts February 5th, 2009 @ 08:59 PM

    Hi Jose, thanks for your response, much appreciated.

    Unfortunately, I'd already tried :enable_starttls_auto => false in the configuration seetings and no joy, I'm still receiving the same error message.

    The production log file looks like this for the POST to the controller responsible for mailing Parameters: {"name"=>"test user", "message"=>"testing", "commit"=>"send message", "authenticity_token"=>"a550eb2ff258d7fef0018lfkdofijd3388961d5", "website"=>"test message", "email"=>"jts@gmail.com"} Sent mail to jts@gmail.com getaddrinfo: Name or service not known

    The fact that I'm getting the message "Sent mail to jts@gmail.com" suggests that the call to UserMailer.deliver_contact_form is working correctly, but that something fails in communicating with the smtp service.

    I've tried POP auth before SMTP too, no luck.. And my hosting provider support is useless. They've suggested trying localhost as the domain twice now !!!

    Is there anything else I might check?

    for your info, here's the versions of ruby & rails I have installed on production, dunno if it'll help..

    ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux] Rails 2.2.2 local gems:

    actionmailer (2.2.2, 2.1.2) actionpack (2.2.2, 2.1.2) activerecord (2.2.2, 2.1.2) activeresource (2.2.2, 2.1.2) activesupport (2.2.2, 2.1.2) builder (2.1.2) cgi_multipart_eof_fix (2.5.0) daemons (1.0.10) fastthread (1.0.1) fcgi (0.8.7) gem_plugin (0.2.3) hpricot (0.6.164) login_generator (1.2.2) mislav-will_paginate (2.3.6, 2.2.3) mongrel (1.1.5) mongrel_cluster (1.0.5) mysql (2.7) radiant (0.6.9) rails (2.2.2, 2.1.2) rake (0.8.3) RedCloth (4.1.1) rmagick (1.15.17) sqlite3-ruby (1.2.4) tzinfo (0.3.12) will_paginate (2.2.2)

    thanks for any advice you might be able to think of! L

  • Ken Liu

    Ken Liu February 6th, 2009 @ 04:17 AM

    Correct me if I am wrong, but it appears that the patch was applied for 2.3, so this option is not available in 2.2.2.

    I got this working by monkey patching 2.2.2.

    I added this to environment.rb:

    
    if RAILS_GEM_VERSION == '2.2.2'
      module ActionMailer
        class Base
    
          def perform_delivery_smtp(mail)
            destinations = mail.destinations
            mail.ready_to_send
            sender = mail['return-path'] || mail.from
    
            smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
            smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
            smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
                  smtp_settings[:authentication]) do |smtp|
                    smtp.sendmail(mail.encoded, sender, destinations)
                  end
           end
        end
      end
    else
      raise "you forgot to remove the ActionMailer monkey patch!"
    end
    
  • jts

    jts February 6th, 2009 @ 03:48 PM

    I'm a bit apprehensive about upgrading to 2.2.3 on the production server - I dimly remember how much of a pain it was to upgrade to 2.2.2 (on bluehost), so I'd like to avoid that if possible.

    I put the "monkey patch" at the end of my environment.rb and the application died, so I quickly removed it again!

    Should I put monkey patch into or outside of the

    Rails::Initializer.run do |config| block?

    thanks for your input ken!

  • Ken Liu

    Ken Liu February 8th, 2009 @ 02:00 AM

    2.2.2 is the latest release, 2.3 (where the fix was committed) hasn't been released yet.

    I put the patch at the end of environment.rb.

  • jts

    jts February 8th, 2009 @ 10:21 PM

    Thanks Ken, I've managed to "monkey patch" my environment.rb.

    Alas, after this failed to fix the issue, I did a telnet session with my hosts SMTP service and it's failing to transmit a message with an SMTP status code 550.

    So I've mailed support about this and I hope they'll (finally) be able to solve the issue...

    thanks for all your help everyone! Will let you know if/when the issue is resolved!!

  • Ken Liu

    Ken Liu February 9th, 2009 @ 02:04 AM

    @jts

    sorry, now reading through the description of your issue again, I'm not sure that this particular patch addresses your problem. Good luck in getting your mail problems solved!

  • jts

    jts February 9th, 2009 @ 02:19 AM

    I've found something quite strange.

    I've got in my user_mailer.rb a function to fire off a mail to the user via the contact form...

    def contact_form(name, email, website, message)

                recipients      "app@domain.com"
                from            "app@domain.com"
                subject         "your contact form"
                sent_on         Time.now
                content_type    "text/html"
                body            :name => name, :email => email, :website => website, :message => message
                template        "contact_form"
        end
    
    

    If I specify the body variables as a hash, like the documentation states, my application fails (error code 500).

    If I specify the body variables as I have above, the program works, but I'm getting this getaddrinfo: Name or service not known error.

    The documentation on ActionMailer that I'm referring to is here: http://guides.rubyonrails.org/ac...

    Could I be doing something wrong that's causing the program to return a 500 status code?

    thanx! j

  • marzocchi

    marzocchi February 9th, 2009 @ 06:07 AM

    • Assigned user cleared.

    how to apply this patch, i'm quite new to rails thx

  • Randy

    Randy February 19th, 2009 @ 08:37 PM

    re: Ken Liu's monkey patch to get it working in 2.2.2

    I'd like to verify Ken's monkey patch mentioned above. It fixed this issue on my shared host that was showing this problem.

  • Mark Glossop

    Mark Glossop May 6th, 2009 @ 01:10 AM

    I'll also confirm Ken's monkey patch as working; fixed this somewhat insidious problem when setting up email, using Redmine 0.8.x on Dreamhost.

    The main indicator of the problem is the error "hostname was not match with the server certificate" - without trawling through the ActionMailer source, I'm guessing that there's a problem in AM when the SSL cert name doesn't match the SMTP server name [relatively common for wildcard SSL certs in shared hosting environments]. Potentially should be an option for ignoring certificate mismatch errors, but I guess that's open to abuse, and I'm not about to make a request for it since I'm not prepared to cobble a patch together myself.

    Still, hopefully helps someone else stumbling through google results.

  • Sayantam Dey

    Sayantam Dey February 15th, 2010 @ 01:22 PM

    • Tag changed from getaddrinfo: name or service not known, 2.2, actionmailer, smtp, socketerror to getaddrinfo: name or service not known, 2, actionmailer, smtp, socketerror

    I think this patch solves the problem of sending mail via TLS but will fail for SSL. The problem with SMTP over SSL (or SMTPS) is the moment you send an EHLO in clear case, the mail server will terminate the connection. So, the capability check with starttls_auto? will never happen.

    I am using Ruby 1.8.7 and net/smtp.rb has the following code:

    s = timeout(@open_timeout) { TCPSocket.open(@address, @port) }   
    logging "Connection opened: #{@address}:#{@port}"
    @socket = new_internet_message_io(tls? ? tlsconnect(s) : s) # *1
    check_response critical { recv_response() }
    do_helo helo_domain
    # control never reaches here
    if starttls_always? or (capable_starttls? and starttls_auto?)
    

    We want tls? to return true (line marked with *1), so that an SSL connection is obtained at the beginning itself. I added the following code to action_mailer#base.rb:perform_delivery_smtp(mail) -

    if smtp_settings[:ssl]
      smtp.enable_tls()
    else
      smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
    end
    

    Full patch is attached. Needs to be dropped into config/initializers and add :ssl => true in config.action_mailer.smtp_settings hash.

    I read through the 3.0.0 beta code, but could not make out if the issue is fixed or not.

  • Andrea Campi

    Andrea Campi October 16th, 2010 @ 11:39 PM

    • Tag changed from getaddrinfo: name or service not known, 2, actionmailer, smtp, socketerror to getaddrinfo: name or service not known, 2-3-stable, actionmailer, smtp, socketerror

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages