chiark / gitweb /
Add support for SMTP over Transport Layer Security (TLS)
authorPavel Roskin <proski@gnu.org>
Mon, 20 Aug 2007 21:36:00 +0000 (22:36 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Mon, 20 Aug 2007 21:36:32 +0000 (22:36 +0100)
Signed-off-by: Pavel Roskin <proski@gnu.org>
examples/gitconfig
stgit/commands/mail.py

index bbb943f53796d3c886b9e75d0505d03d89733790..3abbe6a7f960be979089f83f47c0ff1229afbcc2 100644 (file)
@@ -21,6 +21,7 @@
        #smtpserver = localhost:25
        #smtpuser = username
        #smtppassword = password
+       #smtptls = no
 
        # delay between messages in seconds (defaults to 5)
        #smtpdelay = 5
index cb8dc74bf28532adacbedb4edd63c2edc6c5af60..7ed5c2799ec56b24cffc0687df49b467e47ff233 100644 (file)
@@ -15,7 +15,7 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-import sys, os, re, time, datetime, smtplib
+import sys, os, re, time, datetime, socket, smtplib
 import email, email.Utils, email.Header
 from optparse import OptionParser, make_option
 
@@ -53,7 +53,8 @@ replies to a different e-mail by using the '--refid' option.
 
 SMTP authentication is also possible with '--smtp-user' and
 '--smtp-password' options, also available as configuration settings:
-'smtpuser' and 'smtppassword'.
+'smtpuser' and 'smtppassword'. TLS encryption can be enabled by
+'--smtp-tls' option and 'smtptls' setting.
 
 The patch e-mail template accepts the following variables:
 
@@ -121,6 +122,9 @@ options = [make_option('-a', '--all',
                        help = 'username for SMTP authentication'),
            make_option('-p', '--smtp-password', metavar = 'PASSWORD',
                        help = 'username for SMTP authentication'),
+           make_option('-T', '--smtp-tls',
+                       help = 'use SMTP with TLS encryption',
+                       action = 'store_true'),
            make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one'),
            make_option('-O', '--diff-opts',
@@ -165,7 +169,7 @@ def __parse_addresses(msg):
     return (from_addr_list[0], to_addr_list)
 
 def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep,
-                   smtpuser, smtppassword):
+                   smtpuser, smtppassword, use_tls):
     """Send the message using the given SMTP server
     """
     try:
@@ -177,6 +181,11 @@ def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep,
     try:
         if smtpuser and smtppassword:
             s.ehlo()
+            if use_tls:
+                if not hasattr(socket, 'ssl'):
+                    raise CmdException,  "cannot use TLS - no SSL support in Python"
+                s.starttls()
+                s.ehlo()
             s.login(smtpuser, smtppassword)
 
         result = s.sendmail(from_addr, to_addr_list, msg)
@@ -479,11 +488,14 @@ def func(parser, options, args):
 
     smtppassword = options.smtp_password or config.get('stgit.smtppassword')
     smtpuser = options.smtp_user or config.get('stgit.smtpuser')
+    smtpusetls = options.smtp_tls or config.get('stgit.smtptls') == 'yes'
 
     if (smtppassword and not smtpuser):
         raise CmdException, 'SMTP password supplied, username needed'
     if (smtpuser and not smtppassword):
         raise CmdException, 'SMTP username supplied, password needed'
+    if (smtpusetls and not smtpuser):
+        raise CmdException, 'SMTP over TLS requested, username needed'
 
     total_nr = len(patches)
     if total_nr == 0:
@@ -527,7 +539,7 @@ def func(parser, options, args):
         else:
             out.start('Sending the cover message')
             __send_message(smtpserver, from_addr, to_addr_list, msg_string,
-                           sleep, smtpuser, smtppassword)
+                           sleep, smtpuser, smtppassword, smtpusetls)
             out.done()
 
     # send the patches
@@ -555,5 +567,5 @@ def func(parser, options, args):
         else:
             out.start('Sending patch "%s"' % p)
             __send_message(smtpserver, from_addr, to_addr_list, msg_string,
-                           sleep, smtpuser, smtppassword)
+                           sleep, smtpuser, smtppassword, smtpusetls)
             out.done()