In this tutorial, you'll learn how to automatically send a personalized email to the currently logged-in site member using Triggered Emails and an input form. You'll use a form submission as the trigger, but you can adapt this approach to send emails from anywhere in your code.
You'll build a simple form where site members can enter their name, select their favorite sport, and add comments. When the form is submitted, your site will send a personalized Triggered Email to the logged-in member with the information they provided.
We'll use the following steps to send a triggered email to a member:
It's important to note the following before starting this tutorial:
sportsEmail, that you can connect to a dataset later.The code in this article was written using the following module versions:
Learn how to install npm packages in the editor or using the CLI.
In this step, you'll create a Triggered Email template with variables for personalization.
To set up your Triggered Email:
Developer Tools on your site's dashboard and select Triggered Emails.name, sport, and comments.sportMail.The code should look like this:
The following code snippet uses the Velo API by importing the 'wix-crm' package.
To use the SDK version, change the import to '@wix/site-crm'.
For a full, detailed walkthrough, see Set Up a Triggered Email.
In this step, you'll build a simple form that collects input from the site member. The form includes input fields for each variable you set up in your Triggered Email, as well as a submit button. All elements should be connected to the same dataset.
To create the form:
nameInput.sportDropdown.commentsInput.submitButton.sportDataset.sportDataset that's connected to the sportsEmail collection you created earlier.sportDataset.In this step, you'll add code that sends the Triggered Email to the site member when the form is submitted.
onAnAfterSave() event handler for your dataset to run the code each time the form is successfully submitted.userId.Replace the placeholders in the code snippet with the values you saved from the form, and the member ID, to personalize the email.
The following example includes error handling for both successful and failed email attempts.