Trying to run daily recurring job to send birthday mail to contacts saved in collection.
the happyBirthday() function runs well in debugging mode only.
Please help to get this solve.
I created a jobs.config that contains:
jobs.config
{
“jobs”: [{
// Option 2: Ddefine execution interval by setting time, day of week, and day of month
“functionLocation”: “/backend/birthdayMail.js(w)”, // Relative to Backend folder, started by slash
“functionName”: “happyBirthday”,
“description”: “Send Birthday mail to contacts”, // Optional
“executionConfig”: {
// “time”: “10:20” // “hh:mm” 24h format, UTC timezone (e.g. 13:00)
“cronExpression”: “13 20 * * *” // Optional: Uncomment section below for more complex intervals
}
},
]
}
I also created happyBirthday.jsw in the backend (not in a folder) which contains:
import wixData from ‘wix-data’;
import { getJSON } from ‘wix-fetch’;
import { sendEmail } from ‘backend/sendEmail.jsw’;
import sgMail from ‘@sendgrid/mail’;
import wixSecretsBackend from ‘wix-secrets-backend’;
export async function sendEmail1(recipient, subject, body) {
const sendGridSecret = JSON.parse(await wixSecretsBackend.getSecret('sendGridSecret'));
const key = sendGridSecret.key;
const senderEmail = sendGridSecret.senderEmail;
sgMail.setApiKey(key);
happyBirthday();
const msg = {
from: senderEmail,
to: mailId,
subject: subject,
text: body
};
try {
return await sgMail.send(msg);
} catch (error) {
console.error('Error sending the email: ' + error.message);
return false;
}
}
// send birthday mail
export async function happyBirthday() {
let now = new Date();
let todayMonth = now.getMonth() + 1;
let todayDate = now.getDate();
console.log(now, “Today’s date”);
console.log(todayDate, "Today date");
console.log(todayMonth, "Today Month");
let birthDate
let birthMonth
let monthDiff
let datediff
// let mailId
// let subject
// let body
wixData.query("Team")
.find()
.then((results) => {
if (results.items.length > 0) {
let contacts = results.items[0];
let bdate = contacts.birthdate;
// let dbate= "2022-11-30"
let mailId = contacts.email;
// let mailId = "keer.anu23@gmail . com";
let subject = "Happy Birthday to you!";
let body = "Wishing You a Very Happy Birthday,Have a good day!";
console.log(contacts);
console.log(bdate, "User Birthdate"); //user birthdate
let birthDate = bdate.getDate()+1;
// let newBirthDate = new Date();
// newBirthDate.setDate(birthDate + 1);
// let birthDate = 3;
let birthMonth = bdate.getMonth() + 1;
// let birthMonth = 12;
console.log(results)
console.log(birthDate, "user bdate");
console.log(birthMonth, "user bMonth");
let monthDiff = todayMonth - birthMonth;
let datediff = todayDate - birthDate;
console.log(monthDiff);
console.log(datediff);
// console.log("dates matched, Happy Birthday")
// sendEmail(mailId, body, subject);
// const emailResult = sendEmail(mailId, body, subject);
// function (params) {
// }
if (monthDiff === 0 && datediff === 0) {
console.log(mailId);
console.log(body);
console.log(subject);
const emailResult = sendEmail(mailId, body, subject);
// clearFields();
// displayMessage1('Happy Birthday! Mail has been sent successfully!');
console.log("dates matched, Happy Birthday")
} else {
// displayMessage1('Your Birthday is coming soon');
console.log("Your Birthday is coming soon")
}
} else {
console.log("error")
}
})
// happyBirthday();
// function sendMail() {
// // console.log("function called")
// if (monthDiff === 0 && datediff === 0) {
// // happyBirthday();
// console.log("dates matched, Happy Birthday")
// } else {
// console.log("Your Birthday is coming soon")
// }
}
// }