title: Using cron in Meteor to send emails in the future
body: <p>I am using <a href="https://github.com/percolatestudio/meteor-synced-cron" rel="nofollow"><code>percolatestudio/meteor-synced-cron</code></a> to try and send email reminders that an invoice is due. For example, if I send an invoice and select "due in 7 days", i want the recipient of my invoice to get an email 7 days from now saying that the invoice is due.</p>
  
  <p>createInvoice.js (client)</p>
  
  <pre><code>Template.createTransfer.events({
    'submit form': function (event, template) {
      event.preventDefault();
  
      var transfer = {
        userId: Meteor.userId(),
        date: Date(),
        files: Session.get('files'),
        senderEmail: template.find('#senderEmail').value,
        recipientEmail: template.find('#recipientEmail').value,
        message: template.find('#message').value
      };
  
      if (Session.get('invoice')) {
        transfer.invoice = {
          total: template.find('#total').value,
          due: template.find('#due').value,
          stripe: Meteor.user().services.stripe
        };
      }
  
      Transfers.insert(transfer, function (error, id) {
        if (error) {
          showError(error);
        } else {
          Session.set('files');
          showAlert('Transfers sent!');
          transfer._id = id;
          Meteor.call('sendEmail', transfer);
          Router.go('viewTransfer', {_id: id});
        }
      });
      scheduleEmail(transfer);
  
    }
  });
  
  function scheduleEmail(transfer) {
  
    if (transfer.date &lt; moment(new Date())) {
      Meteor.call('sendMail', transfer);
    } else {
      if (transfer.invoice.due == "7 days") {
        if (moment(transfer.date).add(7, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
          var thisId = FutureEmails.insert(transfer);
          Meteor.call('addCronMail',thisId, transfer);
        }
      } else if (transfer.invoice.due == "14 days") {
        if (moment(transfer.date).add(14, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
          var thisId = FutureEmails.insert(transfer);
          Meteor.call('addCronMail',thisId, transfer);
        }
      } else if (transfer.invoice.due == "30 days") {
        if (moment(transfer.date).add(30, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
          var thisId = FutureEmails.insert(transfer);
          Meteor.call('addCronMail',thisId, transfer);
        }
      } else if (transfer.invoice.due == "90 days") {
        if (moment(transfer.date).add(90, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
          var thisId = FutureEmails.insert(transfer);
          Meteor.call('addCronMail',thisId, transfer);
        }
      }
    }
  
  }
  </code></pre>
  
  <p>methods.js (server)</p>
  
  <pre><code>Meteor.methods({
  
    //other methods
  
    sendMail: function(transfer) {
  
      check([transfer.senderEmail,
        transfer.recipientEmail,
        transfer.message,
        // transfer.invoice.total
        ], [String]);
  
        // Let other method calls from the same client start running,
        // without waiting for the email sending to complete.
        this.unblock();
  
        transfer.url = Meteor.absoluteUrl() +'transfer/' + transfer._id;
  
        var template = 'invoice-due';
  
        Email.send({
          to: transfer.recipientEmail,
          bcc: transfer.senderEmail,
          from: transfer.senderEmail,
          subject: transfer.senderEmail + ' sent you files!',
          html: Handlebars.templates[template](transfer)
        });
  
      },
  
      addCronMail: function(id, transfer) {
  
        SyncedCron.add({
          name: id,
          schedule: function(parser) {
            // return parser.recur().on(transfer.date).fullDate();
            return parser.recur().every(3).minute();
          },
          job: function() {
            Meteor.call('sendMail',transfer);
            FutureEmails.remove(id);
            SyncedCron.remove(id);
            return id;
          }
        });
  
      }
  });
  </code></pre>
  
  <p>and startup.js(server)</p>
  
  <pre><code>Meteor.startup(function () {
    FutureEmails.find().forEach(function(mail) {
      if (moment(mail.date).format("MMM Do YY") &lt; moment(new Date()).format("MMM Do YY")) {
        Meteor.call('sendMail', mail);
      } else {
        Meteor.call('addCronMail', mail._id, mail);
      }
    });
    SyncedCron.start();
  });
  </code></pre>
  
  <p>I know im checking for the different due dates in the wrong place. Where do I perform the check to see if it's been 7, 14, etc days and if it has, send out the email?</p>