For some reason, I have only been able to send an email using wixCrm to the first email I tried. In all other cases, the contact is created but the email is not sent.
I even recreate the basic functionality on a test page (copied here), with no success.
Your support will be appreciated.
Seems that there is no issue with the coding, but I have not been able to find where the error is.
import wixCrm from 'wix-crm';
import {local} from 'wix-storage';
$w.onReady(function () {
local.clear();
});
export function signUpButton_click(event) {
wixCrm.createContact({
"firstName": $w('#nameInput').value,
"emails": [$w("#emailInput").value]
})
.then((contactId) => {
wixCrm.emailContact("newsletter_signup", contactId, {
"variables": {
"name": $w('#nameInput').value,
"interest_area": $w("#interestArea").value
}
})
.then(() => {
// do something after the email was sent
$w("#status").text = "Email Sent";
})
.catch((err) => {
// handle the error if the email wasn't sent
$w("#status").text = "Email NOT Sent";
});
});
}
Plus, you will notice that there is a gap within the code sample (marked with the // …) for you to have additional code for you to be able to add this snippet for the triggered email to.
Plus, make sure that you are testing all this on a live published site and not on the preview mode. Note:Triggered Emails may not work properly when previewing your site. Publish your site to test the code found below.
It is the same with the Wix Users API where it will only work fully when used on the live published site and not in preview.
I have done similar on a website and here is the code that I used on the page, I have two forms for public users on it, so if you only have the one then simply delete the second one.
You also don’t need Wix Storage on this form either, unless you are using it somewhere else on your page and need it for that.
import wixCRM from 'wix-crm';
$w.onReady(function() {
$w("#PublicContactUs").onAfterSave(() => {
let name = $w('#publiccontactName').value;
let email = $w("#publiccontactEmail").value;
let subject = $w("#publiccontactSubject").value;
let message = $w("#publiccontactMessage").value;
let label = ["Contacted Us"];
wixCRM.createContact({
"name": name,
"emails": [email]
})
.then((contactId) => {
// Need to use the triggered email name
return wixCRM.emailContact('publiccontactus', contactId, {
"variables": {
// Need to use the triggered email variable names
"name": name,
"email": email,
"subject": subject,
"message": message
}
});
})
.catch((err) => {
// handle the error if the email wasn't sent
console.log(`Error: ${err}`);
});
});
});
$w("#NewSubscriber").onAfterSave(() => {
let name = $w("#newsubscriberName").value;
let email = $w("#newsubscriberEmail").value;
let privacyPolicy = $w("#newsubscriberPrivacy").value;
let label = ["Subscribed"];
wixCRM.createContact({
"name": name,
"emails": [email]
})
.then((contactId) => {
// Need to use the triggered email name
return wixCRM.emailContact('newsubscriber', contactId, {
"variables": {
// Need to use the triggered email variable names
"name": name,
"email": email,
}
});
})
.catch((err) => {
// handle the error if the email wasn't sent
console.log(`Error: ${err}`);
});
});
contactId does not match current session contact (401)
By reading another discussion it seems that other people are having the same problem. Is there any way that I can reset/clear the current session contact?
@shantanukumar847
Yes it would be great that they mention in their original forum post about wanting to send to contacts who are not logged in and not just talk about using the Wix triggered email function for sending emails to contacts who are currently using the site.