当前位置:   article > 正文

python处理eml格式的邮件_python eml

python eml

对于数据进行读取

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2020/2/29/029 22:03
  4. # @Author : H
  5. # @File : getEmailHeader.py
  6. import os
  7. import re
  8. from email.parser import Parser
  9. def read_mail(path):
  10. if os.path.exists(path):
  11. with open(path) as fp:
  12. email = fp.read()
  13. return email
  14. else:
  15. print("file not exist!")
  16. def emailInfo(emailpath):
  17. raw_email = read_mail(emailpath) # 将邮件读到一个字符串里面
  18. print('emailpath : ', emailpath)
  19. emailcontent = Parser().parsestr(raw_email) # 经过parsestr处理过后生成一个字典
  20. # for k,v in emailcontent.items():
  21. # print(k,v)
  22. From = emailcontent['From']
  23. To = emailcontent['To']
  24. Subject = emailcontent['Subject']
  25. Date = emailcontent['Date']
  26. MessageID = emailcontent['Message-ID']
  27. XOriginatingIP = emailcontent['X-Originating-IP']
  28. if "<" in From:
  29. From = re.findall(".*<(.*)>.*", From)[0]
  30. if "<" in To:
  31. To = re.findall(".*<(.*)>.*", To)[0]
  32. print("From:\t", From)
  33. print("X-Originating-IP", XOriginatingIP)
  34. print("To:\t", To)
  35. print("Subject:\t", Subject)
  36. print("Message-ID:\t", MessageID)
  37. print("Date:\t", Date)
  38. # 循环信件中的每一个mime的数据块
  39. for par in emailcontent.walk():
  40. if not par.is_multipart(): # 这里要判断是否是multipart,是的话,里面的数据是无用的
  41. content = par.get_payload(decode=True)
  42. # print(str(content,"utf-8",errors='ignore'))
  43. print("content:\t", content.decode(encoding='gbk')) # 解码出文本内容,直接输出来就可以了。
  44. if __name__ == '__main__':
  45. email = "请求预推免系统解锁.eml"
  46. emailInfo(email)
  47. _str = 'str'
  48. print(type(_str))
  49. # 输出为 <class 'str'>
  50. _bytes = b'bytes'
  51. print(type(_bytes))
  52. # 输出为<class 'bytes'>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/1003813
推荐阅读
相关标签
  

闽ICP备14008679号