public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7) and the 
SMTP server supports it [#1336 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
granth (author)
Wed Nov 05 19:54:37 -0800 2008
dhh (committer)
Thu Nov 06 04:07:16 -0800 2008
commit  732c724df61bc8b780dc42817625b25a321908e4
tree    c444d2594fc1bc6f3888d9eeb61b6b8417959d58
parent  6406a87eedb74a41f19f5ad21ea1b8f97dd45755
...
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
0
@@ -1,3 +1,8 @@
0
+*2.2.1 [RC2 or 2.2 final]*
0
+
0
+* Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7) and the SMTP server supports it (This is required for Gmail's SMTP server) #1336 [Grant Hollingworth]
0
+
0
+
0
 *2.2.0 [RC1] (October 24th, 2008)*
0
 
0
 * Add layout functionality to mailers [Pratik]
...
663
664
665
666
667
 
 
 
 
668
669
670
...
663
664
665
 
 
666
667
668
669
670
671
672
0
@@ -663,8 +663,10 @@ module ActionMailer #:nodoc:
0
         mail.ready_to_send
0
         sender = mail['return-path'] || mail.from
0
 
0
-        Net::SMTP.start(smtp_settings[:address], smtp_settings[:port], smtp_settings[:domain],
0
-            smtp_settings[:user_name], smtp_settings[:password], smtp_settings[:authentication]) do |smtp|
0
+        smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
0
+        smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
0
+        smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
0
+                   smtp_settings[:authentication]) do |smtp|
0
           smtp.sendmail(mail.encoded, sender, destinations)
0
         end
0
       end
...
24
25
26
 
 
 
 
27
28
29
30
31
 
 
32
33
34
...
24
25
26
27
28
29
30
31
32
33
 
 
34
35
36
37
38
0
@@ -24,11 +24,15 @@ class MockSMTP
0
   def sendmail(mail, from, to)
0
     @@deliveries << [mail, from, to]
0
   end
0
+
0
+  def start(*args)
0
+    yield self
0
+  end
0
 end
0
 
0
 class Net::SMTP
0
-  def self.start(*args)
0
-    yield MockSMTP.new
0
+  def self.new(*args)
0
+    MockSMTP.new
0
   end
0
 end
0
 
...
938
939
940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
941
942
943
...
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
0
@@ -938,6 +938,20 @@ EOF
0
     mail = TestMailer.create_body_ivar(@recipient)
0
     assert_equal "body: foo\nbar: baz", mail.body
0
   end
0
+
0
+  def test_starttls_is_enabled_if_supported
0
+    MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
0
+    MockSMTP.any_instance.expects(:enable_starttls_auto)
0
+    ActionMailer::Base.delivery_method = :smtp
0
+    TestMailer.deliver_signed_up(@recipient)
0
+  end
0
+
0
+  def test_starttls_is_disabled_if_not_supported
0
+    MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false)
0
+    MockSMTP.any_instance.expects(:enable_starttls_auto).never
0
+    ActionMailer::Base.delivery_method = :smtp
0
+    TestMailer.deliver_signed_up(@recipient)
0
+  end
0
 end
0
 
0
 end # uses_mocha

Comments

juliamae Wed Nov 12 11:30:09 -0800 2008

NICE

mdemare Sun Nov 23 05:11:42 -0800 2008

I don’t think this should be turned on if the server doesn’t have a certificate. Not all servers do, and it broke my ExceptionNotification, so I realized it two days late.

davidw Mon Dec 08 09:21:56 -0800 2008

This is causing problems for me as well:

OpenSSL::SSL::SSLError (hostname was not match with the server certificate)

dhh Tue Dec 09 02:04:00 -0800 2008

Sounds reasonable that we should have an option or at least a fallback for this. Anyone wants to give it a stab?

darragh Tue Dec 09 02:46:09 -0800 2008

While the issue remains – perhaps it’s helpful to point out a quick workaround if you’re using postfix.

Disable tls by setting smtpd_use_tls=no

if you are determined to use tls then really you should set up your certs/keys and enforce it with smtpd_tls_auth_only=yes

more details here: http://postfix.state-of-mind.de/patrick.koetter/smtpauth/postfix_tls_support.html alternatively -

electic Sun Dec 28 12:03:48 -0800 2008

This is likely a architecture oversight. I upgraded my system and it died cause the hosts were mismatched. This is common on hosting environments and there should be at the least, a option in the settings that either ignores it or at least gives the option not to use ssl.

pboling Sat Jan 10 15:16:16 -0800 2009

This is causing a lot of problems for my Capistrano Mailer plugin when used with Rails 2.2.2. So essentially at this point, it’s either dig into postfix, or you’re SOL?

NZKoz Sat Jan 10 18:02:38 -0800 2009

@pboling: We’re happy to take a patch to make this optional, roll it into 2-2-stable and have it fixed in 2.2.3.

Want to take a crack at it?

josevalim Sun Jan 11 02:16:08 -0800 2009
bansalakhil Tue Jan 27 23:33:34 -0800 2009

Thanks, darragh.

I am able to make it working by smtpd_use_tls=no

bradgessler Mon Feb 02 10:43:29 -0800 2009

This is killing a lot of rails apps that need to talk to broken SMTP servers. josevalim is on the right track with the patch at http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1731-make-enable_starttls_auto-opt-in-in-actionmailer

josevalim Mon Feb 02 12:16:28 -0800 2009

Yeah, and I really think it should be false by default! As soon people moves to Rails 2.3 and see that their mailers are not working properly, IRC and mailing lists will be very busy. :)

cavneb Tue Feb 03 13:04:53 -0800 2009

This won’t work with 1.8.6 correct? Is this causing woes to anyone else using non-ssl AuthSMTP?