Sample code for 30+ languages & platforms
Python Requires Chilkat v11.0.0+

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat2.Email()
success = email.LoadEml("with-attachments.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

print("From: " + email.From)
print("Subject: " + email.Subject)
print("Date: " + email.EmailDateStr)
i = 0
while (i < email.NumTo) :
    print("To: " + email.GetToName(i) + "  " + email.GetToAddr(i))
    i = i + 1

referenceId = email.GetHeaderField("X-Reference-ID")
if (email.LastMethodSuccess == True):
    print("Reference: " + referenceId)

if (email.HasPlainTextBody() == True):
    print(email.GetPlainTextBody())
else:
    if (email.HasHtmlBody() == True):
        reader = chilkat2.HtmlToText()
        print(reader.ToText(email.GetHtmlBody()))
    else:
        print("No text or HTML body.")