changeset 2292:903ab41ac7eb

allow to send email using sendmail. default is still smtp. update hgrc doc with sendmail info.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Mon, 15 May 2006 10:25:17 -0700
parents 1cad19678b4c
children 3dc6f2501dbc
files doc/hgrc.5.txt hgext/patchbomb.py mercurial/ui.py
diffstat 3 files changed, 69 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/doc/hgrc.5.txt	Mon May 15 09:27:27 2006 -0700
+++ b/doc/hgrc.5.txt	Mon May 15 10:25:17 2006 -0700
@@ -135,6 +135,20 @@
   from;;
     Optional.  Email address to use in "From" header and SMTP envelope
     of outgoing messages.
+  method;;
+    Optional.  Method to use to send email messages.  If value is
+    "smtp" (default), use SMTP (see section "[mail]" for
+    configuration).  Otherwise, use as name of program to run that
+    acts like sendmail (takes "-f" option for sender, list of
+    recipients on command line, message on stdin).  Normally, setting
+    this to "sendmail" or "/usr/sbin/sendmail" is enough to use
+    sendmail to send messages.
+
+  Email example:
+
+    [email]
+    from = Joseph User <joe.user@example.com>
+    method = /usr/sbin/sendmail
 
 extensions::
   Mercurial has an extension mechanism for adding new features. To
--- a/hgext/patchbomb.py	Mon May 15 09:27:27 2006 -0700
+++ b/hgext/patchbomb.py	Mon May 15 10:25:17 2006 -0700
@@ -254,8 +254,6 @@
         else:
             ui.status('Sending ', m['Subject'], ' ...\n')
             mail.sendmail(sender, to + cc, m.as_string(0))
-    if not opts['test'] and not opts['mbox']:
-        mail.close()
 
 cmdtable = {
     'email':
--- a/mercurial/ui.py	Mon May 15 09:27:27 2006 -0700
+++ b/mercurial/ui.py	Mon May 15 10:25:17 2006 -0700
@@ -8,7 +8,8 @@
 import ConfigParser
 from i18n import gettext as _
 from demandload import *
-demandload(globals(), "errno getpass os re smtplib socket sys tempfile util")
+demandload(globals(), "errno getpass os re smtplib socket sys tempfile")
+demandload(globals(), "templater util")
 
 class ui(object):
     def __init__(self, verbose=False, debug=False, quiet=False,
@@ -270,17 +271,56 @@
         return t
 
     def sendmail(self):
-        s = smtplib.SMTP()
-        mailhost = self.config('smtp', 'host')
-        if not mailhost:
-            raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
-        s.connect(host=mailhost, port=int(self.config('smtp', 'port', 25)))
-        if self.configbool('smtp', 'tls'):
-            s.ehlo()
-            s.starttls()
-            s.ehlo()
-        username = self.config('smtp', 'username')
-        password = self.config('smtp', 'password')
-        if username and password:
-            s.login(username, password)
-        return s
+        '''send mail message. object returned has one method, sendmail.
+        call as sendmail(sender, list-of-recipients, msg).'''
+
+        def smtp():
+            '''send mail using smtp.'''
+
+            s = smtplib.SMTP()
+            mailhost = self.config('smtp', 'host')
+            if not mailhost:
+                raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
+            mailport = int(self.config('smtp', 'port', 25))
+            self.note(_('sending mail: smtp host %s, port %s\n') %
+                      (mailhost, mailport))
+            s.connect(host=mailhost, port=mailport)
+            if self.configbool('smtp', 'tls'):
+                self.note(_('(using tls)\n'))
+                s.ehlo()
+                s.starttls()
+                s.ehlo()
+            username = self.config('smtp', 'username')
+            password = self.config('smtp', 'password')
+            if username and password:
+                self.note(_('(authenticating to mail server as %s)\n') %
+                          (username))
+                s.login(username, password)
+            return s
+
+        class sendmail(object):
+            '''send mail using sendmail.'''
+
+            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)
+                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]))
+
+        method = self.config('email', 'method', 'smtp')
+        if method == 'smtp':
+            mail = smtp()
+        else:
+            mail = sendmail(self, method)
+        return mail