I am building a website contact form. I have form submissions being added to Firebase via Angular js. When a new submission is submitted to Firebase I am using child_added in Node to trigger nodemailer. This is working fine, except that the entire submissions collection is being re-emailed whenever I restart the dev server, and multiple times daily in production on Heroku. How can I ensure that emails won't be sent more than once?
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 protected]",
pass: "password"
}
});
var mailOptions = {
from: "Website <[email protected]>", // sender address
to: "[email protected]", // 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();
});
});