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
davecrusodavecruso 

Sending mail in Apex Code - How best to load text for normal-sized, formatted, e-mail?

Hi all, 

 

 

We're sending a thank-you e-mail to individuals who sign up through our sites-based system. The mail is sent through a controller (see code below).

 

 

try{ //String[] ccAddresses = new String[]{'user@gmail.com'}; mail.setToAddresses(toAddresses); //mail.setCcAddresses(ccAddresses); mail.setReplyTo(VMEmail); mail.setSenderDisplayName('Name'); mail.setSubject('Application Confirmation'); mail.setPlainTextBody('It is done - this text needs to be MUCH longer and more complex, with html code too'); Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail}); }catch(Exception e){ String url = '/apex/exception?err=line135' + e.getmessage(); PageReference r = new PageReference(url); return r; }

 

 

 

 

What is the simplest way to send a mid-sized e-mail that includes some HTML formatting? Should we pop all of the escaped text and HTML into the Apex controller? Place in an external file? If so, how do we place in an external file?

 

Thanks for your suggestions!

--Dave 

Best Answer chosen by Admin (Salesforce Developers) 
Edwin VijayEdwin Vijay

There is a differen way.. not sure if it would work... but worth giving a try and its easy too...

 

 

Create a Email Template.. In this case your email template should not have any merge fields...

 

In your Apex Class.. you can query the EmailTemplate object and get the Body of the particular Email Template and set it as your body..

 

Hope this helps.. 

All Answers

JimRaeJimRae

I have always put all of the text and HTML tags directly in the controller.

I suppose you could create a storage object that contains an identifier field and a textarea field and pull it in from there, but I have never done it that way.

Edwin VijayEdwin Vijay

There is a differen way.. not sure if it would work... but worth giving a try and its easy too...

 

 

Create a Email Template.. In this case your email template should not have any merge fields...

 

In your Apex Class.. you can query the EmailTemplate object and get the Body of the particular Email Template and set it as your body..

 

Hope this helps.. 

This was selected as the best answer
davecrusodavecruso

Hey Edwin,

 

Yes, that's what we're going to do in the end. It'll take some figuring but the Apex e-mail template seems to offer the most flexibility when integrated as a component into a class, rather than using a trigger (etc) to send a Salesforce-standard e-mail template (more options to bring in data from multiple objects through class functions). 

 

Thanks for the suggestions!

--Dave

jkucerajkucera

In case you hadn't thought of it - if you're creating Leads from your sign up, you could use auto-response rules as well.