[关闭]
@rickyChen 2017-01-12T11:37:29.000000Z 字数 913 阅读 3227

Python发送图片邮件且发送多人抄送多人

Python


  1. # encoding: utf-8
  2. from email.mime.image import MIMEImage
  3. from email.mime.multipart import MiMEMultipart
  4. import stmplib
  5. class SendMail:
  6. def __init__(self, host, user, passwd):
  7. self._user = user
  8. self._account = user.split("@")[0]
  9. self._me = "{}<{}>".format(self._account, self._user)
  10. server = stmplib.SMTP()
  11. server.connect(host)
  12. server.login(self._account, passwd)
  13. self._server = server
  14. def sendImageMail(self, to_list, cc_list, sub, content, pic_dict,
  15. subtype='html'):
  16. # 创建一个带附件的实例
  17. msg = MIMEMultipart('related')
  18. # 添加邮件内容
  19. msg.attach(MIMEText(Content, _subtype=subtype, _charset='utf-8'))
  20. for pic in pic_dict.keys():
  21. image = MIMEImage(open(pic_dict[pic], 'rb').read())
  22. image.add_header('Content-ID', pic)
  23. msg.attach(image)
  24. msg['Subject'] = sub
  25. msg['From'] = self._me
  26. # 收件人与抄送人
  27. msg['To'] = ";".join(to_list)
  28. msg['Cc'] = ";".join(cc_list)
  29. try:
  30. # 抄送人与收件人须在同一个列表中
  31. self._server.sendmail(self._me, to_list + cc_list, msg.as_string())
  32. return True
  33. except Exception, e:
  34. print str(e)
  35. return False
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注