function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HareHare 

Send mass email with attachment (attachment need to select from vf page) with template


I have a vf page with email template pick list user able to select email template name
Choose file : user able to upload a file attachment from his computer.
there is a command button send email . once clicking on send email need to send email with selected template with email attachment . please help me on this.. sample code :
<apex:page standardController="Contact" extensions="WrapperClsOnContactMass">
    <apex:form >
    <apex:pageMessages >
    </apex:pageMessages>
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrapperObj}" var="x">
                <apex:column value="{!x.conobj.name}"/>
                <apex:column value="{!x.conobj.email}"/>
                <apex:column >
                  <apex:inputcheckbox value="{!x.checkBox }"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockSection >
            <apex:commandButton value="SendEmail" action="{!sendEmail}"/>
            
            </apex:pageBlockSection>
            </apex:pageBlock>
    </apex:form>
</apex:page>
--------------------------------
public with sharing class WrapperClsOnContactMass {

    public List<WrapperClassEx> WrapperList{get;set;}
    
    public WrapperClsOnContactMass(ApexPages.StandardController controller) {
        
    }
     
    public List<WrapperClassEx> getwrapperObj(){
        List<Contact> conList =[select id, name, email from contact limit 5];   
        WrapperList = new List<WrapperClassEx>();
        for(Contact con: conList){
            
            
            WrapperList.add(New WrapperClassEx(con,false));  
        }
        return WrapperList;  
    }
    
    public class WrapperClassEx{
        
       public WrapperClassEx(){
            
        }
       public Contact conObj{get;set;}
       public Boolean checkBox{get;set;}
       public WrapperClassEx(Contact conRec, boolean SelectedBox){               
          conObj= conRec;
          checkBox = SelectedBox;
       }
    }
        
       public void sendEmail(){
         List<Messaging.SingleEmailMessage> lstEmailId=new List<Messaging.SingleEmailMessage>();
         for(WrapperClassEx w: WrapperList){
            if(w.checkBox == true){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(new String[] {w.conObj.Email});
                mail.setReplyTo('sumit.shukla@magicsw.com');
                mail.setplainTextBody('Hello');
                mail.setSenderDisplayName('Your Company Name');
                mail.setSubject('Test Email From Force.com Sites');
                lstEmailId.add(mail);                        
            }
          }
            if(lstEmailId.size()>0){
                try{
                    Messaging.sendEmail(lstEmailId);
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'Sent!'));
                }Catch(Exception ee){
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error,ee.getMessage()));
                }
                
            }
       }     
}