如何使用Python將生成的測試報告以郵件附件的形式進行發送呢?html
SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。
python的smtplib提供了一種很方便的途徑發送電子郵件,它對smtp協議進行了簡單的封裝。
Python對SMTP支持有smtplib
和email
兩個模塊。其中email
負責構造郵件,smtplib
則負責發送郵件。python
來理一理Python發送一個未知MIME類型的文件附件基本思路:安全
0、前提:導入郵件發送模塊 from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib 一、構造MIMEMultipart對象做爲根容器 二、構造MIMEText對象做爲郵件顯示內容並附加到根容器 a、讀入文件內容並格式化 b、設置附件頭 三、設置根容器屬性 四、獲得格式化後的完整文本 五、用smtp發送郵件 六、封裝成sendEmail類。
同時想一想咱們要發送郵件的幾個要素:服務器
一、服務器。以QQ郵箱舉例,則爲smtp.qq.com 二、端口號。有465和587,請使用587 三、發送者。 四、密碼。密碼總不能直接寫在文件裏吧?哈哈,這裏須要使用qq郵箱獲取受權碼。 五、收件人。(可能還不止一個) 六、發送郵件的主題subject。 七、郵件文本內容。 八、附件。
由於以前寫過如何讀取.ini配置文件,因此此部分,將發送郵件的一些要素放在了配置文件中,配置文件以下:併發
對應讀取配置文件腳本爲:(readConfig.py部分)app
import os import configparser # config cur_path = os.path.dirname(os.path.relpath(__file__)) configPath = os.path.join(cur_path,'config.ini') conf = configparser.ConfigParser() conf.read(configPath) def get_smtpServer(smtpServer): smtp_server = conf.get('email',smtpServer) return smtp_server # ......
構建MIMEMultipart()郵件根容器對象後,須要藉助根容器來定義郵件的各個要素,好比郵件主題subject、發送人from、接收人to、郵件正文body、郵件附件等。函數
# 構建根容器 msg = MIMEMultipart() # 郵件主題、發送人、收件人、內容,此部分能夠來自配置文件,也能夠直接填入 msg['Subject'] = self.mail_subject # u'自動化測試報告' msg['from'] = self.mail_sender msg['to'] = self.mail_pwd
# 郵件正文部分body,一、能夠用HTML本身自定義body內容;二、讀取其餘文件的內容爲body # body = "您好,<p>這裏是使用Python登陸郵箱,併發送附件的測試<\p>" with open(reportFile,'r',encoding='UTF-8') as f: body = f.read() msg.attach(MIMEText(_text=body, _subtype='html', _charset='utf-8')) # _charset 是指Content_type的類型
# 添加附件 attachment = MIMEText(_text=open(reportFile, 'rb').read(), _subtype='base64',_charset= 'utf-8') attachment['Content-Type'] = 'application/octet-stream' attachment['Content-Disposition'] = 'attachment;filename = "result.html"' msg.attach(attachment)
發送四部曲:取得服務器鏈接、再登陸郵箱、發送郵件、退出。
大體以下啦:測試
try: smtp = smtplib.SMTP_SSL(host=self.mail_smtpserver, port=self.mail_port) # 繼承自SMTP except: smtp = smtplib.SMTP() smtp.connect(self.mail_smtpserver, self.mail_port) # smtp.set_debuglevel(1) # 建立安全鏈接,加密SMTP smtp.starttls() # Puts the connection to the SMTP server into TLS mode. # 用戶名和密碼 smtp.login(user=self.mail_sender, password=self.mail_pwd) # 函數:sendmail(self, from_addr, to_addrs, msg, mail_options=[],rcpt_options=[]): smtp.sendmail(self.mail_sender, self.mail_receiverList, msg.as_string()) smtp.quit()
在裏面添加了一句smtp.starttls()
。這一句是用來加密SMTP會話,保證郵件安全發送不被竊聽的。
在建立完SMTP對象後,馬上調用starttls()
方法便可。
其實整個下來郵件發送模塊也就完成了。ui
在這個過程當中有碰見幾個問題,也貼上來跟你們一塊兒分享一下。加密
下面貼上整個文件,這個文件是依賴於其餘文件的的,因此僅供參考,可是方法是同樣的。
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase class SendEmail(object): ''' 發送郵件模塊封裝,屬性均從config.ini文件得到 ''' def __init__(self, smtpServer, mailPort, mailSender, mailPwd, mailtoList, mailSubject): self.mail_smtpserver = smtpServer self.mail_port = mailPort self.mail_sender = mailSender self.mail_pwd = mailPwd # 接收郵件列表 self.mail_receiverList = mailtoList self.mail_subject = mailSubject # self.mail_content = mailContent def sendFile(self, reportFile): ''' 發送各類類型的附件 ''' # 構建根容器 msg = MIMEMultipart() # 郵件正文部分body,一、能夠用HTML本身自定義body內容;二、讀取其餘文件的內容爲body # body = "您好,<p>這裏是使用Python登陸郵箱,併發送附件的測試<\p>" with open(reportFile,'r',encoding='UTF-8') as f: body = f.read() # _charset 是指Content_type的類型 msg.attach(MIMEText(_text=body, _subtype='html', _charset='utf-8')) # 郵件主題、發送人、收件人、內容 msg['Subject'] = self.mail_subject # u'自動化測試報告' msg['from'] = self.mail_sender msg['to'] = self.mail_pwd # 添加附件 attachment = MIMEText(_text=open(reportFile, 'rb').read(), _subtype='base64',_charset= 'utf-8') attachment['Content-Type'] = 'application/octet-stream' attachment['Content-Disposition'] = 'attachment;filename = "result.html"' msg.attach(attachment) try: smtp = smtplib.SMTP_SSL(host=self.mail_smtpserver, port=self.mail_port) # 繼承自SMTP except: smtp = smtplib.SMTP() smtp.connect(self.mail_smtpserver, self.mail_port) # smtp.set_debuglevel(1) # 建立安全鏈接,加密SMTP smtp.starttls() # Puts the connection to the SMTP server into TLS mode. # 用戶名和密碼 smtp.login(user=self.mail_sender, password=self.mail_pwd) # 函數:sendmail(self, from_addr, to_addrs, msg, mail_options=[],rcpt_options=[]): smtp.sendmail(self.mail_sender, self.mail_receiverList, msg.as_string()) smtp.quit() # 調試代碼 if __name__ == "__main__": mail_smtpserver = 'smtp.qq.com' mail_port = 587 mail_sender = '@qq.com' mail_pwd = '' mail_receiverList = ['@qq.com', '@163.com'] mail_subject = u'自動化測試報告' s = SendEmail(mail_smtpserver, mail_port, mail_sender, mail_pwd, mail_receiverList, mail_subject) s.sendFile('F:\Python_project\PythonLearnning_2018\send_email\sendEmail_Test.html.tar.gz') print('--- test end --- ')
若是以爲文章有丟丟用處,動動小指,點個贊吧!
若是哪裏寫的有問題,或者有更好的方式,cue我一下
❤ thanks for watching, keep on updating...