【Web_接口测试_Python3_QQ邮件通知】QQ邮件通知,脚本执行成功发送邮件提醒,自动化测试案例

#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2020/5/28 and 15:44
Project:Python3
FileName:20200528_email.py
Description:
QQ邮件发送
'''
import smtplib  # smtplib 用于邮件的发信动作
from email.mime.text import MIMEText  # email 用于构建邮件内容
from email.header import Header  # 用于构建邮件头
from 抵押贷项目sit.library.basic import Basic
import time, random

logTime = time.strftime('%Y%m%d_%H:%M:%S', time.localtime(time.time()))
curTime = time.strftime('%Y%m%d %H%M%S', time.localtime(time.time()))

class TX_email():
    def __init__(self):
        self.logTime = time.strftime('%Y%m%d_%H:%M:%S', time.localtime(time.time()))
        self.curTime = time.strftime('%Y%m%d %H%M%S', time.localtime(time.time()))
        self.nowdate_8, self.nowtime_6, self.random_3 = self.curTime.split(" ")[0], self.curTime.split(" ")[1], str(random.randint(100, 999))
    def send_email(self, title='python_title', message='python test message', send_status=False):
        if send_status == True:
            # 发信方的信息:发信邮箱,QQ 邮箱授权码
            from_addr = '[email protected]'
            password = '123456'
            # 收信方邮箱
            to_addr = '[email protected]'
            # 发信服务器
            smtp_server = 'smtp.qq.com'
            # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
            msg = MIMEText(message, 'plain', 'utf-8')
            # 邮件头信息
            msg['From'], msg['To'], msg['Subject'] = Header(from_addr), Header(to_addr), Header(title)
            # 开启发信服务,这里使用的是加密传输
            server = smtplib.SMTP_SSL()
            server.connect(smtp_server, 465)
            # 登录发信邮箱
            server.login(from_addr, password)
            # 发送邮件
            server.sendmail(from_addr, to_addr, msg.as_string())
            # 关闭服务器
            server.quit()
        Basic().write_txt(filename=f"run_file\\{self.nowdate_8[:]}_runlog.txt", mode="a+", log_info="(email:)" + "----邮件发送成功----" + "\n(messg:)" + str(f"{message}"))
        print(f"\n----邮件发送成功----")
if __name__ == '__main__':
    # # 邮件通知
    tx_message = "test"
    TX_email().send_email(title=f"测试数据:{logTime}", message=f"{tx_message}")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章