In development I was able to set a button for testing to send a triggered email when pressing button.
This is that code:
export function triggerEmail_click_1(event) {
//Add your code for this event here:
console.log( "TRIGGER pressed" )
//Add your code for this event here:
const endDate = new Date()
//console.log(endDate + " TODAY")
endDate.setDate(endDate.getDate() - 4 );
//console.log(endDate + " END DATE")
const startDate = new Date()
startDate.setDate(startDate.getDate() - 11 );
//console.log(startDate + “START DATE”)
wixData.query( "contact01" )
.eq( "shortTextField17" , "1st" )
//.between(“_updateDate”, startDate, endDate)
.find()
.then((results) => {
if (results.items.length > 0 ) {
let totalCount = results.totalCount;
console.log(totalCount + " TOTALRECS" )
let items = results.items;
let firstItem = items[ 0 ];
console.log(firstItem.contactEmails + " CONTACT EMAIL" )
function iterate(firstItem) {
var date = firstItem._createdDate
var testDate = date.text = date.toLocaleDateString()
wixCrm.createContact({
“firstName” : firstItem.firstName,
“email” : firstItem.email
})
.then((contactId) => {
wixCrm.emailContact( “FollowUpEmail” , contactId, {
“variables” : {
“firstName” : firstItem.firstName,
“quoteNo” : firstItem.shortTextField8,
“createdDate” : testDate
}
})
.then(() => {
console.log( “EMAIL SENT” )
})
. catch ((err) => {
console.log( “EMAIL FAILED” )
})
})
}
items.forEach(iterate);
} else {
// handle case where no matching items found
}
})
. catch ((error) => {
let errorMsg = error.message;
let code = error.code;
});
}
This works perfectly.
This is the same code in sendMail.js
import wixData from ‘wix-data’ ;
import { sendEmail, sendEmailWithRecipient } from ‘backend/email’
import wixCrm from ‘wix-crm’ ;
export function sendContactEmailsUndefined() {
//Add your code for this event here:
console.log( “Cron RUN” )
//Add your code for this event here:
const endDate = new Date()
//console.log(endDate + " TODAY")
endDate.setDate(endDate.getDate() - 4 );
//console.log(endDate + " END DATE")
const startDate = new Date()
startDate.setDate(startDate.getDate() - 11 );
//console.log(startDate + “START DATE”)
wixData.query( "contact01" )
.eq( "shortTextField17" , "1st" )
//.between(“_updateDate”, startDate, endDate)
.find()
.then((results) => {
if (results.items.length > 0 ) {
let totalCount = results.totalCount;
console.log(totalCount + " TOTALRECS" )
let items = results.items;
let firstItem = items[ 0 ];
//console.log(firstItem.contactEmails + " CONTACT EMAIL")
function iterate(firstItem) {
var date = firstItem._createdDate
var testDate = date.text = date.toLocaleDateString()
wixCrm.createContact({
“firstName” : firstItem.firstName,
“email” : firstItem.email
})
.then((contactId) => {
wixCrm.emailContact( “FollowUpEmail” , contactId, {
“variables” : {
“firstName” : firstItem.firstName,
“quoteNo” : firstItem.shortTextField8,
“createdDate” : testDate
}
})
.then(() => {
console.log( “EMAIL SENT” )
})
. catch ((err) => {
console.log( “EMAIL FAILED” )
})
})
}
items.forEach(iterate);
} else {
// handle case where no matching items found
}
})
. catch ((error) => {
let errorMsg = error.message;
let code = error.code;
});
}
export function sendContactEmails2nd() {
console.log( "2nd CRON Success" )
const subject1 = “TESTING 2ndEmail SUBJECT”
const body1 = “TESTING 2ndEmail BODY”
const recipient1 = “mark.hancock@familyheritagepublishers.com”
sendEmail(subject1, body1, recipient1)
.then(response => console.log(response));
}
This is the jobs.config.js
{
“jobs” : [{
“functionLocation” : “/mailutilities/mailSend.js” ,
“functionName” : “sendContactEmailsUndefined” ,
“executionConfig” : {
“cronExpression” : “05 * * * *”
}
}]
}
This is the error I receive from site monitoring.
“[“Error loading web module backend/mailutilities/mailSend.js: Cannot find module ‘wix-crm’\nRequire stack:\n- /user-code/backend/mailutilities/mailSend.js\n- /user-code/stubmodule-that-does-the-require.js\n- /elementory/node_modules/scoped-require/index.js\n- /elementory/create-app.js\n- /elementory/cloud-grid-runner.js”]”
ANY help would be appreciated. Is this even doable?