+ Start a Discussion
MnZ321MnZ321 

Is it possible to send html email from triggers?

Wondering if its possible to send HTML emails only from a trigger. Any help or reference would be helpful. Thanks
Pablo_RoldanPablo_Roldan
Yep, I have done something like that using RemoteAction like this:

global class myGlobalClass{
@RemoteAction
global static void mrGerEmailToPdfFromOwn(){
  // Here we will build the single email message
  Messaging.reserveSingleEmailCapacity(1);
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  String[] toAddresses = new String[]{'any@emailaddr.es'};
  mail.setToAddresses(toAddresses);
  mail.setSaveAsActivity(false);
  mail.setTargetObjectId(UserInfo.getUserId());
  mail.setTemplateId(pEmailTemplate.Id);

  Savepoint sp = Database.setSavepoint();
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
 
Finally you will need to call this method to send the email.

Thanks,
Pablo
SFDC_DevloperSFDC_Devloper
Hi,

  try below code .....

trigger caseUpdationMailNotification on Case (after update) {
    Set<Id> conIds = new Set<Id>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for (Case c: trigger.new) {
        conIds.add(c.ContactId);
    }
    Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
  
    for (Case c : trigger.new) {
        Contact relatedCaseContact = conMap.get(c.ContactId);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new List<String> { relatedCaseContact.Email });
        mail.setReplyTo('ambigaraman@gmail.com');
        mail.setSenderDisplayName('Salesforce Support');          
      
        String oldStatus = trigger.oldMap.get(c.id).status;
        if (c.status != oldStatus) {
          mail.setSubject(' Case Status updation : ' + 'Changed to ' + c.status + '. Case Number:' + c.CaseNumber);
          mail.setPlainTextBody('Your case Status: ' + c.CaseNumber + 'To view your case <a href=https://na1.salesforce.com/' + c.Id);
        }
        else {
           mail.setSubject(' Case  updation : ' + 'Case Number:' + c.CaseNumber);
           mail.setPlainTextBody('Your case : ' + ' has been updated.' + 'To view your case <a href=https://na1.salesforce.com/' + c.Id);
       }
       mails.add(mail);
    }
    Messaging.sendEmail(mails);
}


Thanks,
Rockzz


dev_sfdc1dev_sfdc1
Hi,
Try to do like this:

http://writeforce.blogspot.in/2012/10/sending-email-notification-when.html

Thank You.