How to Send Email with Python without an SMTP Account

This some web page provides a nice example of using SendMail through Python. Most of your Unix web hosting platforms will have SendMail installed, so you’re good to go:

SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: cary@ratatosk.org\\n")
p.write("Subject: test\\n")
p.write("\\n") # blank line separating headers from body
p.write("Some text\\n")
p.write("some more text\\n")
sts = p.close()
if sts != 0:
    print "Sendmail exit status", sts

2 Responses to “How to Send Email with Python without an SMTP Account”

  1. Martin says:

    The backslashes before the trailing n’s in the strings seem to be missing.

  2. Good catch. Should be fixed now, thanks.