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

Viewed 1705

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. 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. So I changed my email settings to allow access from less secure apps. 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.

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.

2 Answers

In my case, i was a trying to send a wrong data type, so i convert the property html to string and was solved. How i was using next.js, the compiler has reading my html like a JSX element.

       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!

Related