I would like to know if anyone knows a way where you can send an email when a specific node in firebase is created, updated or deleted?
More specifically, I have a web service where users can book each other for a period of time. I use Firebase as the backend to store user information and so on but I want to send a confirmation to the users' email address whenever a booking has occurred. I understand that this has previously been possible using Zapier, however they no longer support Firebase.
Anyone who has a workaround or an idea on how to send email notifications in Firebase?
Made by FirebaseComposes and sends an email based on the contents of a document written to a specified Cloud Firestore collection.
Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.
After searching some more I found that there is no direct plugin. I contacted the firebase support team and they are considering to create this functionality themselves; however, when and how is not yet decided.
There are different possibilities but they all require some kind of back end coding, meaning a server that can watch for these changes.
I landed on node.js as this is most similar to what I usually work on, i.e. javascript.
By including nodemailer and firebase through npm, sending emails based on firebase event was achieved like this:
var firebase = require("firebase");
var mainApp = firebase.initializeApp({
//firebase authentication
});
var directTransport = require('nodemailer-direct-transport');
var nodemailer = require('nodemailer');
var options = {};
var transporter = nodemailer.createTransport(directTransport(options))
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'example.gmail.com'
pass: 'password'
}
});
var nodeToWatch = mainApp.database().ref('node_to_watch'); //firebase node to watch
nodeToWatch.on('child_added', function(childSnapshot) { //fires once for every node in this location
if(!childSnapshot.val().email_sent){ //check if email has already been sent
nodeToWatch.child(childSnapshot.key).child('email_sent').set(firebase.database.ServerValue.TIMESTAMP).then(function(response){ //set email value to sent
var data = JSON.stringify(childSnapshot.val(), null, 9); //stringify the response so we can attach it in the email
// create template based sender function
var sendInfo = transporter.templateSender({
subject: 'Node in firebase updated',
html: 'Hello, data for new node is: {{data}}
}, {
from: 'example.gmail.com'
});
// use template based sender to send a message
sendInfo({
to: 'exampleRecipient.email.com'
}, {
data: data //data variable to insert into email
}, function(err, info){
if(err){
console.log(err);
}
else{
console.log('Email sent');
}
});
});
}
});
This example listens for the firebase on_child_added
event but any firebase event will work. Hope this helps someone! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With