gpt4 book ai didi

node.js - 来自 : field is always sending mail from the email used in the smtpTrans auth object 的 nodemailer 邮件选项

转载 作者:行者123 更新时间:2023-12-02 21:02:46 24 4
gpt4 key购买 nike

我正在使用nodemailer来处理联系表格和邮寄东西。

但是当我尝试将 req.body.email 设置为 from: email 时,它只使用身份验证电子邮件。因此,我收到的所有电子邮件均来自:me@me.com 至:me@me.com

而不是从:customer@them.com 到:me@me.com

我很确定我做得对

var mailOpts, smtpTrans;

//Setup Nodemailer transport, I chose gmail. Create an routerlication-specific password to avoid problems.
smtpTrans = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "me@me.com",
pass: "hey"
}
});
//Mail options
console.log(req.body.email);
mailOpts = {
from: req.body.email, //grab form data from the request body object
to: 'me@me.com',
subject: 'Stockist interest form',
text: "Welcome to the My Leisure Stockists application process, we'd love to have you on board"+"\n Email: "+req.body.email
};

smtpTrans.sendMail(mailOpts, function (error, response) {

if (error) {
res.sendStatus(500);
} else {
res.sendStatus(200);
};

});

最佳答案

首先你需要像 bodyparser 这样的东西。 2019 年您可以使用:

app.use(express.json());
app.use(express.urlencoded({extended: true}));

显然首先使用 express :

const express = require('express');
const app = express();

另一件事是这是一个文本字段,因此您将其放入字符串中。与 es6 表示法一样,您可以使用:

`${req.body.email}`

所以你会得到这样的结果:

const express = require('express');
const app = express();

const nodemailer = require('nodemailer')

app.use(express.json());
app.use(express.urlencoded({extended: true}));

const smtpTrans = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "me@me.com",
pass: "hey"
}
});

app.post('/send-email', function (req, res) {
const mailOptions = {
from: `${req.body.email}`, // Sender address
to: 'me@me.com', // List of recipients
subject: 'Stockist interest form',
text: "Welcome to the My Leisure Stockists application process, we'd love to have you on board"+"\n Email: "+req.body.email
};

smtpTrans.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});

res.redirect("/index.html");
})

关于node.js - 来自 : field is always sending mail from the email used in the smtpTrans auth object 的 nodemailer 邮件选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43511061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com