python郵件腳本

#用于發送測試報告(帶附件)或其它內容# -*- coding:utf-8 -*-
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipartclass SendEmail(object):def __init__(self, receivers):mail_host = "127.0.0.1"  # SMTP服務器mail_user = "username"  # 用戶名mail_pass = "password"  # 密碼sender = "123456@qq.com"  # 發件人郵箱file_path = os.path.dirname(os.path.dirname(__file__)) + '\\report\\test_result.html'  # 拼接測試報告的路徑title = '自動化測試結果'  # 郵件主題mail_body = "查看測試報告請下載附件!!!"message = MIMEMultipart()message['From'] = "{}".format(sender)  # 從哪發出郵件 message['To'] = ",".join(receivers)    # receivers:接收人郵箱地址message['Subject'] = title  # 標題msgtext = MIMEText(mail_body, _subtype='plain', _charset='utf-8')  # 三個參數:第一個為文本內容,第二個 plain 設置文本格式,第三個 utf-8 設置編碼message.attach(msgtext)  # 添加主郵件主體內容# 添加一個HTML文本附件ff = open(file_path, 'rb')att = MIMEText(ff.read(), 'base64', 'utf-8')# 附件設置內容類型,設置為二進制流att["Content-Type"] = 'application/octet-stream'# 設置附件頭,添加文件名,filename 可以隨意寫att["Content-Disposition"] = 'attachment; filename="test_result.html"'# 解決中文附件名亂碼問題# att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', basename))message.attach(att)ff.close()try:smtpObj = smtplib.SMTP(mail_host, 25)  # 25 是指定 SMTP 服務使用的端口號,默認 25"""SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用于由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。python的smtplib提供了一種很方便的途徑發送電子郵件。它對smtp協議進行了簡單的封裝。"""smtpObj.login(mail_user, mail_pass)  # 登陸郵箱smtpObj.sendmail(sender, receivers, message.as_string())  # 發送郵件(發信人,收件人,郵件內容),這里需要注意郵件內容的格式,這個格式就是 smtp 協議中定義的格式print("郵件已成功發送")smtpObj.quit()except smtplib.SMTPException as e:print(e)if __name__ == "__main__":SendEmail(["123456@qq.com", "123467@qq.com"])

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/535290.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/535290.shtml
英文地址,請注明出處:http://en.pswp.cn/news/535290.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Rsync+Inotify

RsyncInotify Rsync這個一般使用系統自帶的服務,服務端需要啟動客戶端無需啟動,服務端設置開機自動啟動 systemctl start rsyncd systemctl status rsyncd systemctl restart rsyncd systemctl enable rsyncd Inotify單獨下載安裝&#xff0c…

logstash密碼設置步驟

生成密鑰存儲倉庫 cd /app/logstash-6.8.4/bin/ ./logstash-keystore create 輸入y 添加Logstash中可引用的變量 ./logstash-keystore add es_user 輸入elastic ./logstash-keystore add es_pwd 輸入bamBoo123 ./logstash-keystore add kafka_pwd 輸入bamBoo123 ./logstash…

清理cacahe腳本

#! /bin/bash # 需要釋放內存的,內存使用百分比,可以傳參,默認是85% max_rate$1 if [ ! "$max_rate" ] ; thenmax_rate85 fi echo "max_rate: $max_rate"totalfree -m | awk NR2 | awk {print $2} usedfree -m | awk NR2…

Prometheus node_exporter 指標說明及告警規則表達

Prometheus node_exporter 指標說明及告警規則表達_獨步秋風的博客-CSDN博客_node exporter 指標

UnicodeEncodeError: 'gbk' codec can't encode character '\xeb' in position 20: illegal multibyte sequ

源代碼:with open (os.path.join(self.root,filename),mode‘w’,newline’’) as f: writercsv.writer(f) for img in images: nameimg.split(os.sep)[-2] labelself.name2lable[name] writer.writerow([img,label]) 解決方法 在open()里面…

列表生成式(List)

列表生成式即List Comprehensions,是Python內置的非常簡單卻強大的可以用來創建list的生成式。 list(range(1,6)) [1,2,3,4,5]print([x*x for x in range(10)]) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]print([x*x for x in range(10) if x%20]) [0, 4, 16, 36, 64]

uint8 轉換為 float

znp.arange(3,dtypenp.uintj8) >>> z.astype(float) array([ 0., 1., 2.]) >>> np.int8(z) array([0, 1, 2], dtypeint8)

AttributeError: 'NoneType' object has no attribute 'astype'

img cv2.imread(path)#path為圖片地址 print(type(img))>>> <class numpy.ndarray> <class numpy.ndarray> <class numpy.ndarray> <class NoneType>錯誤問題為沒有正確讀到圖片&#xff0c;返回一個Nonetype類型&#xff0c;

pycharm 自動補全括號 引號

過程&#xff1a; File->settings->Editior->General->Smart Keys 勾選Insert paired barckets(),[],{},<>

RuntimeError: freeze_support()

train_dataloaderDataLoader(train_dataset,batch_sizebatch_size,shuffleTrue,num_workers4) 把nu_worker4 去掉 train_dataloaderDataLoader(train_dataset,batch_sizebatch_size,shuffleTrue)

IndexError: list index out of range image_name=self.images_name[index]

這里是引用 def __len__(self):return len(self.images_name) 注意這個地方

NameError: name 'long' is not defined

是因為高版本Python3.x中沒有long類型&#xff0c;只有int類型。 Python2.x中既有long 類型又有int 類型。 所以只需要把long()改為int 就行

Latex 修改公式的的大小

第一種方法&#xff1a;用比較笨的方法&#xff0c;一個一個公式用\begin{small} \begin{equation} \ldots \end{equation} \end{small} 第二種方法&#xff1a;定義新的變量環境 在開始\newenvironment{sequation}{\begin{equation}\small}{\end{equation}}演示代碼&#xff1…

latex表格名的引用問題

##方法 \begin{table} \label{*****} \end{table} 在文章中加入Tab.~\ref{****} 如果沒有顯示出來的話 在文章的開頭加上\usepackage{table}

Anaconda如何重新在開始菜單顯示

步驟&#xff1a; &#xff08;1&#xff09; win R &#xff0c;然后輸入Cmd 進入電腦命令端 &#xff08;2&#xff09;cd 進入anaconda 安裝的路徑&#xff0c;然后 輸入 、、、 python .\Lib_nsis.py mkmenus 、、、 然后就OK了

python 讀取 Excel 文件的方法 csv.reader

with open(./hotel_bookings.csv,newline) as f:readercsv.reader(f)for row in reader:print(row)row 為list 類型

python matplotlib 繪制曲線圖,柱狀圖

matplotlib.pyplot.bar(x, y, width0.8, bottomNone, \*, aligncenter, dataNone, \*\*kwargs) x:x軸的數據值&#xff0c;或者標簽 y:每個標簽的值 width: 每個柱形圖中每個柱子的寬度 bottom&#xff1a;y軸的基準值&#xff08;一般為0&#xff09; align: 柱形圖中每個柱子…

python for while enumerate

for 循環語句 list[2,3,4,5,6] for i in range(len(list)):print(i)print(list[i]) 其中 len() 表示計算數組的長度&#xff0c;range()表示生成一個指定長度的序列&#xff08;0&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;。。。。&#xff09; list[2,3,4,5,6]fo…

python plt.plot bar 設置繪圖尺寸大小

plt.rcParams(figure.figsize)(12,8) 修改前 修改后