这是个人在项目中抽取的代码,自己写的utils的通用模块,使用的框架是tronado,包括了SMTP方式发送邮件,如有特别需要(POP)可以联系我或者自己扩展,刚学python不久,仅供参考,例子如下。
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def send_mail(auth,to_list,sub,content,file_name): print file_name mail_user, mail_pass, mail_host, mail_postfix = auth me=mail_user+"<"+mail_user+"@"+mail_postfix+">" #Create Mail msg = MIMEMultipart('alternative') msgText = MIMEText(content,'html', 'utf-8') msg.attach(msgText) msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) if file_name != None: att = MIMEText(open(file_name, 'rb').read(), 'base64', 'gb2312') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="%s"'%file_name.split('/')[-1].strip() msg.attach(att) #attachment #Send it! s = smtplib.SMTP() s.connect(mail_host) try: s.login(mail_user,mail_pass) print 'login email server' except Exception, e: pass s.sendmail(me,to_list,msg.as_string()) s.close() print 'success!'