我正在建立一个网站联系表。我通过 Angular js 将表单提交添加到 Firebase。当向 Firebase 提交新的提交时,我在 Node 中使用 child_added 来触发 nodemailer。这工作正常,除了每当我重新启动开发服务器时都会重新通过电子邮件发送整个提交集合,并且每天在 Heroku 上的生产中多次发送。如何确保电子邮件不会被多次发送?
var myRoot = new Firebase('https://firebase.firebaseio.com/website/submissions');
myRoot.on('child_added', function(snapshot) {
var userData = snapshot.val();
var smtpTransport = nodemailer.createTransport("SMTP",{
auth: {
user: "email@gmail.com",
pass: "password"
}
});
var mailOptions = {
from: "Website <email@gmail.com>", // sender address
to: "email@email.com.au", // list of receivers
subject: "New Website Lead", // Subject line
html: "<p><strong>Name: </strong>" + userData.name + "</p>" + "<p><strong>Email: </strong>" + userData.email + "</p>" + "<p><strong>Phone: </strong>" + userData.phone + "</p>" + "<p><strong>Enquiry: </strong>" + userData.enquiry + "</p>" + "<p><strong>Submitted: </strong>" + userData.date + "</p>" // html body
};
smtpTransport.sendMail(mailOptions, function(error, response) {
if(error) {
console.log(error);
}
else {
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
});