zoukankan      html  css  js  c++  java
  • python

    # -*- coding:utf-8 -*-

    '''
    @project: Voctest
    @author: Jimmy
    @file: sendMail.py
    @ide: PyCharm Community Edition
    @time: 2018-11-15 17:14
    @blog: https://www.cnblogs.com/gotesting/

    '''

    import os
    import smtplib
    import time
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from Common.log import Log
    from Config.globalConfig import report_path


    logger = Log()
    # 配置收发件人
    recv_address = ['641969198@qq.com']
    # 163的用户名和密码
    send_addr_name = 'jxy641969198@163.com'
    send_addr_pswd = 'jiaxy19920319'


    class SendMail:
    def __init__(self, recver=None):
    """接收邮件的人:list or tuple"""
    if recver is None:
    self.sendTo = recv_address
    else:
    self.sendTo = recver

    def get_report(self):
    """获取最新测试报告"""
    lists = os.listdir(report_path)
    lists.sort()
    send_report = lists[-1]
    print('The send report name: {0}'.format(send_report))
    return send_report

    def take_messages(self):
    """生成邮件的内容,和html报告附件"""
    report = self.get_report()
    self.msg = MIMEMultipart()
    self.msg['Subject'] = 'VOC自动化测试报告'
    self.msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')

    with open(os.path.join(report_path, report), 'rb') as f:
    mailbody = f.read()
    html = MIMEText(mailbody, _subtype='html', _charset='utf-8')
    self.msg.attach(html)

    # html附件
    att1 = MIMEText(mailbody, 'base64', 'gb2312')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="VocAutoTestReport.html"'
    self.msg.attach(att1)

    def send(self):
    """发送邮件"""
    self.take_messages()
    self.msg['from'] = send_addr_name
    try:
    smtp = smtplib.SMTP('smtp.163.com', 25)
    smtp.login(send_addr_name, send_addr_pswd)
    smtp.sendmail(self.msg['from'], self.sendTo, self.msg.as_string())
    smtp.close()
    logger.log_info("发送邮件成功")
    except Exception:
    logger.log_error('发送邮件失败')
    raise
  • 相关阅读:
    Python中的logging模块
    Windows 域(domain)
    linux下查看某软件是否已安装, ubuntu安装deb包
    linux之iptables
    linux之flock函数锁文件
    《上班赚钱,下班理财》
    Get the image file(s) some informations,Including the Make,Model,Date/Time,etc
    java中的Integer的toBinaryString()方法
    面试题中遇到的SQL题目
    get和post方法的区别
  • 原文地址:https://www.cnblogs.com/gotesting/p/9965249.html
Copyright © 2011-2022 走看看