+ Start a Discussion
Sundar rajan ASundar rajan A 

How to send a email with the document in attachment related list as a zip file

Hi Friends,
         My requirement is , I need to send a email with the document in the attachment related list, as a zip file.  How to add attachment by zippex class,

                       Many thanks in Advance
   
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You can do this. Just modify the query accordingly
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'user@dimain.com'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('user@domain.com');
        mail.setSenderDisplayName('SFDC Support');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setTargetObjectId('005Q0000000Fo7f');
       // Give visualforce template id
        mail.setTemplateId('00XQ0000000iULj');
        mail.saveAsActivity = false;    
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);

      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Sundar rajan ASundar rajan A
Hi Shalabh,
       Thank you for your quick responce .I already used the above code It will sending a mail as attached document,I am using the below code It will send seperate zip file attachment for each document.But I need to send a mail as a Single Zip file ,that contains all documents in Attachment related list.



List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

          for (Attachment a : [select Name, Body from Attachment where ParentId = :oppr.Id])
       {
        maildatas =a.Body;
        zip.addFile('b.txt', maildatas, null);
        //finalStrs =a.Body;
          zipBlob = zip.getZipArchive();
       
       

        Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
  
        
        //csvAttc = new Messaging.EmailFileAttachment();
         String zipName = 'cases.zip';
         csvAttc.setFileName(zipName);

        csvAttc.setBody(zipBlob);
       
        fileAttachments.add(csvAttc);
        }
         //fileAttachments.add(csvAttc);

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
          //email = new Messaging.SingleEmailMessage();

        String[] toAddresses = new List<String> {oppr.email__c};

        String subject = 'Report CSV';

        email.setSubject(subject);

        email.setToAddresses( toAddresses );

        email.setPlainTextBody('The Merchandise report is attached here.');
        
        

       // email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
        
        email.setFileAttachments(fileAttachments);

        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});




                                         Many thanks in Advance.
        
 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Zippex works with visualforce pages. Do you have a vf page in this scenario?
Sundar rajan ASundar rajan A
Hi Shalabh,
          I don't have VF Page,I am using Zippex in my apex class.Is there any other solution for send a emil as zip file?
Pedro I Dal ColPedro I Dal Col
Hi Sundar,

You don't need a VF page, the Zippex library works with or without VF. If you want to add all attachments into the same Zip archive, you need to call the addFile() once per each file.

Here is the code you need to use. Make sure you have installed Zippex: https://github.com/pdalcol/Zippex
    Zippex zip = new Zippex();
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    
    for (Attachment a : [SELECT Name, Body FROM Attachment WHERE ParentId = :oppr.Id])
    {
        zip.addFile(a.Name, a.Body, null);
    }

    Blob zipBlob = zip.getZipArchive();

    Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();

    String zipName = 'cases.zip';
    csvAttc.setFileName(zipName);
    csvAttc.setBody(zipBlob);

    fileAttachments.add(csvAttc);

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    String[] toAddresses = new List<String> {oppr.email__c};
    String subject = 'Report CSV';
    email.setSubject(subject);
    email.setToAddresses( toAddresses );
    email.setPlainTextBody('The Merchandise report is attached here.');
    email.setFileAttachments(fileAttachments);

    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});