Search code examples
javascriptnode.jsexpressemailnodemailer

Nodemailer not sending email, displays "Message sent:Undefined"


It was working and then it wasn't. I sent a few mails and after a while it stopped working. I get "Message sent:Undefined" (node:9048) UnhandledPromiseRejectionWarning: Error: spawn sendmail ENOENT at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)

I have no idea why.

Frontend- Axios console logs the response data therefore the server and the frontend both are working. It is just the issue with the Nodemailer.

Any help is appreciated. Thanks!

const express = require("express");
const app = express();
const cors = require("cors");
const nodemailer = require("nodemailer");
const path = require("path");
const fs = require("fs");
const readData = path.join(__dirname, "../email_take2/data.json");
const { v4: uuidv4 } = require("uuid");


app.use(express.json());
app.use(cors());

const port = process.env.PORT || 5000;
app.listen(5000, () => console.log(`Listening at port ${port}`));
if (process.env.NODE_ENV === "production") {
  // Set static folder
  app.use(express.static("../client/build"));

  app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "../client", "build", "index.html"));
  });
}
function listUserData() {
  const data = fs.readFileSync(readData);
  return JSON.parse(data);
}

app.post("/sendEmail", function (req, res) {
  console.log(req.body);
  const emailInfo = {
    id: uuidv4(),
    email: req.body.email,
    cc: req.body.cc,
    message:req.body.message,
  };
  const dataArray = listUserData();
  dataArray.push(emailInfo);
  fs.writeFileSync(readData, JSON.stringify(dataArray));
  res.send(emailInfo);
  console.log("SentBody", emailInfo);

  let transporter = nodemailer.createTransport({
    sendmail:true,
    host: "smtp.outlook.com",
    port: 587,
    secure: false, // true for 465, false for other ports
    tls: {
        ciphers: "SSLv3",
           },
    auth: {
      user: "memail@outlook.com", // generated ethereal user
     pass: "mypw", // generated ethereal passwordAccount.pass, 
    },
  });


  // send mail with defined transport object
  let info = transporter.sendMail({
    from: '"Fred Foo 👻" <foo@example.com>', // sender address
    to: "garbageacc7878@outlook.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>", // html body
  });
  console.log("Message sent: %s", info.messageId);
  return emailInfo;
});

Solution

  • transporter.sendMail returns a promise, that's why your console log has undefined. So either attach a .then and .catch.

    transporter.sendMail(...)
    .then((data)=>{console.log('Mail sent', data)})
    .catch(err => {console.error('Failure',err)})
    

    Or make your request handler an async function and use async await and use tryCatch.

    try{
    let data = await transporter.sendMail(...)
    console.log('Mail sent', data)
    } catch (err) {
    console.error('Failure',err)
    }