Sample code for 30+ languages & platforms
PureBasic

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to an IMAP server.
    ; Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Login
    success = CkImap::ckLogin(imap,"myLogin","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; We can choose to fetch UIDs or sequence numbers.
    fetchUids.i = 1

    ; Get the message IDs of all the emails in the mailbox
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Fetch the email headers into a bundle object:
    bundle.i = CkEmailBundle::ckCreate()
    If bundle.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    headersOnly.i = 1
    success = CkImap::ckFetchMsgSet(imap,headersOnly,messageSet,bundle)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        CkEmailBundle::ckDispose(bundle)
        ProcedureReturn
    EndIf

    ; Scan for emails with attachments, and save the attachments
    ; to a sub-directory.
    fullEmail.i = CkEmail::ckCreate()
    If fullEmail.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    emailHeader.i = CkEmail::ckCreate()
    If emailHeader.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While i < CkEmailBundle::ckMessageCount(bundle)
        ; The bundle contains email headers..
        CkEmailBundle::ckEmailAt(bundle,i,emailHeader)

        ; Does this email have attachments?
        ; Use GetMailNumAttach because the attachments
        ; are not actually in the email object because
        ; we only downloaded headers.
        numAttach.i = CkImap::ckGetMailNumAttach(imap,emailHeader)

        If numAttach > 0
            ; Download the entire email and save the
            ; attachments. (Remember, we 
            ; need to download the entire email because
            ; only the headers were previously downloaded.

            ; The ckx-imap-uid header field is added when
            ; headers are downloaded.  This makes it possible
            ; to get the UID from the email object.
            uidStr.s = CkEmail::ckGetHeaderField(emailHeader,"ckx-imap-uid")
            uid.i = Val(uidStr)

            success = CkImap::ckFetchEmail(imap,0,uid,1,fullEmail)
            If success = 0
                Debug CkImap::ckLastErrorText(imap)
                CkImap::ckDispose(imap)
                CkMessageSet::ckDispose(messageSet)
                CkEmailBundle::ckDispose(bundle)
                CkEmail::ckDispose(fullEmail)
                CkEmail::ckDispose(emailHeader)
                ProcedureReturn
            EndIf

            success = CkEmail::ckSaveAllAttachments(fullEmail,"attachmentsDir")

            j.i
            For j = 0 To numAttach - 1
                filename.s = CkImap::ckGetMailAttachFilename(imap,emailHeader,j)
                Debug filename
            Next

        EndIf

        i = i + 1
    Wend

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)
    CkEmailBundle::ckDispose(bundle)
    CkEmail::ckDispose(fullEmail)
    CkEmail::ckDispose(emailHeader)


    ProcedureReturn
EndProcedure