Instead of using the built-in SysEmailTable::sendMail() method for replacing placeholders in an email’s body and subject before sending it, you can use the DocEmailTemplateManager::sendMail() method that offers you more options such as an email body with dynamic tables, multiple email attachments, using Cc and Bcc recipients, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class DocEmailTemplateManager { /// <summary> /// Use this method to send an email using the provided built-in email template /// to the given recipient. /// </summary> /// <param name = "_emailId">Email Template ID</param> /// <param name = "_languageId">Language ID</param> /// <param name = "_emailToAddr">Recipient e-mail address or list of e-mail addresses</param> /// <param name = "_mappings"> /// Map(PlaceholderName (str) -> PlaceholderValue (str)). /// This is a map of placeholders and their values used in the dynamic email body. /// If null, it will be created and filled using the corresponding ETH (Email Template Handler) class. /// </param> /// <param name = "_lineMappings"> /// List of Map(PlaceholderName -> PlaceholderValue). /// This is a list of mappings for each line of the dynamic table within the email body. /// If null, it will be created and filled using the corresponding ETH class. /// </param> /// <param name = "_contextInfo"> /// Context info that can be provided for filling _mappings and _lineMappings /// with placeholders' values. /// </param> /// <param name = "_emailCcAddr">Cc e-mail address(es)</param> /// <param name = "_emailBccAddr">Bcc e-mail address(es)</param> /// <param name = "_emailAttachmentFilename">Email attachment filename (optional)</param> /// <param name = "_emailAttachmentContent">Email attachment file content (optional)</param> /// <param name = "_emailAdditionalAttachments"> /// Additional email attachments in a form: /// [ [attachmentName, [content]], [attachmentName, [content]], ...] /// </param> public static void sendMail(SysEmailId _emailId, LanguageId _languageId, str _emailToAddr, Map _mappings = null, List _lineMappings = null, container _contextInfo = conNull(), str _emailCcAddr = '', str _emailBccAddr = '', str _emailAttachmentFilename = '', container _emailAttachmentContent = conNull(), container _emailAdditionalAttachments = conNull()) { ... } } |
Let’s see how it works on an example. Assume that you have an email message with the ID CnfmOrder in your Email Templates setup, and that you need to send an email notification to a customer (sara@gmail.com) that her sales order with ID 000725 is confirmed.
The only thing you have to do is to invoke the DocEmailTemplateManager::sendMail() method with the proper parameters, of course under condition that you have created a custom Docentric's ETH (Email Template Handler) class, which provides both definition and values for your custom placeholders.
1 2 3 4 5 6 7 8 9 10 11 |
public static void testSendEmail() { str recipient = 'sara@gmail.com'; str salesId = '000725'; str languageId = 'en-us'; str emailId = 'CnfmOrder'; // Send an email based on a template with the given emailId. DocEmailTemplateManager::sendMail(emailId, languageId, recipient, null, null, [salesId]); info(strFmt('Email based on template %1 successfully sent to %2!', emailId, recipient)); } |
You can see here how the used ETH class should look like >>
The second approach you might take is to fill the Placeholder name -> Placeholder value mapping outside your custom Docentric’s ETH class before sending an email as shown below. Just make sure that the names of your custom placeholders are the same as those used in Docentric email body editor, i.e. those defined in the ETH class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static void testSendEmail() { str recipient = 'sara@gmail.com'; str salesId = '000725'; str languageId = 'en-us'; str emailId = 'CnfmOrder'; // Create and fill mappings Map(PlaceholderName (str) -> PlaceholderValue (str)) outside of the ETH class. Map mappings = DocOrderConfirmEmailHandler::createAndFillMappingsFromOutside(salesId, languageId); // Send an email based on a template with the given emailId. DocEmailTemplateManager::sendMail(emailId, languageId, recipient, mappings); info(strFmt('Email based on template %1 successfully sent to %2!', emailId, recipient)); } |
Resources
Download custom email handler class >>
See how to create an email body with a dynamic table >>
Read how-to manual on improved Email templates >>