[英]Nodemailer keeps giving me an error: "TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or..."
The full error is EXTREMELY long, but here is a snippet:完整的错误非常长,但这里有一个片段:
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'
}
This is my first time using nodemailer, or any emailing service for that matter.这是我第一次使用 nodemailer,或者任何电子邮件服务。 I originally started using it with my personal gmail account, then was getting the issue where gmail was blocking it because it was considered a less secure app.我最初是通过我的个人 gmail 帐户开始使用它,然后遇到了 gmail 阻止它的问题,因为它被认为是一个不太安全的应用程序。 So I changed my email settings to allow access from less secure apps.所以我更改了我的 email 设置,以允许从不太安全的应用程序访问。 And then I began getting this error.然后我开始收到这个错误。
I also tried using https://ethereal.email/ to autogenerate an email test account so that I could avoid using gmail, just in case that was the issue.我还尝试使用https://ethereal.email/自动生成 email 测试帐户,这样我就可以避免使用 gmail,以防万一出现问题。
Here is my nodemailer code:这是我的节点邮件程序代码:
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;
Does anybody have any idea what the error means?有人知道这个错误是什么意思吗? Let me know if you need any more info.如果您需要更多信息,请告诉我。
In my case, i was a trying to send a wrong data type, so i convert the property html to string and was solved.就我而言,我试图发送错误的数据类型,所以我将属性 html 转换为字符串并得到解决。 How i was using next.js, the compiler has reading my html like a JSX element.我是如何使用 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>`,
}
i think the problem is the return.我认为问题是回报。 you must JSON.stringify(error) and get error from sendMail!您必须 JSON.stringify(error) 并从 sendMail 中获取错误!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.