[PATCH] smtp: fix for server doesn't support starttls extension

Zhigang Wang zhigang.x.wang at oracle.com
Mon Dec 20 03:01:32 CST 2010


 doc/hgrc.5.txt    |   4 ++--
 mercurial/mail.py |  19 +++++++++++++------
 2 files changed, 15 insertions(+), 8 deletions(-)


# HG changeset patch
# User Zhigang Wang <zhigang.x.wang at oracle.com>
# Date 1292835414 -28800
# Node ID 66f3998702bf21ffb1fdba02fbbd240a52c9b988
# Parent  5dac0d04b838d599acdc5db9ee0b7de51de15537
smtp: fix for server doesn't support starttls extension

Currently we only support enabling TLS by using SMTP STARTTLS extension. But
not all the servers support it.

With this patch, user can choose which way to enable TLS:

* Default:

      tls = none
      port = 25

* To use STARTTLS:

      tls = starttls
      port = 465

* To use SMTP over SSL:

      tls = smtps
      port = 465

To keep backward compatibility, when tls = true, we use STARTTLS to enable TLS.

Signed-off-by: Zhigang Wang <w1z2g3 at gmail.com>

diff -r 5dac0d04b838 -r 66f3998702bf doc/hgrc.5.txt
--- a/doc/hgrc.5.txt	Wed Dec 08 13:12:12 2010 -0600
+++ b/doc/hgrc.5.txt	Mon Dec 20 16:56:54 2010 +0800
@@ -709,8 +709,8 @@
 ``port``
     Optional. Port to connect to on mail server. Default: 25.
 ``tls``
-    Optional. Whether to connect to mail server using TLS. True or
-    False. Default: False.
+    Optional. Method to enable TLS when connecting to mail server: starttls,
+    smtps or none. Default: none.
 ``username``
     Optional. User name for authenticating with the SMTP server.
     Default: none.
diff -r 5dac0d04b838 -r 66f3998702bf mercurial/mail.py
--- a/mercurial/mail.py	Wed Dec 08 13:12:12 2010 -0600
+++ b/mercurial/mail.py	Mon Dec 20 16:56:54 2010 +0800
@@ -33,7 +33,17 @@
 def _smtp(ui):
     '''build an smtp connection and return a function to send mail'''
     local_hostname = ui.config('smtp', 'local_hostname')
-    s = smtplib.SMTP(local_hostname=local_hostname)
+    tls = ui.config('smtp', 'tls')
+    # backward compatible: when tls = true, we use starttls.
+    starttls = tls == 'starttls' or util.parsebool(tls)
+    smtps = tls == 'smtps'
+    if (starttls or smtps) and not hasattr(socket, 'ssl'):
+        raise util.Abort(_("can't use TLS: Python SSL support not installed"))
+    if smtps:
+        ui.note(_('(using smtps)\n'))
+        s = smtplib.SMTP_SSL(local_hostname=local_hostname)
+    else:
+        s = smtplib.SMTP(local_hostname=local_hostname)
     mailhost = ui.config('smtp', 'host')
     if not mailhost:
         raise util.Abort(_('smtp.host not configured - cannot send mail'))
@@ -41,11 +51,8 @@
     ui.note(_('sending mail: smtp host %s, port %s\n') %
             (mailhost, mailport))
     s.connect(host=mailhost, port=mailport)
-    if ui.configbool('smtp', 'tls'):
-        if not hasattr(socket, 'ssl'):
-            raise util.Abort(_("can't use TLS: Python SSL support "
-                               "not installed"))
-        ui.note(_('(using tls)\n'))
+    if starttls:
+        ui.note(_('(using starttls)\n'))
         s.ehlo()
         s.starttls()
         s.ehlo()


More information about the Mercurial-devel mailing list