有一个节点 JS 脚本(app.js),用于向邮件发送一封信:
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 tranporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: ,
pass: ,
},
});
const mail_configs = {
from: "myEmail",
to: "artemdvd@mail.ru",
subject: "Testing Koding 101 Email",
text: "tel",
};
tranporter.sendMail(mail_configs, function (error, info) {
if (error) {
console.log(error);
return reject({ message: "An error has occured" });
}
return resolve({ message: "Email sent succesfuly" });
});
});
}
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}`);
});
其他js文件中有一个按钮,它运行这个js脚本并在我按下按钮时发送电子邮件:
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));
});
html 文件中有一个用于文本消息的输入字段
如何在按下按钮时通过电子邮件发送此输入值?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号