Hi there,
I am trying to send an email to the current user after submitting a form. I followed the steps in the tutorial but is not sending the email. Some help please!
This is my code:
import wixUsers from ‘wix-users’ ;
$w.onReady( function () {
$w( “#dataset1” ).onAfterSave( () => {
if (wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id;
wixUsers.emailUser( "RxlIRSb" , userId, {
variables: {
“clientName” : $w( “#firstName” ).value,
“clientLastName” : $w( “#lastName” ).value,
“clientPhone” : $w( “#clientPhone” ).value,
“clientEmail” : $w( “#clientEmail” ).value,
“clientAddress” : $w( “#clientAddress” ).value,
“typeOfAccident” : $w( “#typeAccident” ).value,
“additionalInfo” : $w( “#AditionalInfo” ).value
}
} );
}
} );
} );
I don’t know what I am missing here.
Thanks for your help!
Do what is says at the end of the tutorial first as you are missing out the return of the promise…
We can also add code to verify that the email was sent and handle cases where an error has occurred. Since the emailUser() function returns a Promise , we can define what happens when the Promise resolves successfully or rejects with an error.
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members
import wixUsers from 'wix-users';
$w.onReady(function () {
$w("#sportDataset").onAfterSave( () => {
if(wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id;
wixUsers.emailUser("sportMail", userId, {
variables: {
"name": $w("#nameInput").value,
"sport": $w("#sportDropdown").value,
"comments": $w("#commentsInput").value
}
} )
.then( () => {
// do something after the email was sent successfully
} )
.catch( (err) => {
// handle error that prevented the email from being sent
} );
}
} );
} );
You can also get a detailed description and more code examples in the Wix API Reference for this here.
https://www.wix.com/corvid/reference/wix-users.html#emailUser
Examples
Send a Triggered Email to the currently logged-in member
import wixUsers from 'wix-users';
// ...
let userId = wixUsers.currentUser.id;
wixUsers.emailUser("emailID", userId)
.then( () => {
console.log("Triggered email sent");
} )
.catch( (err) => {
console.log(err);
} );
Send a Triggered Email to the currently logged-in member with variable values
import wixUsers from 'wix-users';
// ...
let userId = wixUsers.currentUser.id;
let value1 = // value for variable1
wixUsers.emailUser("emailID", userId, {
"variables": {
"variable1": value1,
"variable2": "value for variable2"
}
} )
.then( () => {
console.log("Triggered email sent");
} )
.catch( (err) => {
console.log(err);
} );
Note
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.
Another easier option would be to just use Wix Automations for it.
https://support.wix.com/en/article/about-wix-automations
Thanks GOS, I inserted:
.then( () => {
console.log( “Triggered email sent” );
} )
. catch ( (err) => {
console.log( “Triggered email NOT SEND!” );
} );
And I am receiving the message “Triggered email NOT SEND!”, so now I know for sure that the email is not being sent. Why?
@givemeawhisky This form will be use only form website members and I need to send them an email with the info they entered as a confirmation. Wix automations do not let me do that or I can see how.
@jorgegalgor
I’ve used that myself for a simple members comments page on a site and it all works fine for me and I have pasted my basic code at the end of this post.
If yours is not working, then make sure that your triggered email code is correct, or if easier change the code to a name appropriate to the email itself.
Also, make sure that the user input form that you are using has a submit button which is connected to a dataset for which you are using to save the users input submissions, as this is what the onAfterSave is working off.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onAfterSave
onAfterSave( )
Adds an event handler that runs just after a save.
Description
The onAfterSave() function allows you to optionally perform actions right after a save() operation. When you call save(), the callback is run after the save has successfully completed.
Calling onAfterSave() on a read-only dataset causes an error.
Examples
Register a callback to run after a save
$w("#myDataset").onAfterSave( () => {
console.log("After save");
} );
My used code…
import wixUsers from 'wix-users';
$w.onReady(function () {
$w("#MembersComments").onAfterSave( () => {
if(wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id;
wixUsers.emailUser("CommentsFromMembers", userId, {
variables: {
"name": $w("#nameInput").value,
"reply": $w("#replyBy").value,
"comments": $w("#commentsInput").value
}
} )
.then( () => {
// do something after the email was sent successfully
} )
.catch( (err) => {
// handle error that prevented the email from being sent
} );
}
} );
} );
@givemeawhisky After checking everything as you said. The issue was with a variable. The address variable was making the email not to be delivered. I just make that line a comment because I can live without the address in the email, but I would like to know why the address is an issue. And I am sure than the labels are ok. The address appears in the database but for some reason if I include it in the code the email is not delivered.
Thanks a lot for your time, I appreciate your help GOS!