https://www.wix.com/corvid/forum/community-discussion/failed-to-fetch-when-sending-triggered-email
I have the same problem like this post mention. When I try to send email to contact it gives me “failed to fetch” error. But create contact function is working fine. In that post GOS mention using event handler like onAfterSave but still no luck for me. Is anybody got solution? Or it is wix problem?
export function sendEmail_click(event) {
$w("#dataset1").onAfterSave(() => {
let contactInfo = {
"emails": [$w('#inputEmail').value]
};
console.log(contactInfo);
wixCrm.createContact(contactInfo)
.then((contactId) => {
console.log(contactId);
return wixCrm.emailContact('Ru2ePvr', contactId)
.then(() => {
console.log('Triggered email sent')
$w('#inputEmail').value = '';
})
.catch((error) => {
console.log(`Error: ${error}`)
$w('#inputEmail').value = '';
})
})
})
}
Have you seen the Wix support page for sending triggered email to contacts?
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-contacts
As shown on that page they have a newsletter signup form which captures the users interest area and name which will then be saved into a dataset from which you pull the data from with the after save command.
So your code should look something like this below, with the triggered email being triggered and sent after the user has submitted the user input form.
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,
//"lastName": $w('#lastName').value, << - not defined in the triggered email
"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}`);
});
});
});
Yes I’ve seen a lot of it. Sorry I don’t know where the difference beetwen your code and my code beside a lot of object you put in contact info.
Never mind I found a solution by using backend code.
Thankyou for replying me. Yes I’ve already read it a lot of time