Python: Send SMTP message (email)

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

#!/usr/bin/python
 
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 = f.read()
f.close()
 
# open a pipe to the mail program and
# write the data to the pipe
p = os.popen("%s -t" % MAIL, 'w')
p.write(mssg)
exitcode = p.close()
if exitcode:
        print "Exit code: %s" % exitcode
  • The message has to be a file
To: mnain@sender.com
From: sysmon@receiver.net
Subject: My Test email

Here is the message body.
This concludes our test.
Fini.

- sig

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