#!/usr/bin/env python# coding=utf-8import smtplibfrom email.MIMEText import MIMEText#from email.Utils import formatdatefrom email.Header import Headerimport sysreload(sys)sys.setdefaultencoding('utf-8')def send_mail(toMail, subject, body): smtpHost = 'test.163.com' smtpPort = '25' fromMail = "test@test.com" username = "test" password = "password" encoding = 'utf-8' toMail=toMail.split(',') mail = MIMEText(body.encode(encoding),'plain',encoding) mail['Subject'] = Header(subject, encoding) mail['From'] = fromMail mail['To'] =",".join(toMail) print mail['To']# mail['Date'] = formatdate() try:: smtp = smtplib.SMTP(smtpHost, smtpPort, timeout=20) smtp.ehlo() smtp.login(username, password) print toMail smtp.sendmail(fromMail, toMail, mail.as_string()) smtp.close() except Exception, data: print Exception, ":", data print 'Error: unable to send email' return False return Trueif __name__ == '__main__': print send_mail(sys.argv[1], sys.argv[2], sys.argv[3])