Skip to content
Advertisement

Send confirmation e-mail to client on saving data to firestore

I am trying to make a website for taking online bookings. The site has been hosted in firebase and I am using firestore to collect the details of booking. I am using the code given below to collect the details in firestore.

var firestore =  firebase.firestore();

var messagesRef = firestore.collection("BookingData");


//listen for submit
document.getElementById('bookingForm').addEventListener('submit',submitForm);

function submitForm(e){
 e.preventDefault();

 //get values
var email = getInputVal('email');
var packageFields = getInputVal('packageFields');
var name = getInputVal('name');
var phone = getInputVal('phone');
var date = getInputVal('date');

saveMessage(email, packageFields, name, phone, date);

//show alert
}

// function to get form values

 function getInputVal(id) {
return document.getElementById(id).value;
 }

//save messages

function saveMessage(email, packageFields, name, phone, date) {

  messagesRef.add({
   email:email,
   packageFields:packageFields,
   name:name,
   phone:phone,
   date:date
   }).then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
 .catch(function(error) {
  console.error("Error adding document: ", error);
});

}

Now the problem is, I would like to send email to my client and get a email at my personal email id whenever there is a booking [i.e. the data get saved in firestore (Document Written with id)] using JS and SMTP.

The email will be send automatically to the provided email id.

How can I do so.

Thanks for any kind of help in advance.

Advertisement

Answer

One of the possible solutions is to use the Firebase extension dedicated to email sending.

Since you want to send an email when a new document is created in the BookingData collection, it will be a piece of cake to configure it.

Just follow the configuration instructions and enter “BookingData” for the field “Email documents collection” (“Email documents collection” is “the path to the collection that contains the documents used to build and send the emails”)

Then, as explained in the doc here, in the document created in the BookingData collection, include a to field with the same value than the email and a cc, (or bcc) field with your own email. Then, use the document’s message field to specify the other email elements, like subject line and email body (either plaintext or HTML).


Note that doing so will add all this extra info (together with some fields containing the status of the extension execution) to the BookingData document. If you prefer to avoid having these extra info added to this document, just use another dedicated collection to trigger (and configure) the emails.

To generate and send an email via this specific, dedicated collection, you could use a batched write, as follows:

var messagesRef = firestore.collection("BookingData");
var emailsRef = firestore.collection("emails");  // Additional collection

var batch = firestore.batch();

batch.set(messagesRef, 
 {
   email:email,
   packageFields:packageFields,
   name:name,
   phone:phone,
   date:date
   }
);

batch.set(emailsRef, 
 {
   to:email,
   bcc:'youremail@mail.com',
   message: {
    subject: 'New order',
    html: 'This is an <code>HTML</code> email body.',
   }
  }
);
// Commit the batch
batch.commit().then(function () {
    // ...
});

Don’t forget to:

  • Deny read and write access rights to the emails collection via security rules.
  • Enter “emails” for the field “Email documents collection”, when configuring the extension.

And note that to install and use Firebase extensions, your project must be on the Blaze plan.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement