Emails sent only to one address all others failed

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";
});
});
}


What you are forgetting is that the tutorial for sending a triggered email to contacts is based on you having a form that they can submit from.

https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-contacts
Although this article uses a form submission for demonstration purposes, you can send an email from anywhere in your code.

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.

You can see more info about using Wix CRM and it’s functions in the Wix API Reference here.
https://www.wix.com/corvid/reference/wix-crm.html

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.

So you need to add before you do all this is to an the onAfterSave function from Wix Dataset API.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onAfterSave

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}`);
});
});

Thanks for the answer.

I am still getting this error message:

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?

You should try to send the email (to the person not logged in) via a backend file using wix-crm-backend

Something like this maybe:

//page code

import {myBackendFunction} from 'backend/crmBck.jsw';
import wixCrm from 'wix-crm';

export function signUpButton_click(event) {
   wixCrm.createContact({
      "firstName": $w('#nameInput').value,
      "emails": [$w("#emailInput").value]
   })
   .then( (contactId) => {
      let variable1 = $w('#nameInput').value;
      let variable2 = $w('#interestArea').value;
      sendMail(contactId, variable1, variable2);
   })
   .catch((err) => {
      $w("#status").text = "" + err;
   });
}

function sendMail(contactId, variable1, variable2) {
   myBackendFunction(contactId, variable1, variable2)
   .then( (response) => {
      if(response.status === 'ok') {
          $w("#status").text = "Email sent"; //success
      } else {
          $w("#status").text = "" + response.error; //failure
      }
   });
}

Backend file:

//backend/crmBck.jsw

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(contactId, variable1, variable2) {
   return wixCrm.emailContact("newsletter_signup", contactId, {
      "variables": {
         "name": variable1,
         "interest_area": variable2
   })
   .then( () => {
      let data = {
         status: 'ok'
      };
      return data; //success
   })
   .catch( (err) => {
      let data = {
         status: 'failed',
         error: err
      };
      return data; //failure
   });
}

@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.

@shantanukumar847

I made the change and now is working perfectly. Thanks… There is one } missing in the backend file: Here is an updated version

import wixCrm from 'wix-crm-backend';
export function myBackendFunction(contactId, variable1, variable2) {
return wixCrm.emailContact("newsletter_signup", contactId, {
"variables": {
"name": variable1,
"interest_area": variable2
}
})
.then(() => {
let data = {
status: 'ok'
};
return data; //success
})
.catch((err) => {
let data = {
status: 'failed',
error: err
};
return data; //failure
});
}