0

I am trying to create an email scheduler which sends an email to the user at a selected time.The system works fine when I run it locally.But when I deploy it on heroku the e-mail is not sent and also no errors are shown on the console. I have already tried allowing access from less secure apps and https://accounts.google.com/DisplayUnlockCaptcha but it still doesn't seem to work.

heroku log


2019-10-22T08:31:47.237225+00:00 app[web.1]: Server started on port 31589
2019-10-22T08:31:47.333128+00:00 heroku[web.1]: State changed from starting to up
2019-10-22T08:31:47.741159+00:00 app[web.1]: mongodb connected
2019-10-22T08:35:53.528505+00:00 heroku[router]: at=info method=GET path="/entries/search" host=diarygenie.herokuapp.com request_id=813c26df-4a70-40d3-a69c-5560354d11fb fwd="42.107.205.105" dyno=web.1 connect=0ms service=35ms status=302 bytes=393 protocol=https
2019-10-22T08:35:53.851462+00:00 heroku[router]: at=info method=GET path="/users/login" host=diarygenie.herokuapp.com request_id=87131056-e50f-4276-8b7e-90df4c36dadb fwd="42.107.205.105" dyno=web.1 connect=0ms service=27ms status=200 bytes=2660 protocol=https
2019-10-22T08:36:10.811904+00:00 heroku[router]: at=info method=POST path="/users/login" host=diarygenie.herokuapp.com request_id=d5efc1ea-8fd7-42da-8d43-31a7448cc0cd fwd="42.107.205.105" dyno=web.1 connect=0ms service=271ms status=302 bytes=261 protocol=https
2019-10-22T08:36:11.161024+00:00 heroku[router]: at=info method=GET path="/dashboard" host=diarygenie.herokuapp.com request_id=41445986-47c2-4f5d-a40b-1dd172c8d116 fwd="42.107.205.105" dyno=web.1 connect=0ms service=48ms status=200 bytes=3314 protocol=https
2019-10-22T08:36:14.226747+00:00 heroku[router]: at=info method=GET path="/email" host=diarygenie.herokuapp.com request_id=d35ba608-cd0d-4223-8c35-30afef184e62 fwd="42.107.205.105" dyno=web.1 connect=1ms service=43ms status=200 bytes=4121 protocol=https
2019-10-22T08:36:50.777252+00:00 heroku[router]: at=info method=POST path="/email" host=diarygenie.herokuapp.com request_id=55b0bcfa-be8f-4762-b562-edef914ee36b fwd="42.107.205.105" dyno=web.1 connect=0ms service=48ms status=302 bytes=261 protocol=https
2019-10-22T08:36:51.108211+00:00 heroku[router]: at=info method=GET path="/dashboard" host=diarygenie.herokuapp.com request_id=9e156601-51e7-4e7c-a9f4-14c8d28e3ba7 fwd="42.107.205.105" dyno=web.1 connect=0ms service=45ms status=200 bytes=3597 protocol=https

routes/email.js

router.post('/',ensureAuthenticated,(req,res) => {
    const {title,email,date,time} =req.body;
    var temp = time.split(':');
    var hour = parseInt(temp[0],10);
    var minute = parseInt(temp[1],10);
    //console.log(hour,minute);
    var mailAccountUser = 'diarygenie123@gmail.com'
    var mailAccountPassword = process.env.PASSWORD;
    var transport = nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    auth: {
        user: mailAccountUser,
        pass: mailAccountPassword
    }
    }));


      const message = {
        from: 'genie@diarygenie.com', // Sender address
        to: req.user.email,         // List of recipients
        subject: title, // Subject line
        text: email // Plain text body
    };
    var mailDate = new Date(date); 
    //console.log(hour,minute);
    mailDate.setHours(hour);
    mailDate.setMinutes(minute);
    temp = new Date();

    //check if date is past date
    if(mailDate < new Date()){
      var dtToday = new Date();

      var month = dtToday.getMonth() + 1;
      var day = dtToday.getDate();
      var year = dtToday.getFullYear();
      if(month < 10)
        month = '0' + month.toString();
      if(day < 10)
        day = '0' + day.toString();

      var maxDate = year + '-' + month + '-' + day;
      var alert = [{msg:"Genie can't sent mails to the past XD .Please enter a future date and time"}];
      res.render('email',{errors:alert,name:req.user.name,maxDate:maxDate,title:title,email:email});
      return;
    }

    //validation passed

    mailDate.setSeconds(temp.getSeconds());

    //console.log(mailDate); //this date is in UTC


    //scheduling job to sent email at a given time
    nodeSchedule.scheduleJob(mailDate,() =>{
        transport.sendMail(message, function(err, info) {
            if (err) {
              console.log(err)
            } else {
              console.log(info);
              console.log('email sent successfully');
            }
            transport.close();
        });

    });
    req.flash('success_msg','Email scheduled successfully')
    res.redirect('/dashboard');  

});

module.exports = router;

Jose Dominic
  • 27
  • 1
  • 11

1 Answers1

1
IamJMBassey
  • 61
  • 1
  • 5