Send Birthday Email to Contacts Using Apex Code

// This is Class for Sending the Birthday Email with using Custom Template
// Schedule this class weekly every morning 

public  class SendBirthdayWishesEmail {
    
    Public void sendBirthdayMail()
    {
          //Get date and Month from Today Date 
        Date thisDate = system.Today();
        Integer SysDay = thisDate.Day(); // Get Day from 1 to 31
        Integer SysMonth = thisDate.month(); //get Month from 1 to 12
        String SysDayAndSysMont = SysDay +'/'+SysMonth; // get Day and Month 
     
      
       
        Map<string,EmailTemplate> mapNameToEmailTemplate = new Map<string,EmailTemplate>();
       // OrgWideEmailAddress replyEmail = [SELECT ID, DisplayName FROM OrgWideEmailAddress WHERE Address =: 'sumitshukla.mca@gmail.com'];
        
        for(EmailTemplate emailTemp : [Select id ,subject,body,DeveloperName,HtmlValue  from EmailTemplate where DeveloperName = 'Email Template Name']) // Firstly you need to create Custom Template. In template you can add Image and Text and So on
        {
            mapNameToEmailTemplate.put(emailTemp.DeveloperName,emailTemp);
            
        }
        system.debug('mapNameToEmailTemplate**'+mapNameToEmailTemplate);
            
        List<Contact> lstofBirthCon = new List<Contact>();
        
        List<Messaging.SingleEmailMessage> lstEmailId=new List<Messaging.SingleEmailMessage>();
          
 // Birth_Day_and_Month__c this is formula field :output of this FF is Day/Month of Birthday date
        for(Contact objC : [Select id,Name,Birthdate,Email,
                                    Birth_Day_and_Month__c
                                    from Contact where  
                                    Birth_Day_and_Month__c = :SysDayAndSysMont 
                                    
                                     ])
        {
            if(objC.Birth_Day_and_Month__c != null && objC.Birth_Day_and_Month__c == SysDayAndSysMont)
            {
                lstofBirthCon.add(objC);
            }             
                
        }                                                    
        List<String> toaddress; 
        if(lstofBirthCon !=null && lstofBirthCon.size()>0) 
        { 
       for(Contact ObjCon :lstofBirthCon)
       {
           toaddress = new List<String>();
           if(ObjCon.Email !=null)
           {
               toaddress.add(ObjCon.Email);
           }
         
           system.debug('toaddress*****'+toaddress);
           if(toaddress != null && toaddress.size() > 0)
           {
               if(mapNameToEmailTemplate.get('Email Template Name') != null)
               {
                    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                   email.setSubject(mapNameToEmailTemplate.get('Email Template Name').Subject); //get Email Template Subject 
                   email.setHtmlBody(mapNameToEmailTemplate.get('Email Template Name').HtmlValue );//get Email Template Body Content 
                   email.setToAddresses(toaddress); 
                    email.setReplyTo('care@abc.in');
// By default mail sent from Admin user if you want change from Address you need to add in OrgWideEmailAddress then Below code UnComment and add required email address  
                  // email.setOrgWideEmailAddressId(replyEmail.Id);  
                   lstEmailId.add(email);  
               }
           }  
           
           
       }
        }
        
        
        system.debug('lstEmailId*****'+lstEmailId);
        if(lstEmailId.size()>0)
        {
            Messaging.SendEmail(lstEmailId);
        }
                 
    }

}

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page