There is a Node.js script (app.js) that sends an email:
const { response } = require("express");
const express = require("express");
const nodemailer = require("nodemailer");
const app = express();
const port = 5000;
//
function sendEmail(tel) {
return new Promise((resolve, reject) => {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: ,
pass: ,
},
});
const mail_configs = {
from: "myEmail",
to: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43223137262e273527032e222a2e5c3136">[email protected]</a>",
subject: "Testing Koding 101 Email",
text: "tel",
};
transporter.sendMail(mail_configs, function (error, info) {
if (error) {
console.log(error);
return reject({ message: "An error has occurred" });
}
return resolve({ message: "Email sent successfully" });
});
});
}
app.get("/", (req, res) => {
sendEmail()
.then((response) => res.send(response.message))
.catch((error) => res.status(500).send(error.message));
});
app.listen(port, () => {
console.log(`nodemailerProject is listening at http://localhost:${port}`);
});
There is a button in another JS file which runs this JS script and sends an email when the button is clicked:
let input = document.getElementById("phonenumber");
head.addEventListener("click", function () {
fetch("http://localhost:5000/")
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
});
There is an input field for typing a message in the HTML file
<input id="phonenumber" class="text-order" type="text"
placeholder="___________"'/>
How can we send the value entered in this input as an email message when a button is pressed?