Search.../

emailContact( )

Sends a Triggered Email to the current contact.

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.

Specify which contact the email is sent to by passing the contact's ID in the toContact parameter. Note, that you can only send an email to the current site visitor using the client-side emailContact() function. To send an email to a contact who is not the current site visitor, use the backend emailContact() function.

If the specified Triggered Email contains variables, you can pass values for those variables using the optional options parameter. You pass a TriggeredEmailOptions 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 the object you pass to the options parameter 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 toContact 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, toContact: string, [options: TriggeredEmailOptions]): Promise<void>

emailContact Parameters

NAME
TYPE
DESCRIPTION
emailId

string

The Email ID of the Triggered Email to send.

toContact

string

The Contact ID.

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 contact

Code Example

Copy Code
1import wixCrm from 'wix-crm';
2
3// ...
4
5let contactId = // get contact ID
6
7wixCrm.emailContact("emailID", contactId)
8 .then( () => {
9 console.log("Triggered email sent");
10 } )
11 .catch( (err) => {
12 console.log(err);
13 } );
Send a Triggered Email with variable values

Code Example

Copy Code
1import wixCrm from 'wix-crm';
2
3// ...
4
5let contactId = // get contact ID
6let value1 = // value for variable1
7
8wixCrm.emailContact("emailID", contactId, {
9 "variables": {
10 "variable1": value1,
11 "variable2": "value for variable2"
12 }
13 } )
14 .then( () => {
15 console.log("Triggered email sent");
16 } )
17 .catch( (err) => {
18 console.log(err);
19 } );
Create a contact and then send a Triggered Email to the new contact

Code Example

Copy Code
1import wixCrm from 'wix-crm';
2
3$w.onReady(function () {
4 $w("#myButton").onClick( () => {
5 wixCrm.createContact( {
6 "firstName": $w("#firstName").value,
7 "lastName": $w("#lastName").value,
8 "emails": [$w("#email").value],
9 "phones": [$w("#phone").value]
10 } )
11 .then( (contactId) => {
12 wixCrm.emailContact("thankyou", contactId, {
13 "variables": {
14 "firstName": $w("#firstName").value,
15 "lastName": $w("#lastName").value
16 }
17 } );
18 } );
19 } );
20} );