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

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

email = Chilkat::CkEmail.new()
success = email.LoadEml("with-attachments.eml")
if (success != true)
    print email.lastErrorText() + "\n";
    exit
end

filename = Chilkat::CkStringBuilder.new()
mediaType = Chilkat::CkStringBuilder.new()
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.new()
        success = email.GetAttachmentBd(i,data)
        if (success != true)
            print email.lastErrorText() + "\n";
            exit
        end

        success = data.WriteFile("received-agenda.csv")
        if (success != true)
            print "Failed: data.WriteFile." + "\n";
            exit
        end

        print "Saved attachment: " + data.get_NumBytes().to_s() + " bytes." + "\n";
        found = true
    end

    i = i + 1
end
if (found != true)
    print "No matching attachment." + "\n";
end

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