[英]Send E-mail on submitting form to Firestore
I am making a website to take bookings using a html form and submit the form to firestore, also as soon as the data is submitted an confirmation mail is send to the customer.我正在制作一个网站来使用 html 表单进行预订并将表单提交给 firestore,并且一旦提交数据,就会向客户发送确认邮件。 I am using the code given below to achieve it.我正在使用下面给出的代码来实现它。
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: "trippyadive.web.app@gmail.com",
Password: "xxxxxxxxxxxxxxx",
To: "subhodiproy161101@gmail.com",
From: "trippyadive.web.app@gmail.com",
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 working fine but the email id
of the To:
is constant, I would like to change the To:
field automatically based on the email id given in the form, How can I do So?一切正常,但To:
的email id
是不变的,我想根据表单中给出的电子邮件 ID 自动更改To:
字段,我该怎么做?
Thanks for any help in advance.提前感谢您的任何帮助。
Just add a new argument of your sendMail
function the email
and pass it to To:
:只需在email
添加sendMail
函数的新参数并将其传递给To:
:
function sendEmail(email, packageFields, name, date, persons) {
Email.send({
Host: "smtp.gmail.com",
Username: "trippyadive.web.app@gmail.com",
Password: "xxxxxxxxxxxxxxx",
To: email,
From: "trippyadive.web.app@gmail.com",
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");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.