使用 nodemailer 发送邮件,并将表单数据插入 mysql db
Send email with nodemailer, and insert form data into mysql db
尝试设置一个表单来接收用户输入的 Beta 测试电子邮件列表,这将向我发送一封包含用户信息的电子邮件,并将相同的表单数据存储到 mysql 数据库中。
我在与 nodemailer 对象相同的路由内有 mysql 连接和查询,但是当提交表单时我收到一条错误消息 "admin is not defined"。这是 transporter 对象中的管理员。我认为如果两个不同的对象(传输器和连接)具有相同的 属性 名称可能会导致问题,所以我将传输器内部的常规 nodemailer 'user' 更改为 'admin'.
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
const mysql = require("mysql");
app.use(bodyParser.urlencoded({extended: true}));
// send "Beta" email when user signs up to the email list using the "Beta" form
app.post("/register", function(req, res){
// connect to database
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "PASSWORD",
database: "DATABASE_NAME"
});
// insert statement
let insert = "INSERT INTO Beta_Testers(First_Name, Last_Name, Beta_Email) VALUES ('" + req.body.First_Name + "', '" + req.body.Last_Name + "', '" + req.body.Beta_Email + "')";
// execute the insert statement
connection.query(insert);
// disconnect from database
connection.end();
// nodemailer objects
let mailOpts, transporter;
// email transporter
transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 3000, // changed from 465
secure: true,
auth: {
admin: "GMAIL_USER",
pass: "PASSWORD"
}
});
// email credentials
mailOpts = {
from: req.body.First_Name + " " + req.body.Last_Name + " <" + req.body.Beta_Email + ">",
to: admin,
subject: "You have a new BETA tester!",
text: `${req.body.Beta_Email} has signed up to be a BETA tester for WEBAPP_NAME. Please confirm sucessful registration into DATABASE_NAME.`
};
// send email and verify contact
transporter.sendMail(mailOpts, function(err, res) {
if (err) {
res.render("contact-failure");
} else {
res.render("contact-success");
}
});
});
ReferenceError: admin is not defined
错误在:"to: admin"
它应该包含一封电子邮件,电子邮件将被发送到该电子邮件地址。这些是 mail_options 选项 "to" 需要使用 "abc@xyz.com".
之类的电子邮件进行设置
您可以设置:-
至:req.body.email
尝试设置一个表单来接收用户输入的 Beta 测试电子邮件列表,这将向我发送一封包含用户信息的电子邮件,并将相同的表单数据存储到 mysql 数据库中。
我在与 nodemailer 对象相同的路由内有 mysql 连接和查询,但是当提交表单时我收到一条错误消息 "admin is not defined"。这是 transporter 对象中的管理员。我认为如果两个不同的对象(传输器和连接)具有相同的 属性 名称可能会导致问题,所以我将传输器内部的常规 nodemailer 'user' 更改为 'admin'.
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
const mysql = require("mysql");
app.use(bodyParser.urlencoded({extended: true}));
// send "Beta" email when user signs up to the email list using the "Beta" form
app.post("/register", function(req, res){
// connect to database
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "PASSWORD",
database: "DATABASE_NAME"
});
// insert statement
let insert = "INSERT INTO Beta_Testers(First_Name, Last_Name, Beta_Email) VALUES ('" + req.body.First_Name + "', '" + req.body.Last_Name + "', '" + req.body.Beta_Email + "')";
// execute the insert statement
connection.query(insert);
// disconnect from database
connection.end();
// nodemailer objects
let mailOpts, transporter;
// email transporter
transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 3000, // changed from 465
secure: true,
auth: {
admin: "GMAIL_USER",
pass: "PASSWORD"
}
});
// email credentials
mailOpts = {
from: req.body.First_Name + " " + req.body.Last_Name + " <" + req.body.Beta_Email + ">",
to: admin,
subject: "You have a new BETA tester!",
text: `${req.body.Beta_Email} has signed up to be a BETA tester for WEBAPP_NAME. Please confirm sucessful registration into DATABASE_NAME.`
};
// send email and verify contact
transporter.sendMail(mailOpts, function(err, res) {
if (err) {
res.render("contact-failure");
} else {
res.render("contact-success");
}
});
});
ReferenceError: admin is not defined
错误在:"to: admin"
它应该包含一封电子邮件,电子邮件将被发送到该电子邮件地址。这些是 mail_options 选项 "to" 需要使用 "abc@xyz.com".
之类的电子邮件进行设置您可以设置:-
至:req.body.email