Send Mail in Python
last updated : Thu 24 August 2006
This is a small snippet of code to send a mail in Python.
#!/usr/bin/env python
import os, sys, cgi, smtplib, email.Utils
from email.Message import Message
from email.MIMEText import MIMEText
from email.Header import Header
print "Content-type: text/html\n"
print "Simple Python Mail<br />"
to = "name@company.com"
From = "your.name@yourcompany.com"
subject = "This is a mail test."
body = "Python mail test body."
smtpServer = "10.0.0.1" #Change to your smtp server address
if len(body) > 4096:
body = body[1:4096]
msg = MIMEText(body, 'plain', 'UTF-8')
msg['From'] = From
msg['To'] = to
msg['Subject'] = Header(subject, 'UTF-8')
try:
server = smtplib.SMTP(smtpServer)
result = server.sendmail(From, to, msg.as_string())
server.quit()
print "Message sent to " + to
except smtplib.SMTPException, errmssg:
print "Error sending mail : " + errmsg