Python: Send SMTP message (email)

{red}This works only on Linux/Unix/Solaris{red}

import os
# adjust MAIl as per your system environment
MAIL = "/usr/sbin/sendmail"
# get the email message from a file
f = open('mssg.txt', 'r')
mssg =
# open a pipe to the mail program and
# write the data to the pipe
p = os.popen("%s -t" % MAIL, 'w')
exitcode = p.close()
if exitcode:
        print "Exit code: %s" % exitcode
  • The message has to be a file
Subject: My Test email

Here is the message body.
This concludes our test.

- sig

pythonsmtp.txt · Last modified: 2014/10/25 21:52 (external edit)
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0