Skip to content

Instantly share code, notes, and snippets.

@helloandie
Created March 6, 2017 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloandie/1a93eac59c94b48dc2ef52dbf939b6b1 to your computer and use it in GitHub Desktop.
Save helloandie/1a93eac59c94b48dc2ef52dbf939b6b1 to your computer and use it in GitHub Desktop.
emailHelper methods
//From Salesforce documentation:
public void sendEmail(String ToAddress) {
// First, reserve email capacity for the current Apex transaction to ensure
// that we won't exceed our daily email limits when sending email after
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(1);
// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.
// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {ToAddress};
// Assign the recipient
mail.setToAddresses(ToAddresses);
// Specify the address used when the recipients reply to the email.
mail.setReplyTo('support@acme.com');
// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');
// Specify the subject line for your email address.
mail.setSubject('New Case Created : + case.Id');
// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);
// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);
// Specify the text content of the email.
mail.setPlainTextBody('Your Case: has been created.');
mail.setHtmlBody('Your case:<b> case.Id </b>has been created.<p>'+
'To view your case <a href=https://***yourInstance***.salesforce.com/+case.Id+>click here.</a>');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
//Simplified from Salesforce documentation
public Messaging.SingleEmailMessage createSampleEmail (String toAddress) {
// create single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// set values for email
String[] toAddresses = new String[] {toAddress};
mail.setToAddresses(ToAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('New Case Created : + case.Id');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Your Case: has been created.');
mail.setHtmlBody('Your case:<b> case.Id </b>has been created.<p>'+
'To view your case <a href=https://***yourInstance***.salesforce.com/+case.Id+>click here.</a>');
return mail;
}
//Template sending using the documentation
public void sendEmailUsingTemplate(String MyTemplateID, String MyRecipientID, String MyReplyTo, String MyDisplayName) {
// First, reserve email capacity for the current Apex transaction to ensure
// that we won't exceed our daily email limits when sending email after
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(1);
// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.
// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Specify the reply-to and display name.
mail.setReplyTo(MyReplyTo);
mail.setSenderDisplayName(MyDisplayName);
mail.setTemplateId(MyTemplateID);
mail.setTargetObjectId(MyRecipientID);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment