views:

112

answers:

2

I'm kind of confused about how subprocess.Popen works. If anyone has example code that sends email using the subprocess module and sendmail that'd be great.

A: 

One of the first gotchas I encountered with subprocess was the fact that it doesn't take full shell string commands by default.

If you want to do the equivalent of a shell command like:

os.system("echo hello world")

you need to use the shell=True option:

subprocess.Popen("echo hello world", shell=True)
Ross Rogers
It's not really a gotcha: well documented. It's also one of the reasons subprocess rocks. :)
Lars Wirzenius
You're right. It is documented. If you come from a world of Perl's system() call, or ` ` syntax, or Python's os.system then you'll just assume that it's doing a subshell call -- as I did and had to page in the necessary information from Stackoverflow and the python docs.
Ross Rogers
+2  A: 

This doesn't directly answer the question, but given your response to a comment by "DNS", it might solve your problem.

When sending SMTP mail, you need to understand that the "from" and "to" addresses that you pass to the smtplib.sendmail() routine as arguments are not the same thing as what you see in the From: and To: headers in the message when it's received. Those arguments become parameters given to the receiving SMTP mailer, with the "MAIL FROM" and "RCPT TO" commands. This is commonly referred to as the "envelope" of the mail, and the values usually show up in the Received: header lines.

To specify the headers you want, you have to supply them yourself before the body of the message. The smtplib example shows how that's done, in that case with a tuple called "msg" that they prepend to the message body.

Peter Hansen