标签:
SMTP是发送邮件的协议。python对SMTP的支持有两个模块,smtplib模块和email模块。email
负责构造邮件,smtplib
负责发送邮件。
一个例子简单的实现发送邮件的功能
#!/usr/bin/env python # coding=utf-8 import smtplib from email.mime.text import MIMEText mail_to_list=["openstack@126.com"] mail_host="smtp.126.com" mail_user="linux" mail_pass="password" mail_postfix="126.com" def send_mail(to_list,sub,content): me=mail_user+"<"+mail_user+"@"+mail_postfix+">" msg=MIMEText(content) msg[‘Subject‘]=sub msg[‘From‘]=me msg[‘To‘]=";".join(to_list) try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() return True except Exception, e: print str(e) return False if __name__ == ‘__main__‘: if send_mail(mail_to_list,"this is sub","This is content"): print "ok" else: print "fail"
标签:
原文地址:http://www.cnblogs.com/yeming/p/4790174.html