I have a requirement where I am sending email via batch class. I have created an email template and I want to send data to the merge fields of the template from the code that I am writing. Since the mail will be sent for more than 1 record so I am using a for loop for the Messaging.SingleEmailMessage. The issue is with the HTMLBody component. In the for loop I am observing that the HTMLBody is iterating but the merge fields are not getting captured accordingly. This implies I am receiving email for all the expected records but the email content keeps repeating only for the first record. Please let me know how will I be able to send the email for all the concerned records and the email template will have the data accordingly.
Below is the part of the email code that I have been writing :-
for(Test__c res : finalResourceEmailList){
htmlBody = htmlBody.replace('{!Test__c.Name}', res.Name);
htmlBody = htmlBody.replace('{!Test__c.Notification_Date__c}', String.valueof(res.Notification_Date__c));
htmlBody = htmlBody.replace('{!Test__c.Id}', res.Id);
plainBody = plainBody.replace('{!Test__c.Name}',res.Name);
plainBody = plainBody.replace('{!Test__c.Notification_Date__c}',String.valueof(res.Notification_Date__c));
plainBody = plainBody.replace('{!Test__c.Id}', res.Id);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(et.Id);
mail.setTargetObjectId(res.OwnerId);
mail.setSubject(et.Subject);
mail.setHTMLBody(htmlBody);
mail.setPlainTextBody(plainBody);
mail.setSaveAsActivity(false);
mail.setUseSignature(false);
allmsg.add(mail);
}
if(allmsg.size() > 0 ){
Messaging.SendEmailResult[] results = Messaging.sendEmail(allmsg);
}