Tuesday 31 March 2015

Sending Mass Email Through Apex Controllers

This tip demonstrates how to send mass emails along with attachments(pdf/resources/assets etc) through Apex Controllers.
Apex Controllers

/**
*@purpose : Method for Creating an Attachment
*@param : None
*@return : PageReference to convertpdf Page
**/
public PageReference attachpdf() {

   //Parameters getting from the URL
    Id quoteId = ApexPages.currentPage().getParameters().get('quoteId');
    Id cId = ApexPages.currentPage().getParameters().get('cId');
    Quote quoteObj = [select id,name from Quote where id =:quoteId];

   //Referring from the Pdf page(The page which is rendered as PDF)
    PageReference pdfPage = Page.pdfPage;
    pdfPage.getParameters().put('id',quoteid);
    Blob pdfBlob = pdfPage.getContent();
 
   //Creating a Attachment
    Attachment attachmentObj = new Attachment(parentId = quoteObj.id, name=quoteObj.name + '.pdf', body = pdfBlob);
    INSERT attachmentObj;
    PageReference nextPage = new PageReference('/apex/convertpdf?cId='+cId+'qId='+quoteId);
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'PDF Attached Successfully'));
    return nextPage;
}


/**
*@purpose : Method for Sending Mass mail along with the Attachment
*@param : None
*@return : PageReference to convertpdf Page
**/
public PageReference sendEmail(){  

   //Parameters getting from the URL
   Id quoteId = ApexPages.currentPage().getParameters().get('quoteId');
   Id emailTemplate = ApexPages.currentPage().getParameters().get('emailTemplate');
   Id cId = ApexPages.currentPage().getParameters().get('cId');
   Id aId = ApexPages.currentPage().getParameters().get('aId');
  List<Contact> contactNames = [SELECT id, name FROM Contact WHERE AccountId =: aId];
   for (String obj : contactNames){
      contact = [SELECT Id,name,email FROM Contact WHERE name =:obj];
      for(Integer i=0; i < contact.size(); i++){

       //Initializing for Sending Mails(Written Single because of in a loop)
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

       //Initializing the Email Attachment
         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

       //Attaching them to the mail by setting the parameters
         for (Attachment attachmentObj : [SELECT Name, Body, BodyLength FROM Attachment WHERE ParentId = :quoteId]){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(attachmentObj.Name);
            efa.setBody(attachmentObj.Body);
            fileAttachments.add(efa);
         }

       //Setting the Template and Target Ids
         email.setFileAttachments(fileAttachments);
         email.setTargetObjectId(contact[i].Id);
         email.setTemplateId(emailTemplate.id);

       //Sending the Mail
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
      }
   }
   PageReference nextPage = new PageReference('/apex/convertpdf?quoteId='+quoteId+'&cId='+cId);
   ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email Sent Successfully'));
   return nextPage;
}

IMPORTANT POINTS
1) setFileAttachments = Attaching the files to the Mail
2) setTargetObjectId = Setting the Target Ids(Contacts to whom the mail is to be send)
3) setTemplateId = Format of the Mail by setting the Template Format
4) Messaging.sendEmail = Sending the Mail

#Note: It is good to use "Messaging.SingleEmailMessage"  within a loop rather than "Messaging.MassEmailMessages".

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am really impressed with the details. Well, the businesses are competing on a higher level in the modern age and they should use the best ways that are going to help you connect to them easily. Personally, we have been using the platform that enables the customers to send sms to slack. It is so easy and effective.

    ReplyDelete