Search code examples
node.jsexpressmulternodemailer

send attachement nodemailer wihout storing file in server


I am looking for a way to send attachements using nodemailer ,multer ,express without having to store the files in a server .

This my actual sendEmai fucntion , i want to update it so i can send an attachement

exports.sendMail = (req, res) => {
 let credentials=await Integration_mail.findOne({
    where: {
      userId: req.body.userId,
    },
  })
    
      //decrypt password
      var bytes = CryptoJS.AES.decrypt(
        credentials.password,
        secretconfig.secret
      );
      var originalPwd = bytes.toString(CryptoJS.enc.Utf8);
  let transporter = nodemailer.createTransport({
    host: credentials.host_smtp,
    port: 587,
    secure: false, // upgrade later with STARTTLS
    auth: {
      user: credentials.user,
      pass: originalPwd,
    },
  });

  const { destMail, subject, text } = req.body;

  var mailOptions = {
    from: credentials.user,
    to: destMail,
    subject: subject,
    text: text,
  };
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      return res.status(500).json({ msg: error });
    } else {
      return res.status(200).json({ msg: 'email sent' });
    }
  });
})

});
};

Thank you for the help


Solution

  • So after a period of time and research, I used form data to upload the file and using only the buffer of the file I send it via nodemailer without having to upload it into the server and using its path. Here's the code:

      let transporter = nodemailer.createTransport({
      host: host_smtp,
      secureConnection: false, // TLS requires secureConnection to be false
      port: 587, // port for secure SMTP
      tls: {
        ciphers: "SSLv3",
      },
      auth: {
        user: credentials.email,
        pass: credentials.password,
      },
    });
    
    const { to, subject, text, cc, bcc } = req.body;
    let attachments = [];
    if (req.files) {
        let files = req.files.file;
    //if its an array of files I preferred to not allow uploads > 20Mb
      if (Array.isArray(files)) {
        size = files.reduce((acc, file) => acc + file.size, 0);
        if (size > 20971520)
          return res.status(500).json({ message: "files size cannot be greater than 20MB" });
    
        attachments = files.map((file) => ({
          filename: file.name,
          content: file.data,
          encoding: file.encoding,
        }));
      } else {
        if (files.size > 20971520)
          return res.status(500).json({ message: "files size cannot be greater than 20MB" });
        attachments = [
          {
            filename: files.name,
            content: files.data,
            encoding: files.encoding,
          },
        ];
      }
    }
    const mailOptions = {
      from: credentials.email,
      to: to,
      cc: cc,
      bcc: bcc,
      subject: subject,
      html: text,
      attachments: attachments,
    };
    await transporter.sendMail(mailOptions);
    

    and here's a screenshot o the body of the request using form data

    Request body

    and here's a reference to the official documentation: https://nodemailer.com/message/attachments/