• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python3实现带多张图片、附件的邮件发送

python 搞代码 4年前 (2022-01-07) 66次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python3实现带多张图片、附件的邮件发送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python3实现多张图片附件邮件发送的具体代码,供大家参考,具体内容如下

直接上代码,没有注释!

 from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.header import Header class Mail(object): def __init__(self, host, nickname, username, password, postfix): self.host = host self.nickname = nickname self.username = username self.password = password self.postfix = postfix def send_mail(self, to_list, subje<b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>ct, content, cc_list=[], encode='gbk', is_html=True, images=[]): me = str(Header(self.nickname, encode)) + "" msg = MIMEMultipart() msg['Subject'] = Header(subject, encode) msg['From'] = me msg['To'] = ','.join(to_list) msg['Cc'] = ','.join(cc_list) if is_html: mail_msg = '' for i in range(len(images)): mail_msg += '' % (i+1) msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8')) for i, img_name in enumerate(images): with open(img_name, 'rb') as fp: img_data = fp.read() msg_image = MIMEImage(img_data) msg_image.add_header('Content-ID', '' % (i+1)) msg.attach(msg_image) # 将图片作为附件 # image = MIMEImage(img_data, _subtype='octet-stream') # image.add_header('Content-Disposition', 'attachment', filename=images[i]) # msg.attach(image) else: msg_content = MIMEText(content, 'plain', encode) msg.attach(msg_content) try: s = smtplib.SMTP() # s.set_debuglevel(1) s.connect(self.host) s.login(self.username, self.password) s.sendmail(me, to_list + cc_list, msg.as_string()) s.quit() s.close() return True except Exception as e: print(e) return False def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]): content = '<pre class="prettyprint linenums">%s

‘ % content m = Mail(‘smtp.163.com’, ‘TV-APP TEST’, ‘tvapp_qa’, ‘ujlnluutpfespgxz’, ‘163.com’) m.send_mail(to_list, title, content, cc_list, encode, is_html, images) if __name__ == ‘__main__’: images = [ ‘1.png-600’, ‘2.png-600’, ‘3.png-600’, ‘4.png-600’, ] import time title = ‘new images %s’ % time.strftime(‘%H:%M:%S’) content = ‘this is attach images %s’ % time.time() send_mail([‘[email protected]’], title, content, [‘[email protected]’, ‘[email protected]’], ‘utf-8’, True, images)

后记

调试发送多张图片的时候遇到的问题:

用for循环生成的mail_msg,不能直接attach,需要和content一起attach

 mail_msg = '' for i in range(len(images)): mail_msg += '' % (i+1) msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8')) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是python3实现带多张图片、附件的邮件发送的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python3实现带多张图片、附件的邮件发送

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址