December 16, 2009

Python - Send Email With smtplib

This is mostly just for my own reference


sending an email using Python's smtplib:


import smtplib
from email.MIMEText import MIMEText

smtp_server = 'exchangeserver.foo.com'
recipients = ['corey@foo.com',]
sender = 'corey@foo.com'
subject = 'subject goes here'
msg_text = 'message body goes here'


msg = MIMEText(msg_text)
msg['Subject'] = subject
s = smtplib.SMTP()
s.connect(smtp_server)
s.sendmail(sender, recipients, msg.as_string())
s.close()

1 comment:

Marius Gedminas said...

To make things more interesting, try to make one or more of your strings contain non-ASCII characters.

I had to jump through quite a lot of hoops to make it work, once.