emailContact( )
Sends a triggered email to a contact, unless that contact is marked as unsubscribed..
Description
The emailContact()
function returns a Promise
that resolves when the triggered email is sent to the contact.
Note: This function replaces the deprecated
wix-crm-backend.emailContact()
. The deprecated function will continue to work, but it will not receive updates. To keep any existing code compatible with future changes, see the migration instructions.
To learn more about triggered emails, see:
Before using the emailContact()
function, you must set up at least 1
triggered email.
Specify which email to send by passing the email's ID in the emailId
parameter.
Specify which contact the email is sent to by passing a contact's ID in the contactId
parameter. To receive the email, the contact must be subscribed or marked as 'subscription not set'.
If the contact is marked as unsubscribed, the email will not be sent.
Read more about how to check the contact's subscription status.
If the specified triggered email contains variables,
you can pass values for those variables using the optional options
parameter.
You pass a variables
object
which contains the values to be inserted into your email in place of the variables
defined in that email. The values passed must be strings. If variables
does not contain a key:value
pair for a
variable in your triggered email, the fallback value defined when creating
your triggered email is inserted in place of the variable.
Notes:
- The statistics for Triggered Emails are based on every time an email is sent, including test emails that you send to yourself. This is different from the statistics for Email Marketing that are based on emails sent to unique contacts. Therefore, the statistics for Triggered Emails may seem inflated compared to Email Marketing statistics.
- Triggered Emails generates a code snippet for each of your email templates. The generated code includes the email's ID and keys for all the email's variable names. You can copy and paste the snippet into your code. Then, you need to define values for the
memberId
property and for each variable key. To learn how to use the generated snippet in your code, see How to Send a Triggered Email with Code.
Syntax
function emailContact(emailId: string, contactId: string, [options: TriggeredEmailOptions]): Promise<void>
emailContact Parameters
NAME
TYPE
DESCRIPTION
ID of the contact to send the email to.
Variable values to insert into the email.
Returns
Fulfilled - When the email is sent. Rejected - Error message.
Return Type:
Was this helpful?
1import { triggeredEmails } from 'wix-crm-backend';23// Sample emailId value:4// 'thanks_for_joining'5//6// Sample contactId value:7// 'e6b569cf-e275-598f-9247-e585ad116e66'89export function myEmailContactFunction(emailId, contactId) {10 return triggeredEmails.emailContact(emailId, contactId)11 .then(() => {12 console.log('Email was sent to contact');13 })14 .catch((error) => {15 console.error(error);16 });17}
1import { triggeredEmails } from 'wix-crm-backend';23// Sample emailId value:4// 'thanks_for_joining'5//6// Sample contactId value:7// 'e6b569cf-e275-598f-9247-e585ad116e66'8//9// Sample options value:10// {11// variables: {12// firstName: 'Johnny',13// lastName: 'Appleseed'14// }15// }1617export function myEmailContactFunction(emailId, contactId, options) {18 return triggeredEmails.emailContact(emailId, contactId, options)19 .then(() => {20 console.log('Email was sent to contact');21 })22 .catch((error) => {23 console.error(error);24 });25}
You can use queryContacts()
to find a contact with specified details, and then extract the contact's ID.
With the contact ID, you can use emailContact()
to send a triggered email.
1import { contacts, triggeredEmails } from 'wix-crm-backend';23/* Sample emailToFind value:4 * 'zackary.sirko@example.com'5 */67export async function sendEmailToContact(emailToFind) {89 let contactId;10 const queryResults = await contacts.queryContacts()11 .eq('info.emails.email', emailToFind)12 .find();13 const contactsWithEmail = queryResults.items;1415 if (contactsWithEmail.length === 1) {1617 console.log('Found 1 contact');18 contactId = contactsWithEmail[0]._id;1920 } else if (contactsWithEmail.length > 1) {2122 console.log('Found more than 1 contact');23 // Handle when more than one contact is found2425 } else {2627 console.log('No contacts found');28 // Handle when no contacts are found2930 }3132 const triggeredEmailTemplate = 'welcome_email';3334 try {35 await triggeredEmails.emailContact(triggeredEmailTemplate, contactId);36 console.log('Email sent to contact');37 } catch (error) {38 console.error(error);39 // Handle the error40 }41}42