经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
python3实现带多张图片、附件的邮件发送
来源:jb51  时间:2019/8/12 8:38:21  对本文有异议

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

直接上代码,没有注释!

  1. from email.mime.text import MIMEText
  2. from email.mime.image import MIMEImage
  3. from email.mime.multipart import MIMEMultipart
  4. from email.header import Header
  5.  
  6. class Mail(object):
  7. def __init__(self, host, nickname, username, password, postfix):
  8. self.host = host
  9. self.nickname = nickname
  10. self.username = username
  11. self.password = password
  12. self.postfix = postfix
  13.  
  14. def send_mail(self, to_list, subject, content, cc_list=[], encode='gbk', is_html=True, images=[]):
  15. me = str(Header(self.nickname, encode)) + "<" + self.username + "@" + self.postfix + ">"
  16. msg = MIMEMultipart()
  17. msg['Subject'] = Header(subject, encode)
  18. msg['From'] = me
  19. msg['To'] = ','.join(to_list)
  20. msg['Cc'] = ','.join(cc_list)
  21. if is_html:
  22. mail_msg = ''
  23. for i in range(len(images)):
  24. mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
  25. msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8'))
  26.  
  27. for i, img_name in enumerate(images):
  28. with open(img_name, 'rb') as fp:
  29. img_data = fp.read()
  30. msg_image = MIMEImage(img_data)
  31. msg_image.add_header('Content-ID', '<image%d>' % (i+1))
  32. msg.attach(msg_image)
  33. # 将图片作为附件
  34. # image = MIMEImage(img_data, _subtype='octet-stream')
  35. # image.add_header('Content-Disposition', 'attachment', filename=images[i])
  36. # msg.attach(image)
  37. else:
  38. msg_content = MIMEText(content, 'plain', encode)
  39. msg.attach(msg_content)
  40.  
  41. try:
  42. s = smtplib.SMTP()
  43. # s.set_debuglevel(1)
  44. s.connect(self.host)
  45. s.login(self.username, self.password)
  46. s.sendmail(me, to_list + cc_list, msg.as_string())
  47. s.quit()
  48. s.close()
  49. return True
  50. except Exception as e:
  51. print(e)
  52. return False
  53.  
  54. def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]):
  55. content = '<pre>%s</pre>' % content
  56. m = Mail('smtp.163.com', 'TV-APP TEST', 'tvapp_qa', 'ujlnluutpfespgxz', '163.com')
  57. m.send_mail(to_list, title, content, cc_list, encode, is_html, images)
  58.  
  59.  
  60. if __name__ == '__main__':
  61. images = [
  62. '1.png',
  63. '2.png',
  64. '3.png',
  65. '4.png',
  66. ]
  67. import time
  68. title = 'new images %s' % time.strftime('%H:%M:%S')
  69. content = 'this is attach images %s' % time.time()
  70. send_mail(['x@163.com'], title, content, ['xx@163.com', 'xxx@163.com'], 'utf-8', True, images)

后记

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

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

  1. mail_msg = ''
  2. for i in range(len(images)):
  3. mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
  4. msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8'))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号