Sample code for 30+ languages & platforms
Chilkat2-Python

Iterate Email Headers

Demonstrates how to iterate over the email header fields.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

email = chilkat2.Email()

# First, load an email from a file. 
# Note: an email object may be loaded from a file, or
# downloaded directly from a POP3 or IMAP server...

success = email.LoadEml("testReceivedHdrs.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

# How many header fields?

n = email.NumHeaderFields
if (n > 0):

    # Display the name and value of each header:

    for i in range(0,n):
        name = email.GetHeaderFieldName(i)
        value = email.GetHeaderFieldValue(i)
        print(str(i))
        print(name)
        print(value)