Python3+requests+unittest+log+excel+HTMLTestRunner+email框架接口自动化案例⑺——邮件系统

一、邮件发送报告方法

邮箱发送方法是封装smtplib和email模块。

sendemail.py

# _*_ coding:utf-8 _*_
import smtplib,time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_mail(file_new):
    """定义发送邮件函数"""
    mail_from='xxxxxx@qq.com'                                 #发信邮箱
    mail_to=['xxxxxx@163.com']                                #收信邮箱
    f = open(file_new, 'rb')                                  #打开文件
    mail_body = f.read()                                      #读取文件
    f.close()                                                 #关闭文件
    msg=MIMEText(mail_body,_subtype='html',_charset='utf-8')  #内容、格式、编码
    msg['From'] = "{}".format(mail_from)                      #发件人
    msg['To'] = ";".join(mail_to)                             #收件人
    msg['Subject']="API自动化测试报告"                          #邮件主题
    msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')     #定义发送时间(不定义的可能有的邮件客户端会不显示发送时间

    smtp=smtplib.SMTP()
    #smtp.set_debuglevel(1)                                   #开启发送debug模式,把发送邮件的过程显示出来
    smtp.connect('smtp.qq.com',25)                            #设置第三方SMTP服务器
    smtp.starttls()                                           #启动安全传输模式
    smtp.login('xxxxxx@qq.com','xxxxxx')                      #用户名、#授权密码,非登录密码
    smtp.sendmail(mail_from,mail_to,msg.as_string())          #配置发送邮箱,接收邮箱,以及发送内容
    smtp.quit()                                               #关闭发邮件服务

if __name__ == '__main__':
    send_mail(‘附件地址’)
—————————————————————————————— 选择正确的事、再把事做正确 ——————————————————————————————
原文地址:https://www.cnblogs.com/airb/p/13328067.html