微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python实现邮件循环自动发件

发邮件是一种很常见的操作,本篇主要介绍一下如何用python实现自动发件。

import smtplib
from email.mime.text  MIMEText
from email.mime.multipart  MIMEMultipart
from email.header  Header
from email.mime.image  MIMEImage
 time
mail_host="smtp.126.com"
mail_user=[email protected]
mail_pass=******#注意如果邮箱开启了授权码,此处要填写授权码,否则会报smtplib.SMTPAuthenticationError: (535,b'Error: authentication failed')
sender=
receiver = ['邮箱1',邮箱2']群发邮件
for i in range(n):自定义循环发多少遍
    try:
        message = MIMEMultipart()
        message[From"] = Header(sender)
        message[To"] = ,'.join(receiver)
        message[Subject"] = Header(主题",1)">utf-8").encode()主题
        message.attach(MIMEText(正文plain"))正文
        """
        定附件
        """
        att = MIMEText(open(rC:\Users\Administrator\Desktop\1.txt').read(),1)">base64)
        att[Content-Typeapplication/octet-stream
            att.add_header(Content-Dispositionattachment1.txt")这一步可避免文件不能正常打开
        message.attach(att)
        
        构造图片(以附件形式上传)
        
        image = MIMEImage(open(rC:\Users\Administrator\Desktop\1.jpgrb).read())
        image.add_header(Content-ID<image1>')可避免图片不能正常打开
        image[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['].split(),message.as_string())
        print(在%s第" % ctime(),str(i+1),1)">封邮件发送)
        smtp.quit()
    except smtplib.SMTPException as e:
          raise e

最终实现

在这里插入图片描述

 

本文首发于python黑洞网,博客园同步更新

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐