+ Start a Discussion
kevinjia1984kevinjia1984 

Format Email Body

Hi all,

 

I have a trigger to send  emails to the email addresses listed in the "toAddresses" .And all the body message goes within the variable named 'body'. Is there any possible to format the content in the body to make it looks like a table? The following is the email sending part of my code. Thanks in advance for any help

 

 

//Static email subject information
 String subject = Client_Name + '- Project Status Report - ' + Report_Date;
 
 //Construct email body
 String body = 'Report Generated by:   ' + UserName + '<BR />'
 + 'Reporting For:  '+ Report_Date + '<BR />' + '<BR />' +'Client Name: ' +Client_Name + '<BR />'
 +  'Overall Status: '+ Overall_Status + '<BR />' + 'Phase: ' + Phase + '<BR />'
 + 'Actions Today: '+ Actions + '<BR />'+ 'Yellow Flags: '+ Yellow_Flags+'<BR />'
 + 'Action Plan: ' + Action_Plan + '<BR />' 
 
 //Send Email
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 String[] toAddresses = statusReportEmails.get(sReport.Id).split(';');
 mail.setToAddresses(toAddresses);
 mail.setSubject(subject);
mail.setSaveAsActivity(true);
mail.setHtmlBody(body);

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

HTML is a string formatted for rendering by a client. Strings do not care about their content, so it can be HTML without a problem.

All Answers

sfdcfoxsfdcfox

Just use the normal HTML tags: table, tbody, tr, th, td, and caption (if necessary).

 

 

kevinjia1984kevinjia1984

Hi sfdcfox,

 

Thanks for your reply. But how can I realize that, the data type of body is String, I think it is not possible putting html tag in the string. Which type I should use to define body?

sfdcfoxsfdcfox

HTML is a string formatted for rendering by a client. Strings do not care about their content, so it can be HTML without a problem.

This was selected as the best answer
kevinjia1984kevinjia1984

Thanks a lot for your help

kevinjia1984kevinjia1984

Just one further question. How can I replace the variable in the string? Thanks

sfdcfoxsfdcfox

Apex Code does not support "inline variable substitution" such as PHP, Perl, Ruby, et al. You could, however, use a known pattern to do string replacements, similar to Drupal's translation system:

 

 

string myparam1 = 'hello', myparam2 = 'world';
// construct your base, unsubstituted string body.
string body = 'text-text-text-#param1-text-text-#param2-text-text';
// construct a list of "inline" substitutions to perform.
map<string,string> variables = new map<string,string>{'param1'=>myParam1,'param2'=>myParam2};
// perform each of the substitutions.
for(String s:variables.keySet()) {
  body = body.replace('#'+s,variables.get(s));
}

 

 

kevinjia1984kevinjia1984

Many thanks for your help.

Ramesh SomalagariRamesh Somalagari