Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Zip the attachments"

    Get ComAddStringAttachment Of hoEmail "a.txt" "first attachment" To iSuccess
    Get ComAddStringAttachment Of hoEmail "b.txt" "second attachment" To iSuccess
    Get ComNumAttachments Of hoEmail To iTemp1
    Showln "NumAttachments before = " iTemp1

    //  Replace all attachments with a single Zip attachment named "files.zip".
    Get ComZipAttachments Of hoEmail "files.zip" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumAttachments Of hoEmail To iTemp1
    Showln "NumAttachments after = " iTemp1


End_Procedure