[PATCH 4 of 4] mail: switch from os to subprocess module when invoking sendmail

Bryan O'Sullivan bos at serpentine.com
Mon Jan 4 23:56:10 CST 2016


# HG changeset patch
# User Bryan O'Sullivan <bos at serpentine.com>
# Date 1451973286 28800
#      Mon Jan 04 21:54:46 2016 -0800
# Node ID 09ce74e1f92f8838bbcd2ac1309519f0448ffbed
# Parent  3838e999a656ccd5644fadac75ed91f15ad22854
mail: switch from os to subprocess module when invoking sendmail

This gets rid of some ugly string mangling; avoids a potentially
unsafe trip through the shell; and (bonus!) reports the correct
exit code if sendmail fails.

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -12,6 +12,7 @@ import os
 import quopri
 import smtplib
 import socket
+import subprocess
 import sys
 import time
 
@@ -161,12 +162,13 @@ def _smtp(ui):
 def _sendmail(ui, sender, recipients, msg):
     '''send mail using sendmail.'''
     program = ui.config('email', 'method', 'smtp')
-    cmdline = '%s -f %s %s' % (program, util.email(sender),
-                               ' '.join(map(util.email, recipients)))
-    ui.note(_('sending mail: %s\n') % cmdline)
-    fp = util.popen(cmdline, 'w')
-    fp.write(msg)
-    ret = fp.close()
+    cmdline = ([program, '-f', util.email(sender)] +
+               [util.email(r) for r in recipients])
+    ui.note(_('sending mail: %s\n') % ' '.join(util.shellquote(c)
+                                               for c in cmdline))
+    p = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
+    p.communicate(msg)
+    ret = p.wait()
     if ret:
         raise error.Abort('%s %s' % (
             os.path.basename(program.split(None, 1)[0]),
diff --git a/tests/test-patchbomb.t b/tests/test-patchbomb.t
--- a/tests/test-patchbomb.t
+++ b/tests/test-patchbomb.t
@@ -2860,7 +2860,7 @@ single rev, and introduce a deliberate f
   
   sending [PATCH] test ...
   sending mail: $TESTTMP/t2/pretendmail.sh -f test foo
-  abort: pretendmail.sh exited with status 19968
+  abort: pretendmail.sh exited with status 78
   [255]
 Test pull url header
 =================================


More information about the Mercurial-devel mailing list