Search.../

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
emailId
string

Email ID of the triggered email to send.

contactId
string

ID of the contact to send the email to.

options
Optional
TriggeredEmailOptions

Variable values to insert into the email.

Returns

Fulfilled - When the email is sent. Rejected - Error message.

Return Type:

Promise<void>

Was this helpful?

Send a triggered email to a contact

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { triggeredEmails } from 'wix-crm-backend';
3
4export const myEmailContactFunction = webMethod(Permissions.Anyone, (emailId, contactId) => {
5 return triggeredEmails.emailContact(emailId, contactId)
6 .then(() => {
7 console.log('Email was sent to contact');
8 })
9 .catch((error) => {
10 console.error(error);
11 });
12});
Send a triggered email with variable values

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { triggeredEmails } from 'wix-crm-backend';
3
4export const myEmailContactFunction = webMethod(Permissions.Anyone, (emailId, contactId, options) => {
5 return triggeredEmails.emailContact(emailId, contactId, options)
6 .then(() => {
7 console.log('Email was sent to contact');
8 })
9 .catch((error) => {
10 console.error(error);
11 });
12});
Query for a contact and then email the contact

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.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts, triggeredEmails } from 'wix-crm-backend';
3
4export const sendEmailToContact = webMethod(Permissions.Anyone, async (emailToFind) => {
5 let contactId;
6 const queryResults = await contacts.queryContacts()
7 .eq('info.emails.email', emailToFind)
8 .find();
9 const contactsWithEmail = queryResults.items;
10
11 if (contactsWithEmail.length === 1) {
12 console.log('Found 1 contact');
13 contactId = contactsWithEmail[0]._id;
14 } else if (contactsWithEmail.length > 1) {
15 console.log('Found more than 1 contact');
16 // Handle when more than one contact is found
17 } else {
18 console.log('No contacts found');
19 // Handle when no contacts are found
20 }
21
22 const triggeredEmailTemplate = 'welcome_email';
23
24 try {
25 await triggeredEmails.emailContact(triggeredEmailTemplate, contactId);
26 console.log('Email sent to contact');
27 } catch (error) {
28 console.error(error);
29 // Handle the error
30 }
31});