Email notification will be sent upon form submission to Firestore

I'm currently designing a website for booking reservations through an HTML form, with data submission to firestore. Upon submission, a confirmation email is sent to the customer. Below is the code snippet I am using to achieve this:

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");
  var [persons] = getInputVal("persons");

  saveMessage(email, packageFields, name, phone, date, persons);
  sendEmail(email, packageFields, name, date, persons);
  //show alert
}

// function to get form values

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

//save messages

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

function sendEmail(packageFields, name, date, persons) {
  Email.send({
    Host: "smtp.gmail.com",
    Username: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4733352e37373e26232e3122693022256926373707202a262e2b6924282a">[email protected]</a>",
    Password: "xxxxxxxxxxxxxxx",
    To: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f28187909a9d969b828d929caeb38efc8cfcecdcbec9994999396ce838f8dcfa68684">[email protected]</a>", 
    From: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc888e958c8c859d98959aa4dd90949fb69697929884de939f9d">[email protected]</a>",
    Subject: "Sending Email using Javascript",
    Body: `Your package of ${packageFields} for ${name} with total ${persons} persons (incl. ${name}) dated ${date} has been provisonalised. Your seat will be confirmed once you complete the payment of the Security Deposit`,
  }).then(function (message) {
    alert("Mail sent successfully");
  });
}

Everything is functioning correctly, but the To: field in the email is always fixed. Is there a way to dynamically change the To: value based on the inputted email address in the form?

Appreciate any assistance in advance.

Answer №1

To incorporate a new feature into your sendMail function, simply include the email argument and assign it to the To: field.

function sendEmail(email, packageFields, name, date, persons) {
  Email.send({
    Host: "smtp.gmail.com",
    Username: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f0f6edf4f4fde5e0edf2e1aaf3e1e6aae5f4f4c4e3e9e5ede8aae7ebe9">[email protected]</a>",
    Password: "xxxxxxxxxxxxxxx",
    To: email,
    From: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c484e554c4c455d58554a59124b595e125d4c4c7c5b515d5550125f5351">[email protected]</a>",
    Subject: "Sending Email using javascript",
    Body: `Your package of ${packageFields} for ${name} with total ${persons} persons (incl. ${name}) dated ${date} has been provisonalised. Your seat will be confirmed once you complete the payment of the Security Deposit`,
  }).then(function (message) {
    alert("mail sent successfully");
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an issue with the ProgressPlugin: TypeError - Unable to access property 'tap' of an undefined element

Encountering a Problem with npm run build Here's the issue at hand Compiling client E:\ui\sheraspace_ui\node_modules\webpack\lib\ProgressPlugin.js:223 compilation.hooks.addEntry.tap("ProgressPlugin", entryAdd ...

Tips for implementing event.preventDefault() with functions that require arguments

Here is a code snippet that I'm working with: const upListCandy = (candy) => { /* How can I add event.preventDefault() to make it work properly? */ axios.post("example.com", { candyName: candy.name }).then().ca ...

A guide on navigating to a different component in Vuejs using a link

<div class="enterprise-details" style="margin-top: 20px"> Already signed up? <a href="#"> LOGIN</a></div> <!-- Component for redirection --> <b-button v-if="!registeredUser" class="button-self" v-b-modal.modal-x>Lo ...

Error in jQuery submenu positioning issue

I am attempting to design a menu that opens when the parent element is clicked and closes when the mouse leaves the previously opened one. It should operate from left to right. Here is an example: <ul class="menuFirst"> <li><im ...

Maintaining consistent height using JavaScript

Dealing with equal height using just CSS can be a hassle, especially when you want to support older browsers like IE9. That's why I've decided to use JavaScript instead. If a user disables JavaScript, having unequal heights is the least of my con ...

Preventing users from copying and pasting information from my form by implementing javascript restrictions

I'm looking for a solution to prevent users from copying and pasting in my form using JavaScript. I want to restrict the ability to paste or copy any content into the form. Any assistance would be greatly appreciated! ...

Vue.js - Resetting child components upon array re-indexing

I am working with an array of objects const array = [ { id: uniqueId, childs: [ { id: uniqueId } ] }, { id: uniqueId, childs: [ { id: uniqueId } ] }, ] and I have a looping structure ...

What are the steps to displaying Jade in an Electron application?

With Express, you can easily establish the view engine as Jade by using the following code snippet: app.set('view engine', 'jade'); This enables Express to parse and deliver compiled HTML from Jade files directly. But how can this be ...

Step-by-step guide for importing a JSON file in React typescript using Template literal

I am facing an error while using a Template literal in React TypeScript to import a JSON file. export interface IData { BASE_PRICE: number; TIER: string; LIST_PRICE_MIN: number; LIST_PRICE_MAX: number; DISCOUNT_PART_NUM: Discout; } type Discoun ...

Error: The global variable cannot be emptied due to an issue with the Ajax request

As someone who is relatively new to the world of Javascript/jquery and async, I have spent a significant amount of time reading through various forums. Unfortunately, I have yet to come across a solution that addresses my specific issue. The problem at ha ...

Duplicate the canvas onto a new canvas

One of the functions I am using involves copying an old canvas to a new canvas. Here is the code snippet for this functionality: function cloneCanvas(oldCanvas) { //create a new canvas var newCanvas = document.createElement('canvas&a ...

`Warning: The alert function is not working properly in the console error

I am currently working on integrating otp functionality into my Ionic 3 project. I am facing an issue where I am able to receive the otp, but it is not redirecting to the otp receive page due to a specific error. Below is the console error that I am encou ...

What is the best way to trigger a controller action when the datepicker's value changes?

Hello, I have a custom datepicker and I am trying to perform a calculation when the year is changed. The code provided below does not seem to work on onchange. I also attempted using the onchange attribute and calling a JavaScript function like this oncha ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

I attempted to activate the hover effect on a DOM element using JavaScript, but was unsuccessful. It appears that achieving this is not possible. However, I am curious as to how Chrome is able to

I attempted to activate the hover state of a DOM element using JavaScript, but had no success. It appears that this task is not achievable in the way I initially approached it. I have heard that creating a class with a hover function and then adding or ...

Watching for changes to an object's value in an AngularJS service triggered by a controller from a separate module (Extended Edition)

Referring to this discussion on Stack Overflow: AngularJS trigger and watch object value change in service from controller The original question was about watching for changes in a service from a controller. I am interested in extending this concept to ...

JavaScript string manipulation function

Hey everyone, I need help finding a one-line solution in JavaScript to determine if a string contains a semicolon without using a loop. If anyone knows how to do this, please share your knowledge! ...

Unlinked Checkbox in Angular Bootstrap Modal Controller

I am currently utilizing the modal feature from Bootstrap 3's Angular fork, and I am facing an issue with using a checkbox within the modal and retrieving its value in my main controller. The checkbox value is properly bound in the view but not in th ...

Why won't the code for detecting the DEL key in a textarea work on my computer?

I've been trying to detect when a user presses the Delete key and came across this helpful tutorial here The code worked flawlessly on jsfiddle.net, you can check it out here- http://jsfiddle.net. However, when I copied the same code to my local comp ...

Unknown CSS element discovered: bootstrap, gradient, carousel

I recently created a random quote app using javascript, jQuery, and bootstrap on Codepen. Everything worked perfectly there. However, when I organized the files, pushed them to git, and tried to view the app from Safari, I encountered some warnings and t ...