{"thread":{"id":"13945","subject":"[StGIT PATCH] Allow e-mails to be sent with the Unix sendmail tool","startedAt":"2008-06-14T07:28:24Z","lastAt":"2008-06-14T09:05:04Z","messageCount":2,"participants":["Catalin Marinas","Karl Hasselström"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"79825","messageId":"20080614072824.7899.42222.stgit@localhost.localdomain","threadId":"13945","inReplyTo":null,"subject":"[StGIT PATCH] Allow e-mails to be sent with the Unix sendmail tool","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@gmail.com","sentAt":"2008-06-14T07:28:24Z","receivedAt":"2008-06-14T07:28:24Z","isPatch":true,"sender":{"key":"catalin.marinas@gmail.com","avatar":null},"body":"If the stgit.smtpserver configuration option does not have a host:port\nformat, it is assumed to be an external tool. For example, to use\nsendmail just set this variable to \"/usr/sbin/sendmail -t -i\" (see the\nexamples/gitconfig file).\n\nSigned-off-by: Catalin Marinas <catalin.marinas@gmail.com>\n---\n\n examples/gitconfig     |    1 +\n stgit/commands/mail.py |   29 ++++++++++++++++++++++++-----\n stgit/config.py        |    4 ++--\n 3 files changed, 27 insertions(+), 7 deletions(-)\n\ndiff --git a/examples/gitconfig b/examples/gitconfig\nindex c16f786..28d94af 100644\n--- a/examples/gitconfig\n+++ b/examples/gitconfig\n@@ -19,6 +19,7 @@\n \t#autoresolved = no\n \n \t# SMTP server for sending patches\n+\t#smtpserver = /usr/sbin/sendmail -t -i\n \t#smtpserver = localhost:25\n \n \t# Set to 'yes' to use SMTP over TLS\ndiff --git a/stgit/commands/mail.py b/stgit/commands/mail.py\nindex b4d4e18..67b94c1 100644\n--- a/stgit/commands/mail.py\n+++ b/stgit/commands/mail.py\n@@ -24,6 +24,7 @@ from stgit.utils import *\n from stgit.out import *\n from stgit import stack, git, version, templates\n from stgit.config import config\n+from stgit.run import Run\n \n \n help = 'send a patch or series of patches by e-mail'\n@@ -184,8 +185,14 @@ def __parse_addresses(msg):\n \n     return (from_addr_list[0], to_addr_list)\n \n-def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep,\n-                   smtpuser, smtppassword, use_tls):\n+def __send_message_sendmail(sendmail, msg):\n+    \"\"\"Send the message using the sendmail command.\n+    \"\"\"\n+    cmd = sendmail.split()\n+    Run(*cmd).raw_input(msg).discard_output()\n+\n+def __send_message_smtp(smtpserver, from_addr, to_addr_list, msg,\n+                        smtpuser, smtppassword, use_tls):\n     \"\"\"Send the message using the given SMTP server\n     \"\"\"\n     try:\n@@ -207,13 +214,25 @@ def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep,\n         result = s.sendmail(from_addr, to_addr_list, msg)\n         if len(result):\n             print \"mail server refused delivery for the following recipients: %s\" % result\n-        # give recipients a chance of receiving patches in the correct order\n-        time.sleep(sleep)\n     except Exception, err:\n         raise CmdException, str(err)\n \n     s.quit()\n \n+def __send_message(smtpserver, from_addr, to_addr_list, msg,\n+                   sleep, smtpuser, smtppassword, use_tls):\n+    \"\"\"Message sending dispatcher.\n+    \"\"\"\n+    if smtpserver.startswith('/'):\n+        # Use the sendmail tool\n+        __send_message_sendmail(smtpserver, msg)\n+    else:\n+        # Use the SMTP server (we have host and port information)\n+        __send_message_smtp(smtpserver, from_addr, to_addr_list, msg,\n+                            smtpuser, smtppassword, use_tls)\n+    # give recipients a chance of receiving patches in the correct order\n+    time.sleep(sleep)\n+\n def __build_address_headers(msg, options, extra_cc = []):\n     \"\"\"Build the address headers and check existing headers in the\n     template.\n@@ -338,7 +357,7 @@ def __build_cover(tmpl, patches, msg_id, options):\n             prefix_str = confprefix + ' '\n         else:\n             prefix_str = ''\n-        \n+\n     total_nr_str = str(len(patches))\n     patch_nr_str = '0'.zfill(len(total_nr_str))\n     if len(patches) > 1:\ndiff --git a/stgit/config.py b/stgit/config.py\nindex 9bfdd52..fcf3a5d 100644\n--- a/stgit/config.py\n+++ b/stgit/config.py\n@@ -29,7 +29,7 @@ class GitConfigException(StgException):\n class GitConfig:\n     __defaults={\n         'stgit.autoresolved':\t'no',\n-        'stgit.smtpserver':\t'localhost:25',\n+        'stgit.smtpserver':\t'/usr/sbin/sendmail -t -i',\n         'stgit.smtpdelay':\t'5',\n         'stgit.pullcmd':\t'git pull',\n         'stgit.fetchcmd':\t'git fetch',\n@@ -102,7 +102,7 @@ class GitConfig:\n             if m:\n                 result.append(m.group(1))\n         return result\n-        \n+\n config=GitConfig()\n \n def config_setup():\n"},{"id":"79834","messageId":"20080614090504.GB14282@diana.vm.bytemark.co.uk","threadId":"13945","inReplyTo":"20080614072824.7899.42222.stgit@localhost.localdomain","subject":"Re: [StGIT PATCH] Allow e-mails to be sent with the Unix sendmail tool","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2008-06-14T09:05:04Z","receivedAt":"2008-06-14T09:05:04Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"On 2008-06-14 08:28:24 +0100, Catalin Marinas wrote:\n\n> If the stgit.smtpserver configuration option does not have a\n> host:port format, it is assumed to be an external tool. For example,\n> to use sendmail just set this variable to \"/usr/sbin/sendmail -t -i\"\n> (see the examples/gitconfig file).\n\nLooks good, except that the new feature (and the fact that it's\nmagically triggered if the smtpserver starts with a /) is not\ndocumented. Maybe add something like this to the patch:\n\ndiff --git a/stgit/commands/mail.py b/stgit/commands/mail.py\nindex 4027170..2b1b92c 100644\n--- a/stgit/commands/mail.py\n+++ b/stgit/commands/mail.py\n@@ -31,10 +31,13 @@ usage = r\"\"\"%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]\n \n Send a patch or a range of patches by e-mail using the SMTP server\n specified by the 'stgit.smtpserver' configuration option, or the\n-'--smtp-server' command line option. The From address and the e-mail\n-format are generated from the template file passed as argument to\n-'--template' (defaulting to '.git/patchmail.tmpl' or\n-'~/.stgit/templates/patchmail.tmpl' or\n+'--smtp-server' command line option. (You may give the absolute path\n+to sendmail instead, followed by any sendmail options you wish to\n+use.)\n+\n+The From address and the e-mail format are generated from the template\n+file passed as argument to '--template' (defaulting to\n+'.git/patchmail.tmpl' or '~/.stgit/templates/patchmail.tmpl' or\n '/usr/share/stgit/templates/patchmail.tmpl'). A patch can be sent as\n attachment using the --attach option in which case the 'mailattch.tmpl'\n template will be used instead of 'patchmail.tmpl'.\n@@ -133,8 +136,10 @@ options = [make_option('-a', '--all',\n                        help = 'sleep for SECONDS between e-mails sending'),\n            make_option('--refid',\n                        help = 'use REFID as the reference id'),\n-           make_option('--smtp-server', metavar = 'HOST[:PORT]',\n-                       help = 'SMTP server to use for sending mail'),\n+           make_option('--smtp-server',\n+                       metavar = 'HOST[:PORT] or \"/path/to/sendmail -t -i\"',\n+                       help = ('SMTP server (or sendmail program) to use for'\n+                               ' sending mail')),\n            make_option('-u', '--smtp-user', metavar = 'USER',\n                        help = 'username for SMTP authentication'),\n            make_option('-p', '--smtp-password', metavar = 'PASSWORD',\n\n-- \nKarl Hasselström, kha@treskal.com\n      www.treskal.com/kalle\n"}]}