python自动发送邮件

python自动发送邮件,第1张

import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
mailUser = "你的qq号啦,或者啥邮箱的都行"
mailPass = "用你自己的哈"
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.login(mailUser, mailPass)
sender = "你的qq号"
receiverDict = {收件人,自己填}
path = "附件路径,自己写"
for receiver in receiverDict:
    message = MIMEMultipart()
    message["From"] = Header(f"<{sender}>")
    message["To"] = Header(f"{receiver}<{receiverDict[receiver]}>")
    message["Subject"] = Header(f"{receiver}-发送给最爱的人")
    mailContent = MIMEText(f"Dear {receiver} 邀请你看最美的照片", "plain", "utf-8")
    with open(path, "rb") as imageFile:  
        fileContent = imageFile.read()
    att = MIMEImage(fileContent)
    att.add_header("Content-Disposition", "attachment", filename="结婚照.jpg")
    message.attach(mailContent)
    message.attach(att)
    smtpObj.sendmail(sender, receiverDict[receiver], message.as_string())
    print("发送over")

话不多说,不会的话也别问我,我是小白。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/730985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存