python自動發郵件
開啟服務
代碼
import smtplib
from email import encoders
from email.header import Header
from email.utils import parseaddr, formataddr
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 發件人郵箱
asender = "[email protected]"
# 收件人郵箱
areceiver = "[email protected]"
# 抄送人郵箱
acc = "[email protected]"
# 郵箱主題
asubject = "謝謝關注"
# 發件人地址
from_addr = "[email protected]"
# 郵箱授權碼
password = "********"
# 郵件設置
msg = MIMEMultipart()
msg['Subject'] = asubject
msg['to'] = areceiver
msg['Cc'] = acc
msg['from'] = "fanstuck"
# 郵件正文
body = "你好,歡迎關注搬磚代師,您的關注就是我繼續創作的動力!"
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# 添加附件
htmlFile = r'C:\\Users\\jiaoyang\\Desktop\\QQ圖片20220731232547.jpg'
html = MIMEApplication(open(htmlFile, 'rb').read())
html.add_header('Content-Disposition', 'attachment', filename='meimei.jpg')
msg.attach(html)
# 設置郵箱服務器地址和接口
smtp_server = "pop.qq.com"
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
# 登錄郵箱
server.login(from_addr, password)
# 發生郵箱
server.sendmail(from_addr, areceiver.split(',') + acc.split(','), msg.as_string())
# 斷開服務器連接
server.quit()
print("發送成功")