Email Notification

Hello everyone, a quick question: does Parse Server provides scheduled email notification? For example: user can set email notification - like a reminder - where Parse Server will email user on a selected (by the user) date with user input message, ex: “please login into an app - there is something that needs your attention.”

Thanks.

1 Like

Which emailAdapter / mail provider are you using?

You will probably need cron scheduler, and query something like:

const schedule = require("node-schedule");
const rule = new schedule.RecurrenceRule();
rule.hour = 17;
schedule.scheduleJob(rule, async () => {
  const query = new Parse.Query("Actions");
  query.equalTo("complete", false);

  const yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);

  const lastWeek = new Date();
  lastWeek.setDate(lastWeek.getDate() - 7);

  const userQuery = new Parse.Query(Parse.User);
  userQuery.lessThan("lastOpened", yesterday);
  userQuery.lessThan("lastEmailed", lastWeek);

  query.matchesQuery("user", userQuery);
  query.include("user.firstName", "user.email");
  query.exists("user.firstName", "user.email");

  const actionsToComplete = await query.find({ useMasterKey: true });

  const mailgun = require("mailgun-js")({ apiKey: api_key, domain: domain });

  const usersToSave = [];
  for (const action of actionsToComplete) {
    const user = action.get("user");
    const data = {
      from: "Excited User <[email protected]>",
      to: user.get("email"),
      subject: "You have tasks to complete",
      text: `Hi ${user.get("firstName")}, you have tasks to complete.}`,
    };
    mailgun.messages().send(data);
    user.set("lastEmailed", new Date());
    usersToSave.push(user);
  }
  await Parse.Object.saveAll(usersToSave, { useMasterKey: true });
});

Obviously this depends on your data structure or mail provider.

1 Like

Thanks, let me take a look at adapters first.

On the master branch, you can now send email via:

Parse.Cloud.sendEmail({
      from: "Excited User <[email protected]>",
      to: user.get("email"),
      subject: "You have tasks to complete",
      text: `Hi ${user.get("firstName")}, you have tasks to complete.`,
})

Sorry for ignorant question, but who provides email service for the parse instance?

You set your own email provider.

That’s ok, you’re welcome to ask questions.

There is a little bit of information around mail adapters on the Parse Server GitHub. The email provider you choose is up to you (price, availability, etc).