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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

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()

filename = chilkat2.StringBuilder()
mediaType = chilkat2.StringBuilder()
found = False
i = 0
while (i < email.NumAttachments) :
    filename.SetString(email.GetAttachmentFilename(i))
    mediaType.SetString(email.GetAttachmentContentType(i))
    if ((filename.ContentsEqual("agenda.csv",False) == True) and (mediaType.ContentsEqual("text/csv",False) == True) and (found == False)):
        data = chilkat2.BinData()
        success = email.GetAttachmentBd(i,data)
        if (success != True):
            print(email.LastErrorText)
            sys.exit()

        success = data.WriteFile("received-agenda.csv")
        if (success != True):
            print("Failed: data.WriteFile.")
            sys.exit()

        print("Saved attachment: " + str(data.NumBytes) + " bytes.")
        found = True

    i = i + 1

if (found != True):
    print("No matching attachment.")

i = 0
while (i < email.NumRelatedItems) :
    print("Related: " + email.GetRelatedContentType(i) + "  Content-ID: " + email.GetRelatedContentID(i))
    i = i + 1