Tuesday, March 17, 2009

Sending simple email using python

Source http://snippets.dzone.com/posts/show/658

In my case, I can send simple email using smtplib but the I received empty/blank email which is without From/To/Body appeared on my email client (Thunderbird).

So you need to wrap your email with email.MIMEText as sample codes bellow:

import smtplib
from email.MIMEText import MIMEText

def sendTextMail(to,sujet,text,server="localhost"):
fro = "Expediteur "
mail = MIMEText(text)
mail['From'] = fro
mail['Subject'] =sujet
mail['To'] = to
smtp = smtplib.SMTP(server)
smtp.sendmail(fro, [to], mail.as_string())
smtp.close()

sendTextMail("toto@titi.com","hello","cheers")


No comments: