Velo Tutorial: Sending a Triggered Email to Contacts
Prerequisites

- name
- Interest_Area



There are a few things to note about the code in this snippet:
- At the top of the snippet there is an
import
statement. This line needs to be added to the very top of the code on the page where you will be using the rest of the snippet. It imports the library of functions that lets your code work with the Triggered Email functionality. - The contact ID and the values of the variables (
name
andinterest_area
) are reflected in the code snippet with placeholders. These placeholder values will need to be replaced with real values in your actual code.
Add a Custom Field to Your Contacts List
Form

Type | ID | Usage |
---|---|---|
Input - text | nameInput | For entering a name |
Input - email | emailInput | For entering an email address |
Dropdown | interestArea | For selecting area of interest |
Button | signUpButton | For submitting the data |
Dataset | newsletterDataset | For connecting the elements |
Code
- Add the import statement to the top of the code where we use the snippet. We will also need to import an additional module to create new site contacts.
- Add an onClick event handler to the button. The code we add to that handler function will create the new contact and email them.
- Add code that creates the new contact and gives us the contact's ID.
- Add the snippet code to the handler function and replace the placeholder values.
- Add some code to handle success and errors.
Paste the import statement that was at the top of your snippet: import {triggeredEmails} 'wix-crm';
to the top of the code in the page where you want to use the snippet. Add contacts
in between the { }
to import that module as well. The final import statement should look like this:
import { triggeredEmails, contacts } from 'wix-crm';
Use the Properties & Events panel to add an onClick()
event handler to the signUpButton that runs each time it's clicked.
Add this code inside the onClick event handler. This uses the appendOrCreateContact API to create the new contact using the information the site visitor entered in the form fields.
1contacts.appendOrCreateContact({
2 name: {
3 first: $w('#nameInput').value
4 },
5 emails: [{
6 email: $w('#emailInput').value
7 }],
8 extendedFields: {
9 interest_area: $w('#interestArea').value
10 }
11});
The appendOrCreateContact
function returns an object containing the contactID
, and identityType
for the newly created contact. We'll grab the ID and use it in our snippet to identify the new contact and email them.
The appendOrCreateContact
function returns a promise, so we'll add our snippet after it. We'll declare a new variable called contactId
and use it to store the contactId
value that is stored in the object returned by appendOrCreateContact()
. We will use this ID in the snippet in place of <enter-contact-ID-here>
.
We'll also use the values that our site visitor entered in the form fields as the actual values for the name
and interest_area
variables in the variables
object.
1export function signUpButton_click(event) {
2 contacts.appendOrCreateContact({
3 name: {
4 first: $w('#nameInput').value
5 },
6 emails: [{
7 email: $w('#emailInput').value
8 }],
9 extendedFields: {
10 interest_area: $w('#interestArea').value
11 }
12 })
13 .then((contactInfo) => {
14 const contactId = contactInfo.contactId;
15 triggeredEmails.emailContact("newsletter_signup", contactId, {
16 "variables": {
17 "name": $w('#nameInput').value,
18 "interest_area": $w("#interestArea").value
19 }
20 })
21 });
22}
Since the emailContact()
function returns a Promise, we can define what happens when the Promise resolves successfully or rejects with an error.
We'll add a .then
and a .catch
to handle these.
1import { triggeredEmails, contacts } from 'wix-crm';
2
3export function signUpButton_click(event) {
4 contacts.appendOrCreateContact({
5 name: {
6 first: $w('#nameInput').value
7 },
8 emails: [{
9 email: $w('#emailInput').value
10 }],
11 extendedFields: {
12 interest_area: $w('#interestArea').value
13 }
14 })
15 .then((contactInfo) => {
16 const contactId = contactInfo.contactId;
17 triggeredEmails.emailContact("newsletter_signup", contactId, {
18 "variables": {
19 "name": $w('#nameInput').value,
20 "interest_area": $w("#interestArea").value
21 }
22 })
23 .then(() => {
24 // do something after the email was sent
25 })
26 .catch((err) => {
27 // handle the error if the email wasn't sent
28 });
29 });
30}