# -*- coding:utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
import smtplib
def send_mail(file_new):
f = open(file_new,‘rb‘)
mail_body = f.read()
f.close()
msg = MIMEText(mail_body,‘html‘,‘utf-8‘)
msg[‘Subject‘] = Header("自动化测试报告",‘utf-8‘)
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("[email protected]",‘password‘)
smtp.sendmail("[email protected]","[email protected]",msg.as_string())
smtp.quit()
print "email has send out !"
send_mail("D:\\123456.txt")
注:以上代码可以实现自动发送邮件,不过在发送中文或者中英混合的文件时,会出现乱码的情况。对于主题、回复人涉及汉字的,要用Header("xxxx","utf-8")方式进行编码转换。至于内容,就不要转换成utf-8了。
时间: 2024-10-06 23:59:52