【问题标题】:No recipients defined error even though console.log shows my entered mail address即使 console.log 显示我输入的邮件地址,也没有收件人定义错误
【发布时间】:2021-11-18 23:48:17
【问题描述】:

我遵循了来自开发者https://dev.to/jlong4223/how-to-implement-email-functionality-with-node-js-react-js-nodemailer-and-oauth2-2h7m的教程

我的问题是,即使我输入了用户电子邮件,并且 console.log 在服务器代码和电子邮件输入代码中都显示了正确的电子邮件。 我仍然收到错误:未定义收件人。 我已将 mailOptions 中的“to”参数更改为“mymail@gmail.com”,效果很好。

这是我的代码;

服务器代码

transporter.verify((err, success) => {
  err
    ? console.log(err)
    : console.log(`=== Server is ready to take messages: ${success} ===`);
 });
 
 
 app.post("/send", function (req, res) {
   console.log(req.body.email); //shows entered mail
  let mailOptions = {
    from: "some@mail.com",
    to: '${req.body.email}',
    subject: "Message from: ",
    text: "Reset password",
  };
 
  transporter.sendMail(mailOptions, function (err, data) {
    if (err) {
      console.log(err);
      res.json({
        status: "fail",
      });
    } else {
      console.log("== Message Sent ==");
      res.json({
        status: "success",
      });
    }
  });

 });

提交邮件的代码

const [email, setEmail] = useState("");

async function handleReset(e) {
  e.preventDefault();
  console.log(email);
   const response = await fetch("http://localhost:2525/send", {
     mode: "cors",
     method: "POST",
     headers: {
       "Content-type": "application/json",
      },
     body: JSON.stringify({ email }),
   })
   .then((res) => res.json())
   .then(async (res) => {
     const resData = await res;
     if (resData.status === "success") {
       alert("Message Sent");
      
      } else if (resData.status === "fail") {
       alert("Message failed to send");
       
     }
   });
}
Error: No recipients defined
    at SMTPConnection._formatError
    at SMTPConnection._setEnvelope 
    at SMTPConnection.send 
    at sendMessage 
    SMTPConnection._actionAUTHComplete 
    at SMTPConnection.<anonymous> 
    at SMTPConnection._processResponse 
    at SMTPConnection._onData 
    at TLSSocket.SMTPConnection._onSocketData
  code: 'EENVELOPE',
  command: 'API'

非常感谢任何意见!

【问题讨论】:

    标签: node.js reactjs express nodemailer


    【解决方案1】:

    您应该在app.post("/send") 回调函数中使用带有to 参数的反引号:

    to: `${req.body.email}`
    

    【讨论】:

    • 非常感谢!!终于成功了
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2022-10-15
    • 2019-12-12
    • 2023-03-19
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多