Sample code for 30+ languages & platforms
Chilkat2-Python

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

emlPath = "C:/AAWorkarea/beatrix/roesner.eml"

mime = chilkat2.Mime()

success = mime.LoadMimeFile(emlPath)
if (success == False):
    print(mime.LastErrorText)
    sys.exit()

print("---- MIME structure ----")
print(mime.GetStructure("text"))
print("------------------------")

email = chilkat2.Email()
success = email.LoadEml(emlPath)

# Was this a signed and/or encrypted email?
# If so, then loading the .eml automatically unwraps
# (i.e. verifies signatures and decrypts) and the resultant
# email is what existed prior to signing/encrypting.
print("Email was Signed: " + str(email.ReceivedSigned))
print("Email was Encrypted: " + str(email.ReceivedEncrypted))
if (email.ReceivedSigned == True):
    print("Signature(s) valid = " + str(email.SignaturesValid))

if (email.ReceivedEncrypted == True):
    print("Decrypted successfully = " + str(email.Decrypted))

i = 0
numAttach = email.NumAttachments
print("Number of attachments = " + str(numAttach))

while i < numAttach :
    print("---- Attachment " + str(i))

    # Examine the filename (if any)
    print("filename: " + email.GetAttachmentFilename(i))
    # Examine the content-ID (if any)
    print("Content-ID: " + email.GetAttachmentContentID(i))
    # Examine the content-type
    print("Content-Type: " + email.GetAttachmentContentType(i))
    # Examine the content-disposition
    print("Content-Disposition" + email.GetAttachmentHeader(i,"content-disposition"))
    # Examine the attachment size:
    print("Size (in bytes) of the attachment: " + str(email.GetAttachmentSize(i)))

    i = i + 1

print("--")

# Now for the related items.

# Note: A MIME sub-part can potentially be both a related item AND an attachment.
# The typical case is when the item is contained under the multipart/related enclosure and 
# the item also has a "Content-Disposition" header indicating "attachment".
# The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
# Related items and attachments are not necessarily mutually exclusive.

numRelated = email.NumRelatedItems
print("Number of related items = " + str(numRelated))
i = 0
while i < numRelated :
    print("---- Related Item " + str(i))

    # Examine the filename (if any)
    print("filename: " + email.GetRelatedFilename(i))
    # Examine the content-ID (if any)
    print("Content-ID: " + email.GetRelatedContentID(i))
    # Examine the content-type
    print("Content-Type: " + email.GetRelatedContentType(i))
    # Examine the content-location (if any)
    print("Content-Location" + email.GetRelatedContentLocation(i))

    i = i + 1

# The email could also have attached messages.
# An attached message is another email that was attached to this email.
em = chilkat2.Email()
numAttachedMessages = email.NumAttachedMessages
print("Number of attached messages = " + str(numAttachedMessages))
i = 0
while i < numAttachedMessages :
    print("---- Attached message " + str(i))

    # Examine the attached email
    email.GetAttachedEmail(i,em)
    print("from: " + em.From)
    print("subject: " + em.Subject)
    i = i + 1

# An email could also be a multipart/report email. 
# This is a DSN (Delivery Status Notification)
# The NumReports property indicates how many "reports" exist.
numReports = email.NumReports
print("Number of reports = " + str(numReports))
i = 0
while i < numReports :
    print("---- Report " + str(i))
    # Get the raw report data...
    print(email.GetReport(i))
    i = i + 1

# If the email is a multipart/report, then the information
# from the message/delivery-status part of the email can be retrieved:
if (email.IsMultipartReport() == True):

    print("--- Delivery Status Information:")
    print("Status: " + email.GetDeliveryStatusInfo("Status"))
    print("Action: " + email.GetDeliveryStatusInfo("Action"))
    print("Reporting-MTA: " + email.GetDeliveryStatusInfo("Reporting-MTA"))

    jsonDsnInfo = chilkat2.JsonObject()
    email.GetDsnInfo(jsonDsnInfo)
    jsonDsnInfo.EmitCompact = False
    print(jsonDsnInfo.Emit())