Can you send a triggered email to your own email?

Hey,
I have been working on a triggered email for when a user is logged in and when they complete any outstanding payments it send an email to the user confirming this. Is it possible for me to get the same email to inform me that the outstanding balance has been paid?

I have the followin code which works well to deliver the email to the logged in user

if(wixUsers.currentUser.loggedIn) {
 const userId = wixUsers.currentUser.id;
 const SRWId = "info@scottishrockandwater.com";
                             wixUsers.emailUser("RJODHtz", userId, {
                                 variables: {
 "name": $w("#fullNameText").text,
 "amount": $w('#amount').value,
 "courseTitle": $w('#courseTitleText').text
                                     }
                                     })
                                     .then( () => {
 // do something after the email was sent successfully
          } )
          .catch( (err) => {
 // handle error that prevented the email from being sent
        } );
    }

But I’m trying to set up the email to be delivered to SRWId also, i have tried to inset this into the code but it just stops the email being delivered to any of the addresses.

Many thanks

This works in my head but sadly not in person… any ideas? The email goes to the logged in user but not to my own email

Recently, this was added to the API: https://www.wix.com/code/reference/wix-users-backend.html#emailUser

So “you” has to be registered as a user to get it working. And you need “your” userid.

@giri-zano Thank you for your response, i had been playing with this but I can’t seem to get it to work as intended, it will only send the email if I am logged in as that user.id “me”

 
let myContactId =  "2a4f5fb7-5027-41e9-8d9c-8ef1a368d098" //this is my 'own/business' login ID
 let value1 = $w("#fullNameText").text;
 let value2 = $w('#amount').value;
 let value3 = $w('#courseTitleText').text;

                        wixUsers.emailUser("paymentCompleteEmail", myContactId, {
 "variables": {
 "name": value1,
 "amount": value2,
 "courseTitle": value3,
                    }
                })
                .then(() => {
                    console.log("Triggered email sent");
                })
                .catch((err) => {
                    console.log(err);
                });

Does this need to be a backend module?

Yes, this goes into the backend, a jsw-file. In it, you will have to import it like:

import wixUsers from 'wix-users-backend';

In your frontend code, you will have to import that backend jsw and then call the function that sends the email.

@giri-zano thanks for your response. I have a little experience with backend modules and will give this a go later.

I was wondering if it ‘needs’ to be a backend module? I noticed on the Wix API that .emailUser is under the wixUser and wixUser backend?

If you are struggling then an easy option would be for to just do a triggered email to members like here:

And then just use Wix Automations to send an email reply back to you when the member has completed anything which can include all the captured details, plus you can also add your own email as preference for where to send it, rather than always using your email that you use for Wix.

Otherwise you can see the triggered email code for sending a email on form submission using backend code here, albeit this is using a third party called SendGrid:

Stephen, you are right: they all called the same in front- end backend, but they behave differently. The frontend can only send email to the logged in user (allthough userid is strangely enough a param), the backend function should, according to Sam´s article when he introduced it a couple of weeks ago, be able to send it to any member.
From a security standpoint, this makes sense, doing it from the backend.

Yes, and then there is the option of using emailjs, as I described lightyears ago here:

@giri-zano Thank you for taking the time to help me. I have put together some code to start getting this function to run, I have a query though, any previous backend modules I have worked with don’t have any variables, I have just run query’s that return a promise then use this in the front end, but I’m unsure of have to get the variables to the backend? Here is my code:

Backend module - email.jsw

import wixUsersBackend from 'wix-users-backend';

export function sendEmailToOwnAddress  () {
let myContactId = "2a4f5fb7-5027-41e9-8d9c-8ef1a368d098" //this is my 'own/business' login ID
let value1 = $w("#fullNameText").text; //How to code the variable 1, 2 & 3?
let value2 = $w('#amount').value;
let value3 = $w('#courseTitleText').text;

wixUsersBackend.emailUser("paymentCompleteEmail", myContactId, {
 "variables": {
 "name": value1,
 "amount": value2,
 "courseTitle": value3,
        }
    })
    .then(() => {
        console.log("Triggered email sent");
    })
    .catch((err) => {
        console.log(err);
    });
}

and in the front end

import {sendEmailToOwnAddress} from 'backend/email.jsw';


 //payment code above this line
if (tokenResponse > 300) {
                        $w("#textFail").text = "There was an error with your card.\nPlease try again later or contact us.";
                        $w("#textFail").show();
                        console.log("There was an error with your card.");
                        $w('#paymentPreLoaderBox').hide();
                    } else {
 if(wixUsers.currentUser.loggedIn) {
 const userId = wixUsers.currentUser.id;
                             wixUsers.emailUser("paymentCompleteEmail", userId, {
                                 variables: {
 "name": $w("#fullNameText").text,
 "amount": $w('#amount').value,
 "courseTitle": $w('#courseTitleText').text
                                     }
                                     })
                                     .then(() => {
                                        sendEmailToOwnAddress //function called from backend
                                            })
          .catch( (err) => {
 // handle error that prevented the email from being sent
        } );
    }
//more payment code below this line