Search code examples
node.jsjspdfnodemailer

Can i attach an object to an email with nodemailer?


I need to send a email with nodemailer, but in the email i need to attach an pdf that i generate using jspdf, the thing is that i cannot attach an object to an email, i can attach a file getting it's path, a string, and a lot of other things, but an object i cannot.

I tought of saving the pdf and using it's path, but this is all working on an VM, so i dont want to use too much cpu power or ram space.

I also tried using JSON.stringify() in the pdf, but it didn't work, and the file attached to the email was empty.


Solution

  • You can attach your pdf file by using content property of attachments object. It support many formats - string, path to file, buffer, fs read stream, etc. See this docs.

    In case with jspdf you can use output() method

    const message = {
        // ...
        attachments: [
           {
               filename: "mypdf.pdf",
               content: pdfObject.output('arraybuffer')
           }
        ]
    };