0

I have a protected url that serves the pdf file and i want to send this pdf file to mail.

This is how I am using the protected url to get binary data

return axios.get(url, {
      headers: {
        "x-api-key": process.env.KEY,
        "Content-Type": "application/json",
        "Accept": "application/pdf"
      }
    }).then((response) => response.data);

and this is how m sending mail

const response = await this.myService.getPdf();
return this.mailService.sendPdf(response);

mail service

async sendPdf(blob: any): Promise<any> {
    return this.sendMail({
      to: "myid@gmail.com",
      subject: "pdf attacgement",
      body: `Hello`,
      attachments: [
        {
          filename: "thunder-file.pdf",
          content: Buffer.from(blob, 'binary'),
        }
      ]
    });

  }

and sendMail function

async sendMail(mail: { subject: string, to: string, body: string, attachments?: any[] }): Promise<any> {
    const mailOptions = {
      from: process.env.MAIL_FROM,
      to: mail.to,
      subject: mail.subject,
      html: mail.body
    };

    if (mail.attachments && mail.attachments.length) {
      mailOptions["attachments"] = mail.attachments;
    }

    return this.transporter.sendMail(mailOptions);
  }

I am receiving mail with attachment but it shows the black page in the pdf. Actual file size if I download directly is 139KB but in mail it shows only 99KB.

Am i missing something? I guess data is truncated in between. Any solution ?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

0 Answers0