I have a batch which I use to send emails to contacts.

Every contact should have a different template - So I Created a body and subject in my apex class which every contact will have his unique body- for Example:

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            String body = '';
            body+= proposalInformation(Some SObject);

private String proposalInformation(SObject a){
            body += '<p style="font-weight:bold; color:red; text-align: center;">' + a.Typo__c  +  '</p>' ;
            body+= '</br></br>';
            body += '<p style="font-weight:bold; text-align: center;">' + a.FirstName__c  +  '</p>' ;
            body+= '</br></br>';
            body+= '<p style="text-align: center;">Submission Deadline : ' + a.date_and_time__c + '</p>';
            body+= '</br></br>';
            body+= '<p style="text-align: center;"> Description : ' + a.Description__c+ '</p>';
            body+= '</br></br>';
            body+= '<p style="text-align: center;">Amount  : ' + a.Total_amount__c+ '</p>';
            body+= '</br></br>';
            body+= ' <p style="text-align: center;">Links : ' + a.link__c+ '</p>';
            body+= '</br></br>';
            body+= '<p style="text-align: center;"> ---------------------------------------------------------- </p>';
            body+= '</br></br>';

  return body;

If I want to add a global < style > < / style > and than add a class to the divs instead of the inline css, how can I do it?

For example:


and then : body += '<p class="cc">' + a.Typo__c + '</p>' ;

Is that a way to involve Bootstrap here for pretty border around the email?

Is there a way to define an email template and in the code just replace the body?

Thanks in advance.

1 Answer 1


Firstly, Create the custom HTML email template where you can easily apply css and use the merge field like '{!Status}' into the template.

In apex, query this email template and get the body into string variable. Then replace the string with dynamic data.

  • Thanks! I did it with VF email Template, I didn't have time to response the answer so the best answer will go to you. Thanks!
    – Salvation
    Sep 2, 2019 at 8:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .