[PATCH] mail: fix regression introduced in dc6ed2736c81 (issue #943)

Christian Ebert blacktrash at gmx.net
Mon Jan 28 02:06:27 CST 2008


* Christian Ebert on Saturday, January 26, 2008 at 09:18:39 +0000
> I can also provide a patch against rolled back crew.

"reverted", of course; here's the patch.

c



# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1201303858 0
# Node ID 98a9582da440c56393b1ee55bac33c43af34b1bf
# Parent  b75105de8573831f4bfef10556e90e7f883ae8d5
mail: fix regression introduced in dc6ed2736c81 (issue #943)

- make separation of python smtplib and external sendmail call
  more transparent
- only catch smtplib errors when we are actually using it

diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -431,7 +431,7 @@
             ui.status('Sending ', m['Subject'], ' ...\n')
             # Exim does not remove the Bcc field
             del m['Bcc']
-            sendmail(ui, sender, to + bcc + cc, m.as_string(0))
+            sendmail(sender, to + bcc + cc, m.as_string(0))
 
 cmdtable = {
     "email":
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -9,8 +9,7 @@
 import os, smtplib, templater, util, socket
 
 def _smtp(ui):
-    '''send mail using smtp.'''
-
+    '''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)
     mailhost = ui.config('smtp', 'host')
@@ -36,33 +35,10 @@
         ui.note(_('(authenticating to mail server as %s)\n') %
                   (username))
         s.login(username, password)
-    return s
 
-def _sendmail(ui, sender, recipients, msg):
-    '''send mail using sendmail.'''
-    program = ui.config('email', 'method')
-    cmdline = '%s -f %s %s' % (program, templater.email(sender),
-                               ' '.join(map(templater.email, recipients)))
-    ui.note(_('sending mail: %s\n') % cmdline)
-    fp = os.popen(cmdline, 'w')
-    fp.write(msg)
-    ret = fp.close()
-    if ret:
-        raise util.Abort('%s %s' % (
-            os.path.basename(program.split(None, 1)[0]),
-            util.explain_exit(ret)[0]))
-
-def connect(ui):
-    '''make a mail connection. return a function to send mail.
-    call as sendmail(sender, list-of-recipients, msg).'''
-
-    func =  _sendmail
-    if ui.config('email', 'method', 'smtp') == 'smtp':
-        func = _smtp(ui)
-
-    def send(ui, sender, recipients, msg):
+    def send(sender, recipients, msg):
         try:
-            return func.sendmail(sender, recipients, msg)
+            return s.sendmail(sender, recipients, msg)
         except smtplib.SMTPRecipientsRefused, inst:
             recipients = [r[1] for r in inst.recipients.values()]
             raise util.Abort('\n' + '\n'.join(recipients))
@@ -71,15 +47,35 @@
 
     return send
 
+def _sendmail(ui):
+    '''send mail using sendmail.'''
+    program = ui.config('email', 'method')
+
+    def send(sender, recipients, msg):
+        cmdline = '%s -f %s %s' % (program, templater.email(sender),
+                                   ' '.join(map(templater.email, recipients)))
+        ui.note(_('sending mail: %s\n') % cmdline)
+        fp = os.popen(cmdline, 'w')
+        fp.write(msg)
+        ret = fp.close()
+        if ret:
+            raise util.Abort('%s %s' % (
+                os.path.basename(program.split(None, 1)[0]),
+                util.explain_exit(ret)[0]))
+
+    return send
+
+def connect(ui):
+    '''make a mail connection. return a function to send mail.
+    call as sendmail(sender, list-of-recipients, msg).'''
+    if ui.config('email', 'method', 'smtp') == 'smtp':
+        return _smtp(ui)
+    return _sendmail(ui)
+
 def sendmail(ui, sender, recipients, msg):
-    try:
-        send = connect(ui)
-        return send(sender, recipients, msg)
-    except smtplib.SMTPRecipientsRefused, inst:
-        recipients = [r[1] for r in inst.recipients.values()]
-        raise util.Abort('\n' + '\n'.join(recipients))
-    except smtplib.SMTPException, inst:
-        raise util.Abort(inst)
+    '''shortcut to send single message.'''
+    send = connect(ui)
+    return send(sender, recipients, msg)
 
 def validateconfig(ui):
     '''determine if we have enough config data to try sending email.'''


More information about the Mercurial-devel mailing list