配置文email.ini件信息:

[email]
sender=xxxxxxxxxxx
pwd=xxxxxxxxxxxx
reciver=xxxxxxxxxxxxx
python 3.x代码如下:
import os,configparser,time,requests,hashlib,json
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def filePath(path):
return os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),path)
def getEmailData():
#获取配置文件email信息
cf=configparser.ConfigParser()
ConfigPath=filePath('config\\email.ini')
cf.read(ConfigPath)
form_addr=cf.get('email','sender')
pwd=cf.get('email','pwd')
to_addr=cf.get('email','reciver')
return form_addr,pwd,to_addr
def sendEmail(report_path,report_name):
    #发送邮件
  '''
  report_path:发送的文件路径
  report_name:发送的文件命名
'''
from_add,pwd,to_user=getEmailData()
fp=open(report_path,'rb')
mail_body=fp.read()
fp.close()
msg=MIMEMultipart()
smtp_server='smtp.exmail.qq.com'
msg['From']=Header(from_add)
msg['To']=Header(to_user)
msg['Subject']=Header(u'私家云接口测试报告','utf-8')
msg['date']=time.strftime('%Y%m%d%H%M')
#发送内容
textpart=MIMEText(mail_body,_subtype='html',_charset='utf-8')
msg.attach(textpart)
#以html附件形式发送
htmlpart=MIMEApplication(open(reportPath(),'rb').read())
htmlpart.add_header('Content-Disposition','attachment',filename=report_name)
msg.attach(htmlpart)
#发送邮件
try:
s=smtplib.SMTP(smtp_server,25)
s.login(from_add,pwd)
s.sendmail(from_add,to_user.split(','),msg.as_string())
s.quit()
log.info(u'邮件发送成功!')
except smtplib.SMTPRecipientsRefused as err:
log.error(u'邮件发送失败!原因为:'+err)
except smtplib.SMTPAuthenticationError as err:
log.error(u'邮件发送失败!原因为:'+err)
except smtplib.SMTPException as err:
log.error(u'邮件发送失败!原因为:'+err)
05-12 17:44