mercurial/mail.py
changeset 5973 ea77f6f77514
parent 5947 528c986f0162
child 5975 75d9fe70c654
--- a/mercurial/mail.py	Tue Jan 22 23:07:23 2008 +0100
+++ b/mercurial/mail.py	Thu Jan 31 14:44:19 2008 -0600
@@ -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,42 @@
         ui.note(_('(authenticating to mail server as %s)\n') %
                   (username))
         s.login(username, password)
-    return s
 
-class _sendmail(object):
-    '''send mail using sendmail.'''
+    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)
 
-    def __init__(self, ui, program):
-        self.ui = ui
-        self.program = program
+    return send
 
-    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)
-        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]),
-                util.explain_exit(ret)[0]))
+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. 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 lambda s, r, m: _sendmail(ui, s, r, m)
 
 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)
+    send = connect(ui)
+    return send(sender, recipients, msg)
 
 def validateconfig(ui):
     '''determine if we have enough config data to try sending email.'''