Sample code for 30+ languages & platforms
PureBasic

Zip an Email's Attachments into One File

See more Email Object Examples

Demonstrates the Chilkat Email.ZipAttachments method, which replaces all of an email's attachments with a single Zip file attachment having the specified filename. The original attachments are removed and packed into the Zip. This example adds two attachments and zips them into one.

Background: Bundling multiple attachments into a single .zip keeps a message tidy, compresses the payload, and works around recipients or gateways that limit the number of attachments. Note that the filename here (files.zip) names the attachment inside the message — it is not a path on disk. The reverse operation is UnzipAttachments.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the ZipAttachments method, which replaces all of an email's attachments
    ;  with a single Zip file attachment having the specified filename.  The original
    ;  attachments are removed and packed into the Zip.

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

    CkEmail::setCkSubject(email, "Zip the attachments")

    CkEmail::ckAddStringAttachment(email,"a.txt","first attachment")
    CkEmail::ckAddStringAttachment(email,"b.txt","second attachment")
    Debug "NumAttachments before = " + Str(CkEmail::ckNumAttachments(email))

    ;  Replace all attachments with a single Zip attachment named "files.zip".
    success = CkEmail::ckZipAttachments(email,"files.zip")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "NumAttachments after = " + Str(CkEmail::ckNumAttachments(email))


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure