发送邮件是大家经常碰到的,接下来看一下使用 python smtplib模块发送邮件,好了废话不多说,直接上代码:
import smtplib,sys,os,time
from email.mime.text import MIMEText
import newreport
def send_mail(me,tomail,sub,content):
#要发给谁,可以发送多个人
mailto_list = [tomail+‘;‘]
#设置服务器,用户名、口令以及邮箱的后缀
mail_host = "smtp.126.com"
mail_user = me
mail_pass = "xss1989"
#mail_postfix = "126.com" #这个可有可无
‘‘‘
to_list:发给谁
sub:主题
content:内容
send_mail("xss21999@126.com","sub","content")
‘‘‘
fp = open(newreport.sendreport(),‘rb‘)
content = fp.read()
fp.close()
#me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
me=mail_user
#这里发送内容我用读取一个文件(通过函数返回)
msg = MIMEText(content,_subtype=‘html‘,_charset=‘utf-8‘)
msg[‘Subject‘] = sub
msg[‘From‘] = me
msg[‘data‘] = time.strftime("%Y-%m-%d %H:%M:%S")
msg[‘To‘] = ";".join(mailto_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == ‘__main__‘:
if send_mail(‘xss****@126.com‘,‘xss**@126.com‘, #如果是多个人请用分号隔开
u‘这是python 自动化脚本测试报告‘,
u‘这是python内容‘):
print ‘Email send successful!!!‘
else:
print u‘Email send fail !!!‘
本文出自 “君志所向,一往无前。” 博客,转载请与作者联系!
python smtplib 模块发送邮件,布布扣,bubuko.com
原文:http://meilisong.blog.51cto.com/2180138/1429985