Search.../

emailUser( )

Sends a Triggered Email to the specified site member.

Description

To learn more about Triggered Emails, see:

Before using the emailUser() 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 member the email is sent to by passing the member's user ID in the toUser parameter.

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 toUser 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 emailUser(emailId: string, toUser: string, [options: TriggeredEmailOptions]): Promise<void>

emailUser Parameters

NAME
TYPE
DESCRIPTION
emailId
string

The Email ID of the Triggered Email to send.

toUser
string

The User ID of the email recipient.

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 site member

Copy Code
1import wixUsersBackend from 'wix-users-backend';
2
3export function myBackendFunction() {
4 let userId = // get user ID
5
6 wixUsersBackend.emailUser("emailID", userId)
7 .then( () => {
8 // email has been sent
9 } )
10 .catch( (err) => {
11 // there was an error sending the email
12 } );
13}
Send a Triggered Email to a site member with variable values

Copy Code
1import wixUsersBackend from 'wix-users-backend';
2
3export function myBackendFunction() {
4 let userId = // get user ID
5 let value1 = // value for variable1
6
7 wixUsersBackend.emailUser("emailID", userId, {
8 "variables": {
9 "variable1": value1,
10 "variable2": "value for variable2"
11 }
12 } )
13 .then( () => {
14 // email has been sent
15 } )
16 .catch( (err) => {
17 // there was an error sending the email
18 } );
19}
Register a user sending an email for confirmation

This example demonstrates a common email verification flow. A user is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When a user goes to the verification page, the approval is granted and the user is logged into the site.

The code is split between three locations:

  • A backend web module named register.jsw.
  • The page code for the page where users register.
  • The page code for the page where users confirm their registration.

Copy Code
1/*******************************
2 * Backend code - register.jsw *
3 *******************************/
4import wixUsersBackend from 'wix-users-backend';
5
6export function doRegistration(email, password, firstName, lastName) {
7 // register the user
8 return wixUsersBackend.register(email, password, {
9 "contactInfo": {
10 "firstName": firstName,
11 "lastName": lastName
12 }
13 })
14 .then((results) => {
15 // user is now registered and pending approval
16 // send a registration verification email
17 wixUsersBackend.emailUser('verifyRegistration', results.user.id, {
18 "variables": {
19 "name": firstName,
20 "verifyLink": `http://yourdomain.com/post-register?token=${results.approvalToken}`
21 }
22 });
23 });
24}
25
26export function doApproval(token) {
27 // approve the user
28 return wixUsersBackend.approveByToken(token)
29 // user is now active, but not logged in
30 // return the session token to log in the user client-side
31 .then((sessionToken) => {
32 return { sessionToken, "approved": true };
33 })
34 .catch((error) => {
35 return { "approved": false, "reason": error };
36 });
37}
38
39/*********************************
40 * Client-side registration code *
41 *********************************/
42import { doRegistration } from 'backend/register';
43
44export function button_click(event) {
45 let email = // the user's email address
46 let password = // the user's password
47 let firstName = // the user's first name
48 let lastName = // the user's last name
49
50 doRegistration(email, password, firstName, lastName)
51 .then(() => {
52 console.log("Confirmation email sent.");
53 });
54}
55
56/**************************************
57 * Client-side post-registration code *
58 **************************************/
59import wixLocation from 'wix-location';
60import wixUsersBackend from 'wix-users';
61import { doApproval } from 'backend/register';
62
63$w.onReady(() => {
64 // get the token from the URL
65 let token = wixLocation.query.token;
66
67 doApproval(token)
68 .then((result) => {
69 if (result.approved) {
70 // log the user in
71 wixUsersBackend.applySessionToken(result.sessionToken);
72 console.log("Approved");
73 }
74 else {
75 console.log("Not approved!");
76 }
77 });
78});
79