簡體   English   中英

Nodemailer 一直給我一個錯誤:“TypeError [ERR_INVALID_ARG_TYPE]:“chunk”參數必須是字符串類型或......”

[英]Nodemailer keeps giving me an error: "TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or..."

完整的錯誤非常長,但這里有一個片段:

    TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object
    at PassThrough.Writable.write (_stream_writable.js:289:13)
    at PassThrough.Writable.end (_stream_writable.js:583:10)
    at Immediate._onImmediate (C:\Users\small\desktop\career-change\coding-round-2\portfolio-round1\recipe-app\projectv2\node_modules\nodemailer\lib
\mime-node\index.js:969:46)
    at processImmediate (internal/timers.js:456:21) {
  code: 'ERR_INVALID_ARG_TYPE'
}

這是我第一次使用 nodemailer,或者任何電子郵件服務。 我最初是通過我的個人 gmail 帳戶開始使用它,然后遇到了 gmail 阻止它的問題,因為它被認為是一個不太安全的應用程序。 所以我更改了我的 email 設置,以允許從不太安全的應用程序訪問。 然后我開始收到這個錯誤。

我還嘗試使用https://ethereal.email/自動生成 email 測試帳戶,這樣我就可以避免使用 gmail,以防萬一出現問題。

這是我的節點郵件程序代碼:

const express = require("express");
const router = express.Router();
const bodyParser = require("body-parser");
const nodemailer = require("nodemailer");

router.post("/contact", (req, res) => {
  const output = {
    msg: `You have a new contact request from ${req.body.name}`,
    userEmail: req.body.userEmail,
    subject: req.body.subject,
    message: req.body.message,
  };
  console.log(res);

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    port: 587,
    auth: {
      user: "ida.pollich73@ethereal.email",
      pass: "JPtPvputy6n8JBzYe2",
    },
  });

  let mailOptions = {
    from: '"Nodemailer Contact" <mypersonalaccount@gmail.com>', // sender address
    to: "differentpersonalaccount@gmail.com", // list of receivers
    subject: "Node Contact Request", // Subject line
    text: output, // plain text body
    html: output, // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      return console.log(error);
    } else {
      console.log(info);
    }
    res.send("email sent");
    console.log("Message sent: %s", info.messageId);
    // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

    // Preview only available when sending through an Ethereal account
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
  });
});

module.exports = router;

有人知道這個錯誤是什么意思嗎? 如果您需要更多信息,請告訴我。

就我而言,我試圖發送錯誤的數據類型,所以我將屬性 html 轉換為字符串並得到解決。 我是如何使用 next.js 的,編譯器像 JSX 元素一樣讀取了我的 html。

       const mailOptions= {
            from: email,
            to: 'mail@gmail.com',
            subject: `FORMULARIO DE: ${req.body.name}`,
            text: req.body.message,
            html: `<div>${req.body.message}</div>`,
        }

我認為問題是回報。 您必須 JSON.stringify(error) 並從 sendMail 中獲取錯誤!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM