#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 send_email(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_text(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')
self.msg.attach(text_plain)
#构造图片链接,发送报错的截图,
def send_image(self):
#获取报错截图的地址,精确到日
image_path=screenshot_path+'\\'+get_current_year()+'\\'+get_current_month()+'\\'\
+get_current_day()
get_hour = 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
self.msg.attach(image)
return
# 判断当前时间是否在列表,不在,把前一小时文件夹下的所有图片都加载在邮件图片中(主要用在跨时间时截图,如10:59:59截图,邮件在11点发送)
elif str(get_hour)+'时' in path_list:
image_path = screenshot_path + '\\' + get_current_year() + '\\' + get_current_month() + '\\' \
+ get_current_day() + '\\' + str(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
self.msg.attach(image)
return
else:
print('image not found')
return
#构造HTML,通过读配置文件发送,把要发送的html放在一个文件里
def send_html(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"'
self.msg.attach(text_html)
#构造附件,通过读配置文件发送,把要发送的附件内容放在一个文件里
def send_file(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')
self.msg.attach(text_att)
#邮件发送
def send(self):
send_email.send_text(self)
send_email.send_image(self)
send_email.send_html(self)
send_email.send_file(self)
self.smtp.connect(smtpserver,port) #链接邮件服务器
self.smtp.login(username,password) #登录,注意password 是授权码
self.smtp.sendmail(sender,receiver,self.msg.as_string()) #发送
self.smtp.quit() #退出
if __name__== '__main__':
se=send_email()
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
|