【问题标题】:nodemailer mail options from: field is always sending mail from the email used in the smtpTrans auth objectnodemailer 邮件选项来自:字段始终从 smtpTrans auth 对象中使用的电子邮件发送邮件
【发布时间】:2017-09-16 14:06:59
【问题描述】:

我正在使用 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);
      };

  });

【问题讨论】:

  • 我遇到了同样的问题。你有想过吗?还是这是预期的行为?
  • @MichaelLynch 不,实际上我仍在使用在 text: 属性中包含“电子邮件:”+req.body.email 的解决方案,以便我们可以通过电子邮件。我不认为这是预期的行为,因为我发誓,当我第一次安装 nodemailer 时,它的设置是从:电子邮件地址到 req.body.email 不再,我们应该提交错误报告
  • 你现在明白了吗?我也有同样的问题。
  • 即使我也有这个问题
  • 我认为他们修复了它...一些方法,它现在可以工作了吗?

标签: node.js email smtp nodemailer


【解决方案1】:

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

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

显然首先使用快递:

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");
})

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2022-07-27
    • 2019-09-30
    相关资源
    最近更新 更多