<p>If I want to send mail not via SMTP, but rather via sendmail, is there a library for python that encapsulates this process?</p>

<p>Better yet, is there a good library that abstracts the whole 'sendmail -versus- smtp' choice?</p>

<p>I'll be running this script on a bunch of unix hosts, only some of which are listening on localhost:25; a few of these are part of embedded systems and can't be set up to accept SMTP.</p>

<p>As part of Good Practice, I'd really like to have the library take care of header injection vulnerabilities itself -- so just dumping a string to <code>popen('/usr/bin/sendmail', 'w')</code> is a little closer to the metal than I'd like.</p>

<p>If the answer is 'go write a library,' so be it ;-)</p>

[{'answer_id': 73807, 'author': 'tovare', 'author_id': 12677, 'author_profile': 'https://Stackoverflow.com/users/12677', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s quite common to just use the sendmail command from Python using os.popen</p>\n\n<p>Personally, for scripts i didn\'t write myself, I think just using the SMTP-protocol is better, since it wouldn\'t require installing say an sendmail clone to run on windows.</p>\n\n<p><a href="https://docs.python.org/library/smtplib.html" rel="nofollow noreferrer">https://docs.python.org/library/smtplib.html</a></p>\n'}, {'answer_id': 73811, 'author': 'Frank Wiles', 'author_id': 12568, 'author_profile': 'https://Stackoverflow.com/users/12568', 'pm_score': -1, 'selected': False, 'text': '<p>The easiest answer is the smtplib, you can find docs on it <a href="http://docs.python.org/lib/SMTP-example.html" rel="nofollow noreferrer">here</a>. </p>\n\n<p>All you need to do is configure your local sendmail to accept connection from localhost, which it probably already does by default.  Sure, you\'re still using SMTP for the transfer, but it\'s the local sendmail, which is basically the same as using the commandline tool. </p>\n'}, {'answer_id': 73844, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 5, 'selected': False, 'text': '<p>This is a simple python function that uses the unix sendmail to deliver a mail.</p>\n\n<pre><code>def sendMail():\n    sendmail_location = "/usr/sbin/sendmail" # sendmail location\n    p = os.popen("%s -t" % sendmail_location, "w")\n    p.write("From: %s\\n" % "from@somewhere.com")\n    p.write("To: %s\\n" % "to@somewhereelse.com")\n    p.write("Subject: thesubject\\n")\n    p.write("\\n") # blank line separating headers from body\n    p.write("body of the mail")\n    status = p.close()\n    if status != 0:\n           print "Sendmail exit status", status\n</code></pre>\n'}, {'answer_id': 74084, 'author': 'Jim', 'author_id': 8427, 'author_profile': 'https://Stackoverflow.com/users/8427', 'pm_score': 8, 'selected': True, 'text': '<p>Header injection isn\'t a factor in how you send the mail, it\'s a factor in how you construct the mail.  Check the <a href="https://docs.python.org/2/library/email.html" rel="noreferrer">email</a> package, construct the mail with that, serialise it, and send it to <code>/usr/sbin/sendmail</code> using the <a href="https://docs.python.org/2/library/subprocess.html" rel="noreferrer">subprocess</a> module:</p>\n<pre><code>import sys\nfrom email.mime.text import MIMEText\nfrom subprocess import Popen, PIPE\n\n\nmsg = MIMEText(&quot;Here is the body of my message&quot;)\nmsg[&quot;From&quot;] = &quot;me@example.com&quot;\nmsg[&quot;To&quot;] = &quot;you@example.com&quot;\nmsg[&quot;Subject&quot;] = &quot;This is the subject.&quot;\np = Popen([&quot;/usr/sbin/sendmail&quot;, &quot;-t&quot;, &quot;-oi&quot;], stdin=PIPE)\n# Both Python 2.X and 3.X\np.communicate(msg.as_bytes() if sys.version_info &gt;= (3,0) else msg.as_string()) \n\n# Python 2.X\np.communicate(msg.as_string())\n\n# Python 3.X\np.communicate(msg.as_bytes())\n</code></pre>\n'}, {'answer_id': 5545462, 'author': 'amcgregor', 'author_id': 211827, 'author_profile': 'https://Stackoverflow.com/users/211827', 'pm_score': 3, 'selected': False, 'text': '<p>This question is very old, but it\'s worthwhile to note that there is a message construction and e-mail delivery system called <a href="https://github.com/marrow/mailer" rel="nofollow noreferrer">Marrow Mailer</a> (previously TurboMail) which has been available since before this message was asked.</p>\n\n<p>It\'s now being ported to support Python 3 and updated as part of the <a href="https://github.com/marrow" rel="nofollow noreferrer">Marrow</a> suite.</p>\n'}, {'answer_id': 17345007, 'author': 'elec3647', 'author_id': 1842416, 'author_profile': 'https://Stackoverflow.com/users/1842416', 'pm_score': -1, 'selected': False, 'text': '<p>I was just searching around for the same thing and found a good example on the Python website: <a href="http://docs.python.org/2/library/email-examples.html" rel="nofollow">http://docs.python.org/2/library/email-examples.html</a></p>\n\n<p>From the site mentioned:</p>\n\n<pre><code># Import smtplib for the actual sending function\nimport smtplib\n\n# Import the email modules we\'ll need\nfrom email.mime.text import MIMEText\n\n# Open a plain text file for reading.  For this example, assume that\n# the text file contains only ASCII characters.\nfp = open(textfile, \'rb\')\n# Create a text/plain message\nmsg = MIMEText(fp.read())\nfp.close()\n\n# me == the sender\'s email address\n# you == the recipient\'s email address\nmsg[\'Subject\'] = \'The contents of %s\' % textfile\nmsg[\'From\'] = me\nmsg[\'To\'] = you\n\n# Send the message via our own SMTP server, but don\'t include the\n# envelope header.\ns = smtplib.SMTP(\'localhost\')\ns.sendmail(me, [you], msg.as_string())\ns.quit()\n</code></pre>\n\n<p>Note that this requires that you have sendmail/mailx set up correctly to accept connections on "localhost". This works on my Mac, Ubuntu and Redhat servers by default, but you may want to double-check if you run into any issues.</p>\n'}, {'answer_id': 32673496, 'author': 'MEI', 'author_id': 5351807, 'author_profile': 'https://Stackoverflow.com/users/5351807', 'pm_score': 4, 'selected': False, 'text': '<p>Jim\'s answer did not work for me in Python 3.4. I had to add an additional <code>universal_newlines=True</code> argument to <code>subrocess.Popen()</code></p>\n\n<pre><code>from email.mime.text import MIMEText\nfrom subprocess import Popen, PIPE\n\nmsg = MIMEText("Here is the body of my message")\nmsg["From"] = "me@example.com"\nmsg["To"] = "you@example.com"\nmsg["Subject"] = "This is the subject."\np = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)\np.communicate(msg.as_string())\n</code></pre>\n\n<p>Without the <code>universal_newlines=True</code> I get</p>\n\n<pre><code>TypeError: \'str\' does not support the buffer interface\n</code></pre>\n'}, {'answer_id': 61923564, 'author': 'Robin Stewart', 'author_id': 7488171, 'author_profile': 'https://Stackoverflow.com/users/7488171', 'pm_score': 3, 'selected': False, 'text': '<p><strong>Python 3.5+</strong> version:</p>\n\n<pre><code>import subprocess\nfrom email.message import EmailMessage\n\ndef sendEmail(from_addr, to_addrs, msg_subject, msg_body):\n    msg = EmailMessage()\n    msg.set_content(msg_body)\n    msg[\'From\'] = from_addr\n    msg[\'To\'] = to_addrs\n    msg[\'Subject\'] = msg_subject\n\n    sendmail_location = "/usr/sbin/sendmail"\n    subprocess.run([sendmail_location, "-t", "-oi"], input=msg.as_bytes())\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73781', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12779/']