/all-gists/5276813/snippet.py

https://github.com/gistable/gistable · Python · 44 lines · 19 code · 6 blank · 19 comment · 2 complexity · 87e8a95b09d03cfb47d1cce30d332f0c MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """emlx.py
  4. Class to parse email stored with Apple proprietary emlx format
  5. Created by Karl Dubost on 2013-03-30
  6. Inspired by Rui Carmo https://the.taoofmac.com/space/blog/2008/03/03/2211
  7. MIT License"""
  8. import email
  9. import plistlib
  10. class Emlx(object):
  11. """An apple proprietary emlx message"""
  12. def __init__(self):
  13. super(Emlx, self).__init__()
  14. self.bytecount = 0
  15. self.msg_data = None
  16. self.msg_plist = None
  17. def parse(self, filename_path):
  18. """return the data structure for the current emlx file
  19. * an email object
  20. * the plist structure as a dict data structure
  21. """
  22. with open(filename_path, "rb") as f:
  23. # extract the bytecount
  24. self.bytecount = int(f.readline().strip())
  25. # extract the message itself.
  26. self.msg_data = email.message_from_bytes(f.read(self.bytecount))
  27. # parsing the rest of the message aka the plist structure
  28. self.msg_plist = plistlib.readPlistFromBytes(f.read())
  29. return self.msg_data, self.msg_plist
  30. if __name__ == '__main__':
  31. msg = Emlx()
  32. message, plist = msg.parse('your_message.emlx')
  33. # print(message)
  34. # Access to one of the email headers
  35. print(message['subject'])
  36. # Access to the plist data
  37. print(plist)