Sample code for 30+ languages & platforms
CkPython 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 CkPython Downloads

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat.CkEmail()
success = email.LoadEml("with-attachments.eml")
if (success != True):
    print(email.lastErrorText())
    sys.exit()

filename = chilkat.CkStringBuilder()
mediaType = chilkat.CkStringBuilder()
found = False
i = 0
while (i < email.get_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 = chilkat.CkBinData()
        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.get_NumBytes()) + " bytes.")
        found = True

    i = i + 1

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

i = 0
while (i < email.get_NumRelatedItems()) :
    print("Related: " + email.getRelatedContentType(i) + "  Content-ID: " + email.getRelatedContentID(i))
    i = i + 1