Sample code for 30+ languages & platforms
C

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

C
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;

    success = FALSE;

    //  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 = CkEmail_Create();
    CkEmail_putSubject(email,"Zip the attachments");

    CkEmail_AddStringAttachment(email,"a.txt","first attachment");
    CkEmail_AddStringAttachment(email,"b.txt","second attachment");
    printf("NumAttachments before = %d\n",CkEmail_getNumAttachments(email));

    //  Replace all attachments with a single Zip attachment named "files.zip".
    success = CkEmail_ZipAttachments(email,"files.zip");
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    printf("NumAttachments after = %d\n",CkEmail_getNumAttachments(email));


    CkEmail_Dispose(email);

    }