Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda