Sample code for 30+ languages & platforms
PureBasic

Add Email Attachment from FTP

Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.

Note: This example requires Chilkat v9.5.0.63 or later.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkFtp2.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ; Note: This example requires Chilkat v9.5.0.63 or later.

    ; This example assumes Chilkat Ftp2 to have been previously unlocked.
    ; See Unlock Ftp2 for sample code.

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

    CkFtp2::setCkHostname(ftp, "www.my-ftp-server.com")
    CkFtp2::setCkUsername(ftp, "mFtpLogin")
    CkFtp2::setCkPassword(ftp, "myFtpPassword")

    ; Connect to the FTP server.
    success = CkFtp2::ckConnectOnly(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Authenticate with the FTP server.
    success = CkFtp2::ckLoginAfterConnectOnly(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Move to the remove directory where our file is located.
    success = CkFtp2::ckChangeRemoteDir(ftp,"qa_data")
    If success = 1
        success = CkFtp2::ckChangeRemoteDir(ftp,"pdf")
    EndIf

    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Download...
    pdfData.i = CkBinData::ckCreate()
    If pdfData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFtp2::ckGetFileBd(ftp,"fishing.pdf",pdfData)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkBinData::ckDispose(pdfData)
        ProcedureReturn
    EndIf

    CkFtp2::ckDisconnect(ftp)

    ; Create an email object, and add the PDF as an attachment.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Test with PDF attachment.")
    CkEmail::setCkBody(email, "This is a plain-text body..")

    ; Add the PDF attachment.  (This method call requires Chilkat v9.5.0.63 or later)
    success = CkEmail::ckAddAttachmentBd(email,"fishing.pdf",pdfData,"application/pdf")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkFtp2::ckDispose(ftp)
        CkBinData::ckDispose(pdfData)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; Save the email and examine with a text editor to see the PDF attachment is present..
    CkEmail::ckSaveEml(email,"qa_output/out.eml")

    Debug "Success."


    CkFtp2::ckDispose(ftp)
    CkBinData::ckDispose(pdfData)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure