# MIME Header Parsing : email « Network « Python Tutorial






import sys, email, codecs
from email import Header

msg = email.message_from_file(sys.stdin)
for header, value in msg.items():
    headerparts = Header.decode_header(value)
    headerval = []
    for part in headerparts:
        data, charset = part
  if charset is None:
      charset = 'ascii'
  dec = codecs.getdecoder(charset)
  enc = codecs.getencoder('iso-8859-1')
  data = enc(dec(data)[0])[0]
  headerval.append(data)
    print "%s: %s" % (header, " ".join(headerval))








21.25.email
21.25.1.MIME message generation with 8-bit headers
21.25.2.# MIME Header Parsing
21.25.3.MIME Message Parsing
21.25.4.Message Generation with Date and Message-ID
21.25.5.Message Generation
21.25.6.Message Parsing