You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17 lines
574 B

  1. # Import the email modules we'll need
  2. from email.parser import Parser
  3. # If the e-mail headers are in a file, uncomment this line:
  4. #headers = Parser().parse(open(messagefile, 'r'))
  5. # Or for parsing headers in a string, use:
  6. headers = Parser().parsestr('From: <user@example.com>\n'
  7. 'To: <someone_else@example.com>\n'
  8. 'Subject: Test message\n'
  9. '\n'
  10. 'Body would go here\n')
  11. # Now the header items can be accessed as a dictionary:
  12. print('To: %s' % headers['to'])
  13. print('From: %s' % headers['from'])
  14. print('Subject: %s' % headers['subject'])