python selenium-7自動發送郵件
阿新 • • 發佈:2018-12-01
lis pat load dma art connect -- mage subject 3.發送的附件不是指定的文件名,而是tcmime.*.bin類型的文件
https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html
1.發送HTML格式的郵件
import smtplib from email.mime.text import MIMEText from email.header import Header #發送郵箱服務器 smtpserver = "smtp.126.com" #發送郵箱用戶/密碼 user = "發送郵箱@126.com" password = "登陸密碼" #發送郵箱 sender = "發送郵箱@126.com" #接收郵箱 receiver = "接收郵箱@qq.com" #發送郵箱主題 subject = "python發送郵件" #編寫HTML類型的郵箱正文 msg=MIMEText(‘<html><h1>給QQ郵箱發送郵件</h1></html>‘,‘html‘,‘utf-8‘) msg[‘Subject‘]=Header(subject,‘utf-8‘) msg[‘From‘]="張三" msg[‘To‘]=receiver smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user,password) try: smtp.sendmail(sender, receiver, msg.as_string()) print("發送成功") except smtplib.SMTPDataError as e: print("發送失敗") finally: smtp.quit()
2.發送文本內容的郵件
import smtplib from email.mime.text import MIMEText from email.header import Header msg_from = ‘發送郵箱@qq.com‘ passwd = ‘授權碼‘ msg_to = ‘收件人郵箱@126.com‘ subject = "python郵件測試" content = "這是我使用python smtplib及email模塊發送的郵件" msg = MIMEText(content) msg[‘Subject‘] = subject msg[‘From‘] = "發送方" msg[‘To‘] = "接收方" try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print("發送成功") except s.SMTPDataError as e: print("發送失敗") finally: s.quit()
其中 s = smtplib.SMTP_SSL("smtp.qq.com", 465)#相當於以下2行
s = smtplib.SMTP()
s.connect("smtp.qq.com")
3.發送帶附件的郵件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart smtpserver = "smtp.126.com" sender = "發送郵箱@126.com" receiver = "接收郵箱@qq.com" user = "發送郵箱@126.com" password = "發送郵箱密碼" subject = "測試附件3" sendfile = open("/Users/chenshanju/PycharmProjects/SeleniumOfJenkins/report/log.txt","rb").read() att = MIMEText(sendfile,"base64","utf-8") att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] ="attachment;filename=‘log.txt‘" msgRoot = MIMEMultipart("related") msgRoot["Subject"] = subject msgRoot["From"] = sender msgRoot["To"] = receiver msgRoot.attach(att) smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user,password) smtp.sendmail(sender,receiver,msgRoot.as_string()) smtp.quit()
4.查找最新的測試報告
import sys,os
#獲取當前腳本的目錄
script_path=sys.path[0].split("/")
#獲取工程的主目錄
end_index=script_path.index("SeleniumOfJenkins")+1
#report所在的目錄
report_dir="/".join(script_path[:end_index])+"/report"
#查看當前的文件生成列表
list_file=os.listdir(report_dir)
#對文件按照創建時間進行排序
list_file.sort(key=lambda file:os.path.getmtime(report_dir+"/"+file))
print(list_file[-1])
5.自動發送測試報告
import unittest,os,sys
from time import strftime
from HTMLTestRunner import HTMLTestRunner
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def get_new_report(report_dir):
"get_new_report()方法傳入一個參數(測試報告所在目錄),得到最新的report文件"
file_list = os.listdir(report_dir)
file_list.sort(key=lambda file:os.path.getmtime(report_dir+"/"+file))
return report_dir+"/"+file_list[-1]
def send_email(new_report):
"send_email()接收一個參數(new_report),發送郵件"
f = open(new_report,"rb")
mail_body=f.read()
f.close()
sender = "發送郵箱@126.com"
receiver = "接收郵箱@qq.com"
msg = MIMEText(mail_body,"html","utf-8")
msg[‘Subject‘] = Header("自動化測試報告","utf-8")
msg[‘From‘] = sender
msg[‘To‘] = receiver
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("發送郵箱@126.com","發送郵箱密碼")
try:
smtp.sendmail(sender,receiver,msg.as_string())
print("發送成功")
except :
print("發送失敗")
finally:
smtp.quit()
if __name__=="__main__":
"""1.執行測試 2.獲取最新報告 3.發送郵件"""
current_dir_list = sys.path[0].split("/")
index = current_dir_list.index("SeleniumOfJenkins") + 1
if index == len(current_dir_list):
home_dir = "/".join(current_dir_list)
else:
home_dir = "/".join(current_dir_list[:index])
report_dir = home_dir + "/" + "report"
test_dir = home_dir + "/testcase/testsearch"
discover = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")
filename="report/"+strftime("%Y_%m_%d_%H_%M_%S")+"_result.html"
fp = open(filename,"wb")
runner=HTMLTestRunner(stream=fp,title="百度搜索報告",description="用例執行情況")
runner.run(discover)
fp.close()
new_report = get_new_report(report_dir)
send_email(new_report)
FAQ:
1.qq郵箱需要開啟imap和smtp權限
2.從126郵箱發送qq郵箱拋出異常smtplib.SMTPDataError
smtplib.SMTPDataError: (554, b‘DT:SPM 126 smtp1,C8mowABHGtEduABcQk94BA--.46414S2 1543551006,please see http://mail.163.com/help/help_spam_16.htm?ip=125.122.53.172&hostid=smtp1&time=1543551006‘)
解決方法:需要對msg[‘From‘]和msg[‘To‘]賦值
3.發送的附件不是指定的文件名,而是tcmime.*.bin類型的文件
原因:att["Content-Disposition"] ="attachment;filename=‘log.txt‘"誤寫為Content-Dispostion
4.自動發送測試報告中,qq郵箱樣式丟失,126郵箱正常
使用附件可以解決這個問題
import smtplib,os,sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def get_home_dir():
"獲取最新的報告"
current_path_list=sys.path[0].split("/")
index = current_path_list.index("SeleniumOfJenkins")+1
if index == len(current_path_list):
home_path=sys.path[0]
else:
home_path="/".join(current_path_list[:index])
return home_path
def get_new_file():
"獲取最新的報告"
file_list=os.listdir(report_path)
file_list.sort(key=lambda file:os.path.getmtime(report_path+file))
return file_list[-1]
def send_email():
"發送郵件"
new_file_name = get_new_file()
new_file = report_path + new_file_name
server = "smtp.126.com"
sender="發送郵箱@126.com"
receiver="接收郵箱@qq.com"
subject = "測試報告見附件3"
sendfile= open(new_file,"rb").read()
att = MIMEText(sendfile,"base64","utf-8")
att[‘Content-Type‘] = "application/octet-stream"
att[‘Content-Disposition‘] = "attachment;filename=‘%s‘" % new_file_name
msgRoot = MIMEMultipart(‘related‘)
msgRoot[‘Subject‘] = subject
msgRoot[‘From‘] = sender
msgRoot[‘To‘] = receiver
msgRoot.attach(att)
smtp = smtplib.SMTP()
try:
smtp.connect(server)
smtp.login("發送郵箱@126.com","發送郵箱密碼")
smtp.sendmail(sender,receiver,msgRoot.as_string())
print("發送成功")
except:
print("發送失敗")
finally:
smtp.quit()
if __name__=="__main__":
home_path = get_home_dir()
report_path = home_path + "/report/"
send_email()
python selenium-7自動發送郵件