TypeError: Failed to fetch when Triggered Email

Hi everyone,

I have read most of the post about my problem (I found at least 2 with the same problem) however it looks like I tried everything mentionned so here I come.

I have an email template edited and published identified by ‘cedricMailId’
I have a dataset (“dataset1”) where all my guest inputdata is supposed to get saved (everything is configured “connected to data”)

However, the clic on the submit button does trigger an error instead of an email.

The code is a bit long as the message sent in the email depends on the guest choices so we are going through different test to create the email based on 4 parameters :
from_name, coming_status, coming_people, message

Here is my code :

import wixCRM from 'wix-crm';
import wixLocation from 'wix-location';

$w.onReady(function () {
 //TODO: write your page related code here...
    $w("#dataset1").onAfterSave(() => {
        let from_name_value = $w("#input1").value;
        let email = $w("#input2").value; 
        let coming_status_value = "";
        let coming_people_value = "";
        let message_value = "";
        
        if($w("#radioGroup1").value==="NotCome"){
            coming_status_value='I (we) Will not come for the wedding\n';
        }
        else if($w("#radioGroup1").value==="Maybe"){
            coming_status_value='I (we) will MAYBE come for the wedding\n';
        }
        else if($w("#radioGroup1").value==="Come"){
            coming_status_value='I (we) will come for the wedding';
        }
 
        let nameId = 3;
        let dropDownId = 1;
        while( nameId < 8 ){    
            let input = "#input"+nameId;
            let name=$w(input).value;
            if( name === "" || name === "Name")
             break;
 
            let dropDown = "#dropdown"+dropDownId;
            let choice = $w(dropDown).value;
            coming_people_value += "\n ---> Name: "+name+" & Dinner choice : "+choice+"\n";
            nameId ++;
            dropDownId ++;
        }
        let emails = [email];
        wixCRM.createContact({
         "firstName": from_name_value,
         "emails": emails
        })
        .then(contactId => {
             return wixCRM.emailContact('cedricMailId', contactId, {
             variables: {
             "from_name": from_name_value,
             "coming_status": coming_status_value,
             "coming_people": coming_people_value,
             "message": message_value
             }
             })
            .then( () => {
             // do something after the email was sent
                console.log("email sent \n reload the home page");
                wixLocation.to(wixLocation.url);
             })
            .catch((err) => {
            // handle the error if the email wasn't sent
            console.log(`error : ${err}`);
            })
        })
    })
})

After long debugging hours, it looks like everything is going well until its trying to send the email.
I had lots of console.log() that I removed to simplify the code and I can confirm that the contact Id is created and the code reach the emailContact function where the error is triggered.

My website editor link is : https://editor.wix.com/html/editor/web/renderer/edit/c1c16606-c887-4593-ad79-601a524dd03b?metaSiteId=d5dbe868-6d29-4727-953d-09c81decfff8&editorSessionId=f822c33a-0c08-4398-8d89-830437e3a6c2&referralInfo=dashboard

Thanks for your help. I’m giving up … already 6 hours after trying different third part services first and then finally get to wix intagrated mail service. I already figured that you can not call createContact() with a member of wix (myself) … this took me 2hours lol … so i’m using now the email from the input.
But the idea is to send the email to myself (and another one to the guest) … long work to get there :slight_smile:

1 Like

Hi everyone again !!

So I figured it out myself … however this is a bit weird … I think my problem might help others so I will try to explain what I did to go around the error.

Apparently, reading the tutorial about sending a triggered email, the solution explained is to use wix-crm and/or wix-users and to develop all the code in the frontend file of your page …

However, it has been recently implemented the same tools to use as backend which should be anyway how to develop it for safety reasons. If you try to send a triggered email using a third party, then you have to develop it with some backend code.

So here is what I did : first create a new web module : I call it “sendEmail.jsw”
And I code it like that :

import wixCRM from 'wix-crm-backend';
import wixUsers from 'wix-users-backend';



export function sendEmail(
    from_name_value, emails, coming_status_value, 
    coming_people_value, message_value){
    wixCRM.createContact({
         "firstName": from_name_value,
         "emails": emails
    })
    .then(contactId => {
         return wixCRM.emailContact('cedricMailId', contactId,
         {  
         variables: {
         "from_name": from_name_value,
         "coming_status": coming_status_value,
         "coming_people": coming_people_value,
         "message": message_value
         }
        })
        .then( () => {
         // do something after the email was sent
         })
         .catch((err) => {
         // handle the error if the email wasn't sent
         console.log(`error : ${err}`);
         })
  })
 }

This creates a function that I will call on my main page code passing as arguments all the informations needed to create my email (you can put as many as you want).

And now on the page where I want my email triggered, I will first import this function and when the form is submit in my case (or in a button event function if you like) I will call composeAndSendEmail()

import {sendEmail} from 'backend/sendEmail.jsw';


$w.onReady(function () {
 //TODO: write your page related code here...
    $w("#dataset1").onAfterSave( () => {
        composeAndSendEmail();})
})

Finally, the last function call is the one doing all the work, getting all the values from the differents input from the dataset and creates the values sent to the backend function sendMail()

export function composeAndSendEmail(){
 let from_name_value = $w("#input1").value;
 let email = $w("#input2").value;
 
 let coming_status_value = "";
 let coming_people_value = "";
 let message_value = "";
 let emails = [email];

// I removed my long code processing the data from the user

 
  
   //call the backend function with the data processed
 sendEmail(from_name_value,emails,coming_status_value,coming_people_value,message_value)
 .then( () => {
 //what to do once the email is sent
 })
}


I hope this will help others with the same problem !! I close the topic if I can, if not maybe an admin could do it please. Thanks again.

thanks… helps a lot!

Thank you very much! you finally solve my problem. Very useful and smart solution.

Just an optimization: I use this more general sendEmail function in the backend

import wixCRM from ‘wix-crm-backend’ ;
import wixUsers from ‘wix-users-backend’ ;

export function sendEmail(triggerMailID,emails,variables)
{
wixCRM.createContact({
“emails” : emails
})
.then(contactId => {
console.log( “sending mail” );
return wixCRM.emailContact(triggerMailID, contactId, variables)
.then( () => {
// do something after the email was sent
console.log( “mail sent” );
})
. catch ((err) => {
// handle the error if the email wasn’t sent
console.log(error : ${err});
})
})
}

Thanks so much, I was stuck for so long and on the verge of just switching to another solution