Search code examples
node.jspdfattachmentnodemailerpdfkit

Sending base64 encoded PDF through Nodemailer using PDFkit


I have a function that returns me a base64 encode PDF, and I would like to send this as an attachement PDF file using nodemailer.

Regarding the nodemailer documentation, I found this example:

const mailOptions = {
    from: 'email1@gmail.com', // sender address
    to: 'email2@gmail.com', // list of receivers
    subject: 'Simulation', // Subject line
    html: '<p>SALUT</p>', // plain text body
    filename: 'file.pdf',
    attachments: [
          content: Buffer.from(
                'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
                    '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
                    'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
                'base64'
            ),

            cid: 'note@example.com' // should be as unique as possible
        },

However, this did not work for me. Am i missing something ?


Solution

  • Ok, it was all a formatting issue.

    Here is how to use B64 on Nodemailer:

      const mailOptions = {
        from: 'email1@gmail.com', // sender address
        to: 'email2@gmail.com', // list of receivers
        subject: "Hey this is a subject example", //subject
        attachments: [
          {
            filename: `myfile.pdf`,
            content: 'THISISAB64STRING',
            encoding: 'base64',
          },
        ],
      };
    

    then just send it the classic way.