python實現(xiàn)郵件循環(huán)自動發(fā)件功能
發(fā)郵件是一種很常見的操作,本篇主要介紹一下如何用python實現(xiàn)自動發(fā)件。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
import time
mail_host="smtp.126.com"
mail_user="xxx@126.com"
mail_pass="******"#注意如果郵箱開啟了授權(quán)碼,此處要填寫授權(quán)碼,否則會報smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
sender="xxx@126.com"
receiver = ['郵箱1','郵箱2']#群發(fā)郵件
for i in range(n):#自定義循環(huán)發(fā)多少遍
try:
message = MIMEMultipart()
message["From"] = Header(sender)
message["To"] = ','.join(receiver)
message["Subject"] = Header("主題", "utf-8").encode()#主題
message.attach(MIMEText("正文", "plain", "utf-8"))#正文
"""
定附件
"""
att = MIMEText(open(r'C:\Users\Administrator\Desktop\1.txt').read(), "base64", "utf-8")
att["Content-Type"] = 'application/octet-stream'
att.add_header("Content-Disposition", 'attachment', filename="1.txt")#這一步可避免文件不能正常打開
message.attach(att)
"""
構(gòu)造圖片(以附件形式上傳)
"""
image = MIMEImage(open(r'C:\Users\Administrator\Desktop\1.jpg', 'rb').read())
image.add_header('Content-ID', '<image1>')#可避免圖片不能正常打開
image["Content-Disposition"] = 'attachment; filename="picture.jpg"'
message.attach(image)
"""
發(fā)送郵件
"""
smtp = smtplib.SMTP_SSL(host=mail_host)
smtp.connect(host=mail_host, port=465)
smtp.login(mail_user, mail_pass)
smtp.sendmail(sender, message['To'].split(','), message.as_string())
print("在%s第" % ctime(), str(i+1), "封郵件發(fā)送")
smtp.quit()
except smtplib.SMTPException as e:
raise e
最終實現(xiàn)

到此這篇關(guān)于python實現(xiàn)郵件循環(huán)自動發(fā)件功能的文章就介紹到這了,更多相關(guān)python郵件循環(huán)自動發(fā)件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
安裝Keras,tensorflow,并實現(xiàn)將虛擬環(huán)境添加到j(luò)upyter?notebook
這篇文章主要介紹了安裝Keras,tensorflow,并實現(xiàn)將虛擬環(huán)境添加到j(luò)upyter?notebook,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
python numpy中對ndarry按照index增刪改查
這篇文章主要介紹了python numpy中對ndarry按照index增刪改查,在numpy中的ndarry是一個數(shù)組,因此index就是位置下標(biāo),注意下標(biāo)是從0開始,接下來一起進入下面文章了解詳細(xì)內(nèi)容吧2022-02-02
Python使用tkinter模塊實現(xiàn)GUI界面的學(xué)生信息管理系統(tǒng)流程分步詳解
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡易學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-01-01
python連接本地SQL?server詳細(xì)圖文教程
在數(shù)據(jù)分析領(lǐng)域,經(jīng)常需要從數(shù)據(jù)庫中獲取數(shù)據(jù)進行分析和處理,下面這篇文章主要介紹了python連接本地SQL?server的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
Python使用Selenium批量自動化獲取并下載圖片的方法
在現(xiàn)代的Web開發(fā)中,自動化測試和數(shù)據(jù)抓取已經(jīng)成為不可或缺的一部分,Selenium作為一款強大的自動化測試工具,可以用于批量獲取網(wǎng)頁上的圖片,所以本文給大家介紹了Python如何使用Selenium批量自動化獲取并下載圖片的方法2024-11-11

