zoukankan      html  css  js  c++  java
  • python系列——如何发送测试报告邮件

    前言:不轻易评价别人是一种修养;不活在别人的评价里是一种智慧。

    此篇主要介绍,如何发送测试报告邮件

    核心代码

    import smtplib
    from email.mime.text import MIMEText
    
    body_str = '''
    <h3 align="center">自动化测试报告</h3>
    <table border="2" align="center" width="50%",height="400">
    <tr><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td></tr>
    </table>
    '''
    # msg = MIMEText('hello,P1P2','plain','utf-8')
    msg = MIMEText(body_str,'html','utf-8')
    msg['from'] = "***@qq.com"      #发件人
    msg['to'] = "***@qq.com"      #收件人
    msg['Cc'] = '***@qq.com'      #抄送人
    msg['subject'] = '主题'     #主题名
    
    smtp = smtplib.SMTP()
    smtp.connect( "smtp.qq.com" )
    smtp.login( user="***@qq.com" ,password='****' )     #发件人邮箱、授权码
    smtp.sendmail( "***@qq.com",["***@qq.com",'***@qq.com'],msg.as_string() )

    具体步骤:

    1、配置文件内——添加配置

    [email]
    # 邮箱服务器
    smtp_server = smtp.qq.com
    # 邮箱账号
    smtp_sender = ***@qq.com
    # 授权码
    smtp_password = ***
    # 收件人
    smtp_receiver = ***@qq.com
    # 抄送人
    smtp_cc = ***01@qq.com,***02@qq.com
    # 邮件主题
    smtp_subject = 接口自动化测试报告
    #配置工具模块中对应添加方法
     @property
        def REPORT_PATH(self):
            report_path_value = self.cfg.get('path','REPORT_PATH')
            return report_path_value
    
        @property
        def CASE_PATH(self):
            case_path_value = self.cfg.get('path','CASE_PATH')
            return case_path_value
    
        @property
        def SMTP_SERVER(self):
            smtp_server_value = self.cfg.get('email', 'smtp_server')
            return smtp_server_value
    
        @property
        def SMTP_SENDER(self):
            smtp_sender_value = self.cfg.get('email', 'smtp_sender')
            return smtp_sender_value
    
        @property
        def SMTP_PASSWORD(self):
            smtp_password_value = self.cfg.get('email', 'smtp_password')
            return smtp_password_value
    
        @property
        def SMTP_RECEIVER(self):
            smtp_receiver_value = self.cfg.get('email', 'smtp_receiver')
            return smtp_receiver_value
    
        @property
        def SMTP_CC(self):
            smtp_cc_value = self.cfg.get('email', 'smtp_cc')
            return smtp_cc_value
    
        @property
        def SMTP_SUBJECT(self):
            smtp_subject_value = self.cfg.get('email', 'smtp_subject')
            return smtp_subject_value

    2、整合邮件模块

    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from common.localconfig_utils import local_config
    
    class EmailUtils():
        def __init__(self,smtp_body,smtp_attch_path=None):
           self.smtp_server = local_config.SMTP_SERVER
           self.smtp_sender = local_config.SMTP_SENDER
           self.smtp_password = local_config.SMTP_PASSWORD
           self.smtp_receiver = local_config.SMTP_RECEIVER
           self.smtp_cc = local_config.SMTP_CC
           self.smtp_subject = local_config.SMTP_SUBJECT
           self.smtp_body = smtp_body
           self.smtp_attch = smtp_attch_path
    
        def mail_message_body(self):
            message = MIMEMultipart()
            message['from'] = self.smtp_sender
            message['to'] = self.smtp_receiver
            message['Cc'] = self.smtp_cc
            message['subject'] = self.smtp_subject
            message.attach( MIMEText(self.smtp_body,'html','utf-8') )
            if self.smtp_attch:
                attach_file = MIMEText(open(self.smtp_attch, 'rb').read(), 'base64', 'utf-8')
                attach_file['Content-Type'] = 'application/octet-stream'
                attach_file.add_header('Content-Disposition', 'attachment', filename=('gbk', '', os.path.basename(self.smtp_attch)))
                message.attach(attach_file)
            return message
    
        def send_mail(self):
            smtp = smtplib.SMTP()
            smtp.connect(self.smtp_server)
            smtp.login(user=self.smtp_sender, password=self.smtp_password)
            smtp.sendmail(self.smtp_sender,self.smtp_receiver.split(",")+ self.smtp_cc.split(","), self.mail_message_body().as_string())
    
    if __name__=='__main__':
        html_path = os.path.dirname(__file__) + '/../test_reports/P1P2接口自动化测试报告V1.4/P1P2接口自动化测试报告V1.4.html'
        EmailUtils('<h3 align="center">自动化测试报告</h3>',html_path).send_mail()
  • 相关阅读:
    01--DNS服务器3
    01--DNS服务器2
    装配bean
    实现二级域名
    apache反向代理
    struts拓展restful
    restful是什么
    struts的声明式异常处理
    linux常用命令之压缩打包
    linux常用命令之文件系统
  • 原文地址:https://www.cnblogs.com/miaoxiaochao/p/13340317.html
Copyright © 2011-2022 走看看