I created a custom form for customer who want order my service here:
Here is my code for create/update contact and send trigger email:
//send order confirm email by Wix trigger email
function sendOrderConfirmEmail() {
const options = {
weekday: "short",
year: "numeric",
month: "long",
day: "numeric"
};
console.log("Begin send email !!!!")
wixCrm.createContact({
"firstName": $w('#customerName').value,
"emails": [$w("#customerEmail").value],
"phones": [$w("#customerPhone").value]
})
.then((contactId) => {
console.log("Created contact:"+contactId);
wixCrm.emailContact("orderConfirm", contactId, {
"variables": {
"name": $w('#customerName').value,
"service": $w("#serviceList").value,
"email": $w("#customerEmail").value,
"phone": $w("#customerPhone").value,
"class": $w("#customerClassRadio").value,
"deadline ": $w("#customerDeadline").value.toLocaleString("en-US", options),
"detail": $w("#orderDetail").value
}
})
.then(() => {
console.log("The emai sent successfull");// do something after the email was sent
})
.catch((err) => {
console.log(err);// handle the error if the email wasn't sent
});
});
}
I created trigger email here:
But Wix doesn’t send any email. Please help me fix it.
Make sure that your code is correct, here is one that I used.
import wixCRM from 'wix-crm';
$w.onReady(function() {
$w("#JoinUsForm").onAfterSave(() => {
let startDate = $w("#startDate").value;
let firstName = $w('#firstName').value;
let lastName = $w('#lastName').value;
let email = $w("#email").value;
let choirRole = $w("#choirRole").value;
let readMusic = $w("#readMusic").value;
let choirBefore = $w("#choirBefore").value;
let startNow = $w("#startNow").value;
// You can add any label here too, for example....//
//let label = ["Contacted Us"];//
//let label = ["Subscribed"];//
wixCRM.createContact({
"firstName": firstName,
"lastName": lastName,
"emails": [email],
"Choir Role": choirRole,
"Read Music": readMusic,
"Choir Before": choirBefore,
"Start Now": startNow,
"Start Date": startDate
})
.then((contactId) => {
// Need to use the triggered email name
return wixCRM.emailContact('joiningusform', contactId, {
"variables": {
// Need to use the triggered email variable names
"firstName": firstName,
"email": email,
"choirRole": choirRole,
"readMusic": readMusic,
"choirBefore": choirBefore,
"startNow": startNow,
"startDate": startDate.toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric'})
}
});
})
.catch((err) => {
// handle the error if the email wasn't sent
console.log(`Error: ${err}`);
});
});
});
I didn’t see any wrong code. I just follow Wix guide.
Here is console log. Wix said “failed to fetch”
You are best double checking your code as you are still missing your ‘return’.
I copied your code and changed to my variales:
//Test new order confirm
function testOrderConfirmEmail() {
wixCrm.createContact({
"firstName": $w('#customerName').value,
"emails": [$w("#customerEmail").value],
"phones": [$w("#customerPhone").value]
})
.then((contactId) => {
// Need to use the triggered email name
return wixCrm.emailContact('orderConfirm', contactId, {
"variables": {
// Need to use the triggered email variable names
"name": $w('#customerName').value,
"service": $w("#serviceList").value,
"email": $w("#customerEmail").value,
"phone": $w("#customerPhone").value,
"class": $w("#customerClassRadio").value,
"startDate": $w("#customerDeadline").value.toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric'}),
"detail": $w("#orderDetail").value
}
});
})
.catch((err) => {
// handle the error if the email wasn't sent
console.log(`Error: ${err}`);
});
}
But it still does not work with the same error log:
Hi. I created a custom form for customer who want order my trips.
Here is my code for create/update contact and send trigger email:
import wixCrm from ‘wix-crm’;
export function BtnConferma_click(event) {
wixCrm.createContact({
“firstName”: $w(‘#input2’).value,
“emails”: [$w(“#input3”).value],
})
.then((contactId) => {
wixCrm.emailContact(‘Rvgkf8I’, contactId, {
“variables”: {
“name”: $w(‘#input2’).value,
“mail”: $w(“#input3”).value
}
.then( () => {
console.log(“Triggered email sent”);
} )
. catch ( (err) => {
console.log(err);
} )
})
})
}
But Wix doesn’t send any email.
Can someone help me ?
Both of you here are still doing this wrong.
You are using the triggered email after a form has been submitted through your site, so obviously this form is being saved into a dataset that you have added to your site.
Therefore you need to be using onAfterSave event first as shown in the first part of my original code to make sure that your form is saved first.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onAfterSave
Then you can use createContact to create a new contact on your site through the Wix CRM.
Then you can use emailContact to send the triggered email to the same contact.
You don’t need either of these lines here.
function testOrderConfirmEmail() {
export function BtnConferma_click(event) {
As if you look at my tutorial you will see that it runs straight after the form has been saved into your dataset.
Trinh,
If you want to use your code exactly as it is and just using the Wix tutorial for sending a triggered email to contacts, then you need to replace this line here and replace it with an onClick event handler function for your ‘Send’ button as in the tutorial itself.
Remove this line here
function testOrderConfirmEmail() {
Replace with something like this
export function sendButton_click(event) {
//Make sure that you have added the onClick event to the button through the properties panel//
//Change sendButton to whatever element id your send button is//
So your code would be something like this.
import wixCrm from 'wix-crm';
$w.onReady(function () {
});
export function sendButton_click(event) {
const options = {
weekday: "short",
year: "numeric",
month: "long",
day: "numeric"
};
console.log("Begin send email !!!!")
wixCrm.createContact({
"firstName": $w('#customerName').value,
"emails": [$w("#customerEmail").value],
"phones": [$w("#customerPhone").value]
})
.then((contactId) => {
console.log("Created contact:"+contactId);
wixCrm.emailContact("orderConfirm", contactId, {
"variables": {
"name": $w('#customerName').value,
"service": $w("#serviceList").value,
"email": $w("#customerEmail").value,
"phone": $w("#customerPhone").value,
"class": $w("#customerClassRadio").value,
"deadline ": $w("#customerDeadline").value.toLocaleString("en-US", options),
"detail": $w("#orderDetail").value
}
})
.then(() => {
console.log("The email sent successfully");
})
.catch((err) => {
console.log(err);
});
});
}
Adriano,
For yours to work just using the triggered email to contacts tutorial, then you need to be adding the onReady page function at the start of your code, otherwise your code could all be running when the page is loading itself.
This part here.
$w.onReady(function () {
});
Also, with your button, double check that your button element has the id name of ‘BtnConferma’ as all element id names should really be starting with a lower case letter, so just make sure that your button is ‘BtnConferma’ and not ‘btnConferma’ for example.
You can have element id names with a starting capital letter, however like the field key name in the datasets which always start with a lowercase letter, it is best to keep it with a lower case first and then capitals on the next words.
Field name of ‘First Name’ will have a field key of ‘firstName’ etc.
As with Trinh, make sure that you have added the onClick event to the button through the properties panel.
Also, in your triggered email variables, should it be ‘email’ or ‘mail’, however it is your website so it is your choice if you want to use email or mail.
So your code would look something like this.
import wixCrm from 'wix-crm';
$w.onReady(function () {
});
export function BtnConferma_click(event) {
wixCrm.createContact({
"firstName": $w('#input2').value,
"emails": [$w("#input3").value]
})
.then((contactId) => {
wixCrm.emailContact('Rvgkf8I', contactId, {
"variables": {
"name": $w('#input2').value,
"mail": $w("#input3").value
}
.then( () => {
console.log("Triggered email sent");
} )
.catch( (err) => {
console.log(err);
} )
})
})
}
Hi and thanks for your help
I did as you said, but unfortunately the email doesn’t start.
This is the result of the log:
And this is my code:
import wixCrm from ‘wix-crm’;
$w.onReady( function () {
});
export function btn_ConfermaIscrizione(event) {
let firstName = $w(“#input2”).value
let lastName = $w(“#input1”).value
let email = $w(“#input3”).value
let phone = $w(“#input4”).value
console.log(“Begin create contact !!!”)
wixCrm.createContact( {
“firstName”: firstName,
“lastName”: lastName,
“emails”: [email],
“phones”: [phone]
} )
.then( (contactId) => {
// contact created
console.log(“Created contact:”+contactId);
console.log("Mail: "+email)
wixCrm.emailContact(“mail_IscrizioneViaggio”, contactId)
.then( () => {
console.log(“Triggered email sent”);
} )
.catch ( (err) => {
console.log(" Error: email not sent");
console.log(err);
} );
} );
}
Is this your actual button id name as it would not be allowed as an id name.
btn_ConfermaIscrizione
If you take out the underscore, then it will be allowed.
btnConfermaIscrizione
When you add the onClick event to the button through the properties panel, it should add the export function line automatically and it should look like this.
export function btnConfermaIscrizione_click(event) {
Also, with your triggered email, is this your actual triggered email name that you changed it to from the original random characters that Wix gave you?
wixCrm.emailContact("mail_IscrizioneViaggio", contactId)

Also, you don’t really need this part here as it can be combined.
let firstName = $w("#input2").value
let lastName = $w("#input1").value
let email = $w("#input3").value
let phone = $w("#input4").value
console.log("Begin create contact !!!!")
wixCrm.createContact( {
"firstName": firstName,
"lastName": lastName,
"emails": [email],
"phones": [phone]
To this.
wixCrm.createContact( {
"firstName": $w("#input2").value,
"lastName": $w("#input1").value,
"emails": [$w("#input3").value],
"phones": [$w("#input4").value]
This part here is missing code too.
wixCrm.emailContact("mail_IscrizioneViaggio", contactId)
.then( () => {
console.log("Triggered email sent");
Should be something like this, which has been tested and it does not come up with any errors at all.
It was only tested in preview with a false email, so obviously I got the email not sent error message, this should however be replaced with the email sent when used on the live site along with an actual email address.
Test your form in your published site to see that it creates the contact and sends a Triggered Email.

Click on the image above to bring up an enlarged version.
import wixCrm from 'wix-crm';
$w.onReady(function () {
});
export function btnConfermaIscrizione_click(event) {
wixCrm.createContact({
"firstName": $w("#input2").value,
"lastName": $w("#input1").value,
"emails": [$w("#input3").value],
"phones": [$w("#input4").value]
})
.then((contactId) => {
console.log("Created contact:"+contactId);
console.log("Mail:"+$w("#input3").value);
wixCrm.emailContact("mail_IscrizioneViaggio", contactId, {
"variables": {
"name": $w('#input2').value
}
})
.then( () => {
console.log("Triggered email sent");
})
.catch((err) => {
console.log("Error: email not sent");
});
});
}
If you have not got any variables on your actual triggered email, then please mask out the name line simply by doing this and the code won’t read it.
"variables": {
//"name": $w('#input2').value
}
Thanks a lot for your help!
I renamed the button event as you advised me.
The name of the email is the one provided by Wix and that I have changed (as you find it in the code)
I used the code you sent me
Unfortunately, from the console I keep getting an error:
Created contact: 53a71f7d-a986-4ff1-a621-747f8b031619
Mail: offadventure@gmail.com
Error: email not sent
Even if I do the same thing from the “real” published environment, the email doesn’t start
This is the code:
import wixCrm from ‘wix-crm’;
$w.onReady( function () {
});
export function btnConfermaIscrizione(event) {
wixCrm.createContact({
“firstName”: $w(“#input2”).value,
“lastName”: $w(“#input1”).value,
“emails”: [$w(“#input3”).value],
“phones”: [$w(“#input4”).value]
})
.then((contactId) => {
console.log(“Created contact:”+contactId);
console.log(“Mail:”+$w(“#input3”).value);
wixCrm.emailContact(“mail_IscrizioneViaggio”, contactId, {
“variables”: {
//“name”: $w(‘#input2’).value
}
})
.then( () => {
console.log(“Triggered email sent”);
})
. catch ((err) => {
console.log(“Error: email not sent”);
});
});
}
Is this email connected to the site in anyway, if it is then please try using another one that is not already used on the site.
Even a free disposable email can be used, just as long as you can check that the triggered email was sent and received in the inbox etc.
If this doesn’t work, then try using my code from further back up with the onAfterSave in it.
Thanks again for your patience.
Unfortunately I have tried several emails and never received anything.
Sometimes, it seems to go crazy …
Thanks again !
I tried it again with click event, but it is still the same error. I think email trigger have a bug. Here’s my code that i edited from your code:
export function orderSubmit_click(event) {
wixCrm.createContact({
"firstName": $w('#customerName').value,
"lastName": '_test',
"emails": [$w("#customerEmail").value],
"phones": [$w("#customerPhone").value]
})
.then((contactId) => {
console.log("Created contact:"+contactId);
console.log("Mail:"+$w("#customerEmail").value);
wixCrm.emailContact("orderConfirm", contactId, {
"variables": {
"name": $w('#customerName').value,
"service": $w("#serviceList").value,
"email": $w("#customerEmail").value,
"phone": $w("#customerPhone").value,
"class": $w("#customerClassRadio").value,
"startDate": $w("#customerDeadline").value.toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric'})
//"detail": $w("#orderDetail").value
}
})
.then( () => {
console.log("Triggered email sent");
})
.catch((err) => {
console.log("Error: email not sent");
});
});
}
Here is result screenshot:
Wix can create a contact but can’t send email to that contact. Please help me solve this. Maybe many people are facing the same situation.
Here is link to my page if you need it: https://www.handsonguitar.net/order
Hi @fullhouseit Did you solved this issue? Because I have the exact same problem : (
Hi Adriano, Did you solved this issue? Because I have the exact same problem : (