自動發(fā)送郵件功能是自動化測試項目的重要需求之一,當(dāng)自動化測試用例運行完成之
后,可自動向相關(guān)人員的郵箱發(fā)送測試報告。
SMTP(Simple Mail Transfer Protocol)是簡單郵件傳輸協(xié)議,是一組由源地址到目的
地址傳送郵件的規(guī)則,可以控制信件的中轉(zhuǎn)方式。Python 的 smtplib 模塊提供了簡單的 API
用來實現(xiàn)發(fā)送郵件功能,它對 SMTP 進行了簡單的封裝。
在給其他人發(fā)送郵件之前,首先需要有一個自己的郵箱。通過瀏覽器打開郵箱網(wǎng)址(如 www.126.com),或打開郵箱客戶端(如 Foxmail),登錄自己的郵箱賬號。如果是郵箱客戶端,則還需要配置郵箱服務(wù)器地址(如 smtp.126.com)。然后填寫收件人地址、郵件的主題和正文,以及添加附件等。即便通過 Python 實現(xiàn)發(fā)送郵件功能,也需要設(shè)置這些信息。
Python 自帶的發(fā)送郵件功能
在發(fā)送郵件時,除填寫主題和正文外,還可以增加抄送人、添加附件等。這里我們分
別把測試報告作為正文和附件進行發(fā)送。
1.發(fā)送郵件正文
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 發(fā)送郵件主題
subject = 'Python email test'
# 編寫 HTML 類型的郵件正文
msg = MIMEText('<html><h1>你好!</h1></html>', 'html', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
# 發(fā)送郵件
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("sender@126.com", "a123456")
smtp.sendmail("sender@126.com", "receiver@126.com", msg.as_string())
smtp.quit()
首先,調(diào)用 email 模塊下面的 MIMEText 類,定義發(fā)送郵件的正文、格式,以及編碼。
然后,調(diào)用 email 模塊下面的 Header 類,定義郵件的主題和編碼類型。
smtplib 模塊用于發(fā)送郵件。connect()方法指定連接的郵箱服務(wù);login()方法指定登錄
郵箱的賬號和密碼;sendmail()方法指定發(fā)件人、收件人,以及郵件的正文; quit()方法用
于關(guān)閉郵件服務(wù)器的連接。
2.發(fā)送帶附件的郵件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 郵件主題
subject = 'Python send email test'
# 發(fā)送的附件
with open('log.txt', 'rb') as f:
? ? send_att = f.read()
att = MIMEText(send_att, 'text', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="log.txt"'
msg = MIMEMultipart()
msg['Subject'] = subject
msg.attach(att)
# 發(fā)送郵件
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("sender@126.com", "a123456")
smtp.sendmail("sender@126.com", "receiver@126.com", msg.as_string())
smtp.quit()
首先,讀取附件的內(nèi)容。通過 MIMEText 類,定義發(fā)送郵件的正文、格式,以及編碼;
Content-Type 指定附件內(nèi)容類型;application/octet-stream 表示二進制流;Content-Disposition
指定顯示附件的文件;attachment; filename="log.txt"指定附件的文件名。
然后,使用 MIMEMultipart 類定義郵件的主題,attach()指定附件信息。
最后,通過 smtplib 模塊發(fā)送郵件,發(fā)送過程與第一個例子相同。
用 yagmail 發(fā)送郵件
yagmail 是 Python 的一個第三方庫,可以讓我們以非常簡單的方法實現(xiàn)自動發(fā)送郵件
功能。
GitHub 項目地址: https://github.com/kootenpv/yagmail。
通過 pip 命令安裝。
pip install yagmail
項目文檔提供了的簡單發(fā)送郵件的例子。
import yagmail
# 連接郵箱服務(wù)器
yag = yagmail.SMTP(user="sender@126.com", password="a123456",
host='smtp.126.com')
# 郵件正文
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.']
# 發(fā)送郵件
yag.send('receiver@126.com', 'subject', contents)
如果想給多個用戶發(fā)送郵件,那么只需把收件人放到一個 list 中即可。
# 發(fā)送郵件
yag.send(['aaa@126.com','bbb@qq.com','ccc@gmail.com'], 'subject', contents)
如果想發(fā)送帶附件的郵件,那么只需指定本地附件的路徑即可。
yag.send('aaa@126.com', 'subject', contents, ["d://log.txt","d://baidu_img.jpg"])
另外,還可以通過 list 指定多個附件。yagmail 庫極大地簡化了發(fā)送郵件的代碼。
整合自動發(fā)送郵件功能
import time
import unittest
import yagmail
from HTMLTestRunner import HTMLTestRunner
#把測試報告作為附件發(fā)送到指定郵箱
def send_mail(report):
? ? yag = yagmail.SMTP(user="sender@126.com",
? ? ? ? ? ? ? ? ? ? ? password="a123456",
? ? ? ? ? ? ? ? ? ? ? host='smtp.126.com')
? ? subject = "主題,自動化測試報告"
? ? contents = "正文,請查看附件。"
? ? yag.send('receiver@126.com', subject, contents, report)
? ? print('email has send out !')
if __name__ == '__main__':
? ? # 定義測試用例的目錄為當(dāng)前目錄
? ?test_dir = './test_case'
? ? suit = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')
? ?
? ? # 獲取當(dāng)前日期和時間
? ?now_time = time.strftime("%Y-%m-%d %H_%M_%S")
? ?html_report = './test_report/' + now_time + 'result.html'
? ?with open(report, 'wb') as fp:
? ?? ? # 調(diào)用 HTMLTestRunner,運行測試用例
? ? ? ? runner = HTMLTestRunner(stream=fp,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? title="百度搜索測試報告",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? description="運行環(huán)境:Windows 10, Chrome 瀏覽器"
? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? )
? ? ? ? runner.run(suit)
? ? send_mail(html_report) # 發(fā)送報告
整個程序的執(zhí)行過程可以分為兩部分:
(1)定義測試報告文件,并賦值給變量 html_report,通過 HTMLTestRunner 運行測試
用例,將結(jié)果寫入文件后關(guān)閉。
(2)調(diào)用 send_mail()函數(shù),并傳入 html_report 文件。在 send_mail()函數(shù)中,把測試報
告作為郵件的附件發(fā)送到指定郵箱。
為什么不把測試報告的內(nèi)容讀取出來作為郵件正文發(fā)送呢?因為 HTMLTestRunner 報
告在展示時引用了 Bootstrap 樣式庫,當(dāng)作為郵件正文“寫死”在郵件中時,會導(dǎo)致樣式丟文章來源:http://www.zghlxwxcb.cn/news/detail-407084.html
失,所以作為附件發(fā)送更為合適。文章來源地址http://www.zghlxwxcb.cn/news/detail-407084.html
到了這里,關(guān)于【Selenium學(xué)習(xí)】自動發(fā)送郵件功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!