0

我有一个 cronjob 设置为在用户访问我网站上的页面 1 天后发送电子邮件。因此,如果用户多次访问此页面,电子邮件将被发送多次。我如何使它只在用户第一次访问此页面时发送一封电子邮件,之后每次用户访问该页面时,在发送第一封电子邮件之前不会执行 cronjob。我正在使用 sendgrid 发送电子邮件和 cronjob 的库节点计划。

const schedule = require('node-schedule');

const date = new Date();
date.setTime(date.getTime() + 1 * 24 * 60 * 60 * 1000);

useEffect(() => {
      //cron job to send the email after 1 day
      schedule.scheduleJob(date, async function () {
        const to = {
          name: resultInfo.candidate.firstName,
          email: resultInfo.candidate.email,
        };
        const param = {
          candidate: resultInfo.candidate,
          WSProject: resultInfo.WSProject,
          company: resultInfo.company,
        };
        axios
          .post('/api/sendgrid/candidate/viewed', {
            to,
            param,
          })
          .catch((error) => {
            console.log(error);
          });
      });
  }, []);

api 请求在此处发送。

const sendCandidateViewedEmail = async (req, res) => {
  const { to, param } = req.body;
  await SendGridService.sendCandidateViewedEmail(to, param);
  res.send('ok');
};

这是使用 sendgrid 发送电子邮件

const sendCandidateViewedEmail = async (to, params) => {
  const html = await ejs.renderFile(
    templatePath + '/candidate_viewed.ejs',
    Object.assign(params, DefaultParams)
  );
  return await sendEmail(
    null,
    to,
    'Your Assessment Result Has Been Viewed',
    html
  );
};
4

0 回答 0