Sending mail via sendmail from python

If I want to send mail not via SMTP, but through sendmail, is there a library for python that encapsulates this process?

Even better, is there a good library that abstracts out the entire sendmail -versus-smtp selection?

I will run this script on a bunch of unix hosts, only some of which listen on localhost: 25; some of them are part of embedded systems and cannot be configured to accept SMTP.

As part of Good Practice, I would really like the library itself to take care of the vulnerabilities associated with nesting headers, so just drop the line to popen('/usr/bin/sendmail', 'w') little closer to the text than I would like .

If the answer is "go write library", let it be like this :-)

+56
python email sendmail
Sep 16 '08 at 15:46
source share
7 answers

Entering a header is not a factor in how you send mail, it is a factor in how you create mail. Check your email , build mail with this, serialize it and send to /usr/sbin/sendmail using subprocess :

 from email.mime.text import MIMEText from subprocess import Popen, PIPE msg = MIMEText("Here is the body of my message") msg["From"] = "me@example.com" msg["To"] = "you@example.com" msg["Subject"] = "This is the subject." p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) p.communicate(msg.as_string()) 
+95
Sep 16 '08 at 16:12
source share
— -

This is a simple python function that uses unix sendmail to deliver mail.

 def sendMail(): sendmail_location = "/usr/sbin/sendmail" # sendmail location p = os.popen("%s -t" % sendmail_location, "w") p.write("From: %s\n" % "from@somewhere.com") p.write("To: %s\n" % "to@somewhereelse.com") p.write("Subject: thesubject\n") p.write("\n") # blank line separating headers from body p.write("body of the mail") status = p.close() if status != 0: print "Sendmail exit status", status 
+30
Sep 16 '08 at 15:51
source share

Jim answered me not in Python 3.4. I had to add the optional argument universal_newlines=True to subrocess.Popen()

 from email.mime.text import MIMEText from subprocess import Popen, PIPE msg = MIMEText("Here is the body of my message") msg["From"] = "me@example.com" msg["To"] = "you@example.com" msg["Subject"] = "This is the subject." p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True) p.communicate(msg.as_string()) 

Without universal_newlines=True I get

 TypeError: 'str' does not support the buffer interface 
+6
Sep 19 '15 at 21:38
source share

It's quite simple to use the sendmail command from Python using os.popen

Personally, I didn’t write scripts myself, I think it’s better to use the SMTP protocol, since installation on windows does not require the installation of, say, a sendmail clone.

https://docs.python.org/library/smtplib.html

+3
Sep 16 '08 at 15:49
source share

This question is very old, but it is worth noting that there is a message and email system called Marrow Mailer (formerly TurboMail) that was available before this message was asked.

It is now being ported to support Python 3 and is being updated as part of Marrow .

+3
Apr 04 '11 at 23:24
source share

I just searched the same thing and found a good example on the Python website: http://docs.python.org/2/library/email-examples.html

From the mentioned site:

 # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. fp = open(textfile, 'rb') # Create a text/plain message msg = MIMEText(fp.read()) fp.close() # me == the sender email address # you == the recipient email address msg['Subject'] = 'The contents of %s' % textfile msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('localhost') s.sendmail(me, [you], msg.as_string()) s.quit() 

Please note that this requires that you correctly configure sendmail / mailx to accept connections on "localhost". This works on Mac, Ubuntu and Redhat servers by default, but you can double-check if you have any problems.

-3
Jun 27 '13 at 13:49 on
source share

The simplest answer is smtplib, you can find documents on it here .

All you have to do is configure local sendmail to accept a connection to localhost, which it probably already performs by default. Of course, you are still using SMTP for transmission, but this is local sendmail, which is basically the same as using the command line tool.

-6
Sep 16 '08 at 15:49
source share



All Articles