Search.../

emailContact( )

Sends a triggered email to the current contact, unless that contact is marked as unsubscribed.

Description

To learn more about Triggered Emails, see:

Before using the emailContact() function, you need to set up at least one Triggered Email.

Specify which email to send by passing the email's ID in the emailId parameter.

You can use this function to send an email the current site visitor only. To receive the email, the site visitor must be subscribed or marked as 'subscription not set'. If the site visitor is marked as unsubscribed, the email will not be sent. Read more about how to check the contact's subscription status.

To send an email to a subscribed contact who is not the current site visitor, use the wix-crm-backend triggeredEmails.emailContact() function.

If the specified Triggered Email contains variables, you can pass values for those variables using the optional options parameter. You pass the options object, which contains the values to replace the variables defined in your triggered email. The values passed must be strings. If the object you pass to the variables object 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.

Note that 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 contactId 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

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 { triggeredEmails } from 'wix-crm-frontend';
2
3// ...
4
5// Sample emailId value:
6// 'thanks_for_joining'
7//
8// Sample contactId value:
9// 'e6b569cf-e275-598f-9247-e585ad116e66'
10
11triggeredEmails.emailContact(emailId, contactId)
12 .then(() => {
13 console.log('Email was sent to contact');
14 })
15 .catch((error) => {
16 console.error(error);
17 });
Send a triggered email with variable values

Copy Code
1import { triggeredEmails } from 'wix-crm-frontend';
2
3// ...
4
5// Sample emailId value:
6// 'thanks_for_joining'
7//
8// Sample contactId value:
9// 'e6b569cf-e275-598f-9247-e585ad116e66'
10//
11// Sample options value:
12// {
13// variables: {
14// firstName: 'Johnny',
15// lastName: 'Appleseed'
16// }
17// }
18
19triggeredEmails.emailContact(emailId, contactId, options)
20 .then(() => {
21 console.log('Email was sent to contact');
22 })
23 .catch((error) => {
24 console.error(error);
25 });
Send a triggered email to a new contact

Copy Code
1import { contacts, triggeredEmails } from 'wix-crm-frontend';
2
3$w.onReady(function () {
4 $w('#createContact').onClick(async () => {
5 const firstName = $w('#firstName').value;
6 const lastName = $w('#lastName').value;
7 const email = $w('#email').value;
8 const phone = $w('#phone').value;
9
10 const contactInfo = {
11 name: { first: firstName, last: lastName },
12 emails: [{ email: email }],
13 phones: [{ phone: phone }],
14 };
15
16 let resolvedContact;
17
18 try {
19 resolvedContact = await contacts.appendOrCreateContact(contactInfo);
20 console.log('Resolved to contact', resolvedContact);
21
22 if (resolvedContact.identityType !== 'CONTACT') {
23 console.log('Current contact is already a site member. Not sending a welcome email.');
24
25 // emailContact() cannot be used to email site members.
26 // If you want to email a member, use triggeredEmails.emailMember()
27
28 return;
29 } else {
30
31 const emailId = 'thanks_for_joining';
32 const contactId = resolvedContact.contactId;
33 const options = {
34 variables: { firstName: firstName, lastName: lastName },
35 };
36
37 await triggeredEmails.emailContact(emailId, contactId, options);
38 console.log('Emailed contact');
39 }
40 } catch (error) {
41 console.error(error);
42 }
43 });
44});