sendMail
Sends an email using a predefined template. It uses the MessageQueue entity to store the email body.
IMPORTANT!
Starting with v22.1.9, emails are sent asynchronously using job servers after
For v22.1.8 and prior, this function requires a valid SMTP server configured in the web service.
Starting with v22.1.9, emails are sent asynchronously using job servers after
EmailChannelProviderName
is enabled in the Configuration Manager. For v22.1.8 and prior, this function requires a valid SMTP server configured in the web service.
Syntax
Copy
function sendMail(templateName: string, to: string[], tokens: any, attachments: any, cc: string[]): void
Parameter | Description |
---|---|
templateName
|
Name of a predefined email template to be used. |
to
|
Recipient's email address. |
tokens
|
Keys for substitution. They are defined as properties of an object and replaced with their values. Tokens are enclosed in brackets in the email template body (e.g., {accountName}). |
attachments
|
The files to be attached to the email. This is a JSON object with its key-value pairs representing:
|
cc
|
Array of strings containing the Carbon Copy email recipients. |
Examples
Send a predefined welcome message
In this example:
- We send an email message based on the welcomeTemplate.
- The recipients of the message are john.doe@gmail.com and jane.doe@gmail.com.
- A copy of the message is sent to marketing@mycompany.com.
Copy
sendMail('welcomeTemplate', ['john.doe@gmail.com', 'jane.doe@gmail.com'], {}, {}, ['marketing@mycompany.com']);
Send a customized message which contains tokens and an attachment
In this example:
- We send a customized offer based on the offerTemplate.
- The recipient of the message is john.doe@gmail.com.
- The accountName token from the message template is replaced with the customer's name (John).
- The termsAndConditions_ddde85d9-ca38-42b8-b6b9-e5cc6bb4078c.pdf file is attached to the email under the TermsAndConditions.pdf name.
Copy
sendMail('offerTemplate', ['john.doe@gmail.com'], {accountName: 'John'}, {'TermsAndConditions.pdf': 'termsAndConditions_ddde85d9-ca38-42b8-b6b9-e5cc6bb4078c.pdf'},[]);