Home > Mobile >  Unable to send pdf attachment with more than 2 photos using sendgrid
Unable to send pdf attachment with more than 2 photos using sendgrid

Time:03-27


I have one weird problem. I created API, that allow you to: Upload photos to server, then save data to database, generate pdf with these datas and photos and then send email with that pdf attachment. It was working well for me, while I was uploading/sending only two photos. I was able to open that generated pdf file using finder, also I was able to open it from received email.
Then I uploaded more files and after sending API request, I received email with that pdf file but I was not able to open it. It shows error that finder was not able to open that file. When I checked manually inside server folder, it was working well.

So there must be problem while sending that email. 0 to 2 photos works well, more than 2 is not working. But when I open that pdf file on server, it is working well even with 8 photos.

Sending picture of that pdf attachment.

pdf attachment

Here is code, that I am using to generate pdf with pictures:

const photosToPdf = [];
let positionX = 30;
let positionY = 240;

 for(let j=0;j<uploadsPath.length;j  ){
    photosToPdf.push(uploadsFileNames[j]);
 }
    
 photosToPdf.forEach(img => {
    if(positionX == 570){
       positionX = 30
       positionY = positionY   160 
    }
    doc.image(`uploads/${img}`, positionX, positionY, {width: 80});
    positionX = positionX   90
});

And here is code that generate email:

function highPriorityEmail(){
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let filePath = path.join(__dirname, `../../${problemName}_${creationDate}.pdf`);
      let attachment = fs.readFileSync(filePath).toString("base64");
      let fileName = `${problemName}_${creationDate}.pdf`

      if (problemPriority == "vysoká") {
        global.msg = {
          //to: ['[email protected]', '[email protected]'], // Change to your recipient
          //to: "[email protected]", // Change to your recipient
          to: "[email protected]", // Change to your recipient
          from: "[email protected]", // Change to your verified sender
          subject: "Vytvorenie nového nedostatku: "   problemName,
          html: `
                        Dobrý deň, <br>
                        <br>
                        dňa ${creationDate} bol vykonaný dozor koordinátora ${koordinatorName}. <br> 
                        Boli zistené nedostatky s vysokou prioritou, ktoré sú priložené v prílohe tohoto emailu.
                        <br>
                    `,
          attachments: [
              {
                  content: attachment,
                  filename: fileName,
                  contentType:'application/pdf'
              },
          ],
        };
        

        // sgMail.sendMultiple(msg) to prevent seeing
        sgMail
          .send(msg)
          .then(() => {
            console.log("Email sent");
          })
          .catch((error) => {
            console.error(error);
          });

        Case.update(
          { wasSended: 1 },
          { returning: true, where: { caseName: problemName } }
        );
      }
      resolve();
    }, 1000)
  });
};

I am using:
pdfkit-table
and sendgrid/mail

Thanks for help

CodePudding user response:

Ok, I think we got to an answer here.

The issue is in the line

        global.msg = {

Setting a variable that is local to the function as a global variable makes it possible that other code can change this data while it is being used. Change this to a const instead and you avoid that issue:

        const msg = {

One other thing to point out, but the attachments object content type property should be type not contentType (though in my testing that didn't change anything).

          attachments: [
              {
                  content: attachment,
                  filename: fileName,
                  type:'application/pdf'
              },
          ],
  • Related