溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python實現郵件循環自動發件功能

發布時間:2020-09-26 19:51:06 來源:腳本之家 閱讀:156 作者:liyali2020 欄目:開發技術

發郵件是一種很常見的操作,本篇主要介紹一下如何用python實現自動發件。

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="******"#注意如果郵箱開啟了授權碼,此處要填寫授權碼,否則會報smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
sender="xxx@126.com"
receiver = ['郵箱1','郵箱2']#群發郵件
for i in range(n):#自定義循環發多少遍
	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)
 	"""
 	構造圖片(以附件形式上傳)
 	"""
 	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)
 		"""
 		發送郵件
 		"""
 	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), "封郵件發送")
 	smtp.quit()
	except smtplib.SMTPException as e:
  	raise e

最終實現

python實現郵件循環自動發件功能

到此這篇關于python實現郵件循環自動發件功能的文章就介紹到這了,更多相關python郵件循環自動發件內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女