Sending a Triggered Email to Contacts

Hi, I have a triggered email that I would like sent when someone on my site completes a questionnaire. I have used the Wix Code Tutorial: Sending a Triggered Email to Contacts.

From my understanding, the code creates a new contact and then sends a “triggered email”.

However, the code is not working to create a contact or send the email. I’m not sure if something is wrong on my end or if there is a bug, but I can’t get it to work!

Would anyone know what I’m doing wrong?

I tried this with several email addresses. I’m not logged in to the site and also these email addresses are not yet present in my contacts.

This is the URL with the form: https://www.wixcreate.com/test-questionnaire

This is the code:

I very much appreciate any help on this!

hi i want export database from wix to my tool ( Email Verifier Software — Bulk Email Address Verification Tool — Massmail Software )for mass sms sending. can you help me?

Hi Linda,

I’m working on essentially the same thing and have had the same result. Looking for a solution. If you’ve figured it out, I’d love to hear how. If not, I’ll try to post here if I’m able to find the solution.

Edit: I have mine working now. I’m using wixUsers rather than wixCRM and I the user needs to be logged in as well as registered for it to work. Not sure if the same is true of wixCRM. Also, check carefully how many emails have been sent when you’re testing. I was watching the inbox and not seeing emails as they were getting filtered to gmail’s promotions tab, which I hadn’t noticed. I thought the emails just weren’t going out until I checked to see that the number of sent emails was going up!

Best,

Justin

1 Like

You have missed out the all important onReady call at the start of your code, plus you need to add the then and catch parts at the end of your code too.

Plus remember that you can only test it on your live and published site, you can’t test it whilst in preview mode.

Here is the full code from the tutorial page that you were looking at:

import wixCRM from 'wix-crm';

$w.onReady(function () {

});

export function signUpButton_click(event) {
 wixCRM.createContact({
   "firstName": $w('#nameInput').value,
   "emails": [$w("#emailInput").value],
   "interest_area": $w("#interestArea").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
    })
    .catch((err) => {
     // handle the error if the email wasn't sent
    });
  });
}

This is the code that I use on my page, however I’ve added a on After Save datahook to mine too, so the code on my page will be different to yours.

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;

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

Did you just use the send triggered email to members code instead?

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

Hello Coders!

Does anybody got this to work? I am having the same error and I can’t get it to work, I always get the error:
Uncaught (in promise) contactId does not match current session contact (401)
Sadly i can’t use the logged in user version because this is a lead capturing form.

Any Ideas, comments, experiences?

Thanks a lot guys!

Hi Fernandust, I do have it working using the wixUsers version. Unfortunately, I’m not familiar enough with the CRM to offer any insight into that error. I understand that you can’t use it for the finished version, but perhaps switch to the wixUsers version and do some testing keeping the rest of your code the same? If it works that way, we know it’s related to the CRM version code. If it still doesn’t work, there may be something else going on. Either way, could help to isolate the issue…

@beyondobediencetrain hi Justin, thanks for your reply. Great idea, I’ll do this. Have you also tried to do it with the crm version?

@fernandust I remember trying that way. I remember having a lot of issues getting the process in general to work, but I couldn’t say whether there were issues with the crm version specifically. Sorry!

So not sure if this will help but I had similar issue and it would fail on the send Email part. It would create the contact fine, but fail on the the Send Email part. My problem was because I was doing http-request to backend code on the test/dev system and not on the published/live site. So not sure if some people might be having issue with live data vs. non-live data? Anyway hope this helps.
Tom