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

Christian Ebert blacktrash at gmx.net
Mon Jan 28 02:08:32 CST 2008


* Christian Ebert on Monday, January 28, 2008 at 08:06:27 +0000
> * 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.

Sorry, wrong patch file.

c



# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1201303858 0
# Node ID e294cdf2b1f9d39eb56608dcf522bf6f24ba3a09
# Parent  ed4d55c2366f1bd5fbb6b7ce39dff7fe959802b7
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
@@ -381,6 +381,7 @@
     parent = None
 
     sender_addr = email.Utils.parseaddr(sender)[1]
+    sendmail = None
     for m in msgs:
         try:
             m['Message-Id'] = genmsgid(m['X-Mercurial-Node'])
@@ -425,10 +426,12 @@
             fp.write('\n\n')
             fp.close()
         else:
+            if not sendmail:
+                sendmail = mail.connect(ui)
             ui.status('Sending ', m['Subject'], ' ...\n')
             # Exim does not remove the Bcc field
             del m['Bcc']
-            mail.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,46 +35,47 @@
         ui.note(_('(authenticating to mail server as %s)\n') %
                   (username))
         s.login(username, password)
-    return s
 
-class _sendmail(object):
+    def send(sender, recipients, msg):
+        try:
+            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))
+        except smtplib.SMTPException, inst:
+            raise util.Abort(inst)
+
+    return send
+
+def _sendmail(ui):
     '''send mail using sendmail.'''
+    program = ui.config('email', 'method')
 
-    def __init__(self, ui, program):
-        self.ui = ui
-        self.program = program
-
-    def sendmail(self, sender, recipients, msg):
-        cmdline = '%s -f %s %s' % (
-            self.program, templater.email(sender),
-            ' '.join(map(templater.email, recipients)))
-        self.ui.note(_('sending mail: %s\n') % cmdline)
+    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(self.program.split(None, 1)[0]),
+                os.path.basename(program.split(None, 1)[0]),
                 util.explain_exit(ret)[0]))
 
+    return send
+
 def connect(ui):
-    '''make a mail connection. object returned has one method, sendmail.
+    '''make a mail connection. return a function to send mail.
     call as sendmail(sender, list-of-recipients, msg).'''
-
-    method = ui.config('email', 'method', 'smtp')
-    if method == 'smtp':
+    if ui.config('email', 'method', 'smtp') == 'smtp':
         return _smtp(ui)
-
-    return _sendmail(ui, method)
+    return _sendmail(ui)
 
 def sendmail(ui, sender, recipients, msg):
-    try:
-        return connect(ui).sendmail(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