Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Last active July 19, 2021 17:09
Show Gist options
  • Save TheShubhamVsnv/6886477a58a112047ebc59659bbc6a23 to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/6886477a58a112047ebc59659bbc6a23 to your computer and use it in GitHub Desktop.
Sending eamil from apex trigger
// Send a Email to each new Contact
trigger ContactEmailTrigger on contact (before insert) {
//get all email in the list
List<Messaging.SingleEmailMessage> mails =
new List<Messaging.SingleEmailMessage>();
for (Contact myContact : Trigger.new) {
if (myContact.Email != null && myContact.FirstName != null) {
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(myContact.Email);
mail.setToAddresses(sendTo);
// Step 3. Set email contents - you can use variables!
mail.setSubject('Your contact detail are added'); //Subject of the mail and the body of the mail
String body = 'Dear ' + myContact.FirstName + ', ';
body += 'According to the ContactEmailTrigger trigger';
body += 'your contact details were added successful';
body += 'For more details you can visit ';
body += ' https://salesforceforfresher.wordpress.com/ ';
mail.setHtmlBody(body);
// Step 4. Add your email to the master list
mails.add(mail);
}
}
// Step 5: Send all emails in the master list
Messaging.sendEmail(mails);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment