Incognito
Incognito

Reputation: 1953

anyone have example python code that sends mail using sendmail and subprocess?

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.

Upvotes: 0

Views: 830

Answers (2)

Peter Hansen
Peter Hansen

Reputation: 22047

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.

Upvotes: 2

Ross Rogers
Ross Rogers

Reputation: 24228

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)

Upvotes: 0

Related Questions