如何使用nodemailer检查录制的消息并防止再次发送类似的消息?

时间:2018-06-18 02:09:01

标签: node.js nodemailer loopback

我可以知道如何检查已发送的邮件并阻止再次发送,直到在某种情况下发送另一条消息?下面是代码,我使用nodemailer和Node.js:

client.on('message', function (topic, temperature, timestamp, name, description, tags, last_activity, last_temperature) {
    var messageObject = {
        topic: topic,
        temperature: temperature.toString(),
        timestamp: new Date(),
        name: topic,
        description: null,
        tags: null,
        last_activity: new Date(),
        last_temperature: temperature.toString()
    };
    console.log(messageObject);

    if(temperature>25){
      //for sending the message to the email
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@gmail.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was higher than the temperature of 25 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });          
    }else if(temperature<19){
      //for sending the message to the phone number
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@gmail.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was smaller than the temperature of 19 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });   
    }

    collection.insert(messageObject, function(error, result) {
        if(error != null) {
            console.log("ERROR: " + error);
        }
    });
});
});

从上面的代码中,当设备的温度超过25时,它将仅发送一次通知消息,直到温度低于19,然后它将再次发送另一个通知消息,也只发送一次。因为在我的情况下,如果温度超过25或低于19,它将继续一次又一次地发送消息,所以我可以知道如何检查已发送的消息并阻止它再次发送类似的消息吗? 或者使用其他方法来做到这一点?谢谢你的预先建议。

0 个答案:

没有答案