Title: Send Mail via python
Last Modified: 04.27.2008
This isn't so bad really. I use the smtplib module. I import the module, then since I will be using this via CGI I connect to localhost. So here it is.
#!/usr/bin/env python
import smtplib
import time
letterMachine = smtplib.SMTP()
letterMachine.connect('localhost')
sender = 'matt@mysite.com'
recipient = 'who@getsit.com'
messageHeader ="From: \"Matt\" <matt@mysite.com>\r\n"
messageHeader += "To: \"Friend\" <who@getsit.com>\r\n"
messageHeader += "Subject: Letter From Matt \r\n"
messageHeader += "Date: %s\r\n"%time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
messageHeader += "Content-Type: text/html\r\n"
messageBody = "\r\n This is where you place the messsage content.\r\n\r\n"
msg = messageHeader + messageBody
letterMachine.sendmail(sender, recipient, msg)
letterMachine.quit()
Thats it, a simple message header To let you send e-mail with a subject, to/from and date. Note the to/from do not need to be the same as the one that it is actually being sent to/from.

