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

Is it possible to dynamically add the URL to an iframe based on a condition being true, and then iterate through a list of URLs before

I'm trying to figure out how to change the URL in an iframe based on the presence of a class="show". The first time the element has the class "show," it should load about.html. The second time the same element has the class "show," it should open wor ...

Is there a way to make my modal appear only when the "New" option is clicked?

Is there a way to make my modal in VueJS open only when I click on the "New" option? <select v-model="input.des" @change="$refs.modalName.openModal()"> <option value="A">A</opt ...

Retrieve the origin of the copied text

While working on a Vue application, I'm curious to know if it's possible to access the source of pasted text. For example, whether the pasted text originated from your own application, Word, or Notepad? I attempted the code below but was unable ...

Ways to eliminate brackets from a string

Currently, I am working on a challenge involving replacing strings using a function that accepts a string and an object of values. This task involves a two-part algorithm: Replacing values within the string that are enclosed in braces. If the value is wi ...

The content on Twitter Bootstrap remains consistent

While using Twitter Bootstrap modals for updating contacts, I encountered some issues. The modal consistently displays the information of the first contact, whereas when displaying the information outside the modal, everything appears correctly. <?php ...

React Router updates the URL path without actually rendering the content of the second page

I'm having an issue with my React app where the address changes correctly in the search bar, but the page is not loading. Here is my code: import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; import App from "./App"; i ...

Issue with function execution within useEffect() not being triggered

I am facing an issue where two functions in my component are not being called every time it renders, despite my efforts. Placing these functions in the dependency array causes an infinite loop. Can anyone identify why they are not executing? function Por ...

My div is currently being concealed by a jQuery script that is hiding all of its

Below is the current code snippet: jQuery(document).ready(function($) { $("ul.accordion-section-content li[id*='layers-builder'] button.add-new-widget").click(function() { $("#available-widgets-list div:not([id*='layers-widget']) ...

Building a custom React carousel using visibility logic starting from the ground up

Can anyone explain the logic or algorithm behind a Carousel? I have been researching how to display one item at a time, but in my case, I need to display three items. When I click on next, I want the first item to hide and the fourth item to appear. <di ...

Tips for accessing a variable value within a JavaScript function

I am currently facing an issue where I am unable to retrieve a variable from a JavaScript function and use it outside of the function. While I can successfully output the variable value inside the function, I am struggling to access it elsewhere in my sc ...

The React Component is not displaying any content on the webpage

I developed a Reactjs application using the create-react-app. However, I am facing an issue where the components are not displaying on the page. I suspect that App.js is failing to render the components properly. Take a look at my code: App.js import R ...

Tips on how to increase and update the index value by 2 within an ngFor loop while maintaining a fixed format

I have a specific template layout that displays only two items in each row as shown below. I want to use the ngFor directive to iterate through them: <div class="row" *ngFor="let item of cityCodes; let i = index"> <div class="col-6" (click)= ...

Remove the model from operation

I'm fairly new to angularjs and have a working service. However, I want to move the patient model out of the service and into a separate javascript file. I believe I need to use a factory or service for this task, but I'm not entirely sure how to ...

Collecting data from an HTML form filled out by users

I need a way to capture user input from an HTML form and display it later on my website. I want to show all the information users provide along with their pizza selections. I've been struggling for hours trying to make this work without success :( Spe ...

jQuery code unable to alter the class of a div

Need help with a script on my single page site that adds a "read more" style link to a content area's div. The goal is for the button to change the class of the content area when clicked, showing all of the content. Clicking again should hide the cont ...

Swapping items within Angular's forEach()

I am working with an array of objects and using a forEach() function in my code. Here is the structure: $scope.things = [ {x: 2}, {x: 4}, {x: 5} ]; angular.forEach($scope.things, function(thing) { //You can uncomment one line at a time ...

Migration processes encountering delays due to running nodes

When running migrations in Node, I am encountering a timeout error. The specific error message is: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. Here is the ...

Oops! Looks like node.js is throwing an error with the message "connect ECONNREFUSED"

After creating a node.js application that interacts with a locally installed MySQL database, I encountered an issue where running the app through CMD (npm start) returned the desired results, but testing it through Postman resulted in an error: connect E ...

Incorporating Information into an Array as a State Object within React.js

Hey everyone, I've hit a bit of a roadblock and I could really use some assistance. I'm trying to figure out why I keep getting an error when attempting to push user input into my array. Can someone take a look at my code and offer some insight? ...

Conceal a script-generated div using CSS styling

Using the code below, you can create an HTML chatbox with a link in the top panel and multiple child divs. The structure is as follows: cgroup is the parent of CBG, which is the parent of CGW, and CGW is the parent of the div I want to hide. How can I u ...