Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Is there a way to know or calculate only the Attachments size before or after sending the email using Nodemailer?

In our application we are attaching PDF, at some random scenario the attachment is getting corrupted. So we decided to check if the attachment size is same as before and after the attachment is attached, and validate for the same.

I couldn't find any information regarding how the messageSize is calculated or a way to know the attachment size from the callback of transporter.sendMail().

We observed the changes in size by manipulating the attributes does not have any repeatable patter that we can take advantage off.

var mailOptions = {
  from: "<some email>",
  to: "<some email>",
  subject: "S",
  text: "",
  attachments: [
    {
      filename: "a.pdf",
      path: "./sample.pdf",
    },
  ],
};

const getFileSize = () => {
  const fileSize = fs.statSync(mailOptions.attachments[0].path);
  return fileSize.size;
};

const calTotalSize = () => {
  let total = 165;

  if (mailOptions?.from?.length > 0) {
    total = total + mailOptions.from.length;
  }
  if (mailOptions?.to?.length > 0) {
    total = total + mailOptions.to.length;
  }
  if (mailOptions?.subject?.length > 0) {
    total = total + mailOptions.subject.length + 11;
  }
  if (mailOptions?.text?.length > 0) {
    total = total + mailOptions.text.length + 50;
  }
  if (mailOptions?.attachments?.length > 0) {
    if (mailOptions.attachments?.filename?.length > 0) {
      total = total + mailOptions.attachments[0].filename.length;
    }
    if (mailOptions.attachments?.path?.length > 0) {
      total = total + mailOptions.attachments[0].path.length;
    }
    total = total + getFileSize();
  }

  return total;
};

transporter.sendMail(mailOptions, function (error, info) {
  if (error) {
    console.log(error);
  } else {
    console.log(getFileSize(), "calculate original pdf size");
    console.log(calTotalSize(), "calculate total size");
    console.log(info.messageSize, "Email size");
  }
});

Thank you.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!