selenium 数据驱动框架自动化从0到1–10(发送邮件的封装)

#encoding=utf-8

from util.config_file_parse import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
from util.date_time import *
from config.var_config_path import * #导入文件地址
import os


#邮件发送只测试了qq 邮箱和163邮箱,其它邮箱还得改配置
class SendEmail(object):
    def __init__(self):
        global smtpserver,port,username,password,sender,receiver,subject
        pc = ConfigParseFile()
        smtpserver = pc.get_option_value_email('email', 'smtpserver')
        port = pc.get_option_value_email('email','port')
        username = pc.get_option_value_email('email', 'username')
        password = pc.get_option_value_email('email', 'password')
        sender = pc.get_option_value_email('email', 'sender')
        receiver = pc.get_option_value_email('email', 'receiver')
        subject = pc.get_option_value_email('email_content', 'subject')
        subject = Header(subject,'utf-8').encode()           #邮件标题可以用中文
        #当用163邮箱给其它邮箱发邮件时
        self.smtp = smtplib.SMTP()
        #当用QQ邮箱给其它邮箱发送邮件时
        #self.smtp = smtplib.SMTP_SSL(smtpserver)
        self.msg = MIMEMultipart('mixed')
        self.msg['Subject'] = subject      #邮件标题
        self.msg['From'] = sender          #发件人
        self.msg['To'] = receiver          #收件人,可添加同一邮箱的多人


    #邮件发送
    def send(self):
        self.__sendText()
        self.__sendImage()
        self.__sendHtml()
        self.__sendFile()
        self.smtp.connect(smtpserver,port)     #链接邮件服务器
        self.smtp.login(username,password)     #登录,注意password 是授权码
        self.smtp.sendmail(sender,receiver,self.msg.as_string())  #发送
        self.smtp.quit()   #退出

    #构造文字内容,发送的文字内容可以写在一个文件里,通过读取配置文件发送
    def __sendText(self):
        if send_email_content_path:
            with open(send_email_content_path,'r',encoding='utf-8') as f:
                text=f.read()
        else:
            print('file address not found')
            return
        text_plain = MIMEText(text, 'plain', 'utf-8')
        text=self.msg.attach(text_plain)
        return text

    #构造图片链接,发送报错的截图,
    def __sendImage(self):
        #获取报错截图的地址,精确到日
        image_path=screenshot_path+'\\'+get_current_year()+'\\'+get_current_month()+'\\'\
                       + get_current_day()
        if int(get_current_hour()[:-1]) - 1 < 10:
            get_hour = '0'+str(int(get_current_hour()[:-1]) - 1) + '时'
        else:
            get_hour = str(int(get_current_hour()[:-1]) - 1) + '时'
        os.chdir(image_path)
        path_list=[]
        #把当天下面的所有文件夹存在一个列表
        for y in os.listdir(image_path):
            path_list.append(y)
        #判断当前时间是否在列表,在,把当前时间文件夹下的所有图片都加载在邮件图片中
        if get_current_hour() in path_list:
            image_path=image_path+'\\'+get_current_hour()
            os.chdir(image_path)
            for i in os.listdir(image_path):
                sendimagefile = open(image_path+'\\'+i, 'rb').read()
                image = MIMEImage(sendimagefile)
                # 以下图片可以重命名成XXX.jpg/png,但不支持中文名
                image["Content-Disposition"] = 'attachment; filename="testimage"'+i
                imagecoutent = self.msg.attach(image)
            return imagecoutent
            # 判断当前时间是否在列表,不在,把前一小时文件夹下的所有图片都加载在邮件图片中(主要用在跨时间时截图,如10:59:59截图,邮件在11点发送)
        elif get_hour in path_list:
            image_path = screenshot_path + '\\' + get_current_year() + '\\' + get_current_month() + '\\' \
                         + get_current_day() + '\\' + get_hour
            os.chdir(image_path)
            for i in os.listdir(image_path):
                sendimagefile = open(image_path + '\\' + i, 'rb').read()
                image = MIMEImage(sendimagefile)
                image["Content-Disposition"] = 'attachment; filename="testimagetime"' + i
                imagecoutent = self.msg.attach(image)
            return imagecoutent
        else:
            print('image not found')
            return

    #构造HTML,通过读配置文件发送,把要发送的html放在一个文件里
    def __sendHtml(self):
        if send_email_content_path:
            with open(send_email_content_path, 'r', encoding='utf-8') as f:
                html = f.read()
        else:
            print('file address not found')
            return
        text_html = MIMEText(html, 'html', 'utf-8')
        # 以下html可以重命名成XXX.html,但不支持中文名
        text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
        html=self.msg.attach(text_html)
        return html

    #构造附件,通过读配置文件发送,把要发送的附件内容放在一个文件里
    def __sendFile(self):
        if send_email_content_path:
            with open(send_email_content_path, 'rb') as f:
                sendfile=f.read()
        text_att = MIMEText(sendfile, 'base64', 'utf-8')
        text_att["Content-Type"] = 'application/octet-stream'
        # 以下附件可以重命名成XXX.txt,但不支持中文名
        #text_att["Content-Disposition"] = 'attachment; filename="XXX.txt"'
        # 另一种实现方式
        text_att.add_header('Content-Disposition', 'attachment', filename='aaa.txt')
        file = self.msg.attach(text_att)
        return file




if  __name__== '__main__':
    se=SendEmail()
    se.send()

邮箱配置文件

[email]
#qq邮箱给其它邮箱发邮件配置
#smtpserver = smtp.qq.com
#port = 465
#username = 
#password = xnmtkouedjmqbeii
#piwdxigsgdipbgag
#apukozuowfyibedi
#xnmtkouedjmqbeii
#163邮箱给其它邮箱发邮件配置
smtpserver = smtp.163.com
port = 25
username = 
password = BSLTPZMSKWSNRZBS
sender = 
receiver = 
[email_content]
subject = 发送邮件测试

地址配置文件

#encoding=utf-8

import os

#当前文件绝对路径的上上级目录(工程目录)
parent_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(parent_path)

#页面元素表达式配置文件地址
test_page_expression_path = os.path.join(parent_path, r'config\page_elements_location.ini')
print(test_page_expression_path)

#发送邮件的配置地址
send_email_path = os.path.join(parent_path,r'config\email_config.ini')
print(send_email_path)

#发送邮件内容地址
send_email_content_path=os.path.join(parent_path,r'config\aa.txt')
print('-----',send_email_content_path)

#截图文件地址
screenshot_path = os.path.join(parent_path, r'screenshot_file')
print(screenshot_path)

#日志文件地址
log_path = os.path.join(parent_path, r'log')
print(log_path)

这里我封装了一个邮件发送的方法,根据自己的情况稍作修改就可使用,具体的详情解释可参考https://www.cnblogs.com/yufeihlf/p/5726619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值