Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
JS send pdf file with Microsoft Graph

I am trying to send an email with a pdf file attached via Microsoft graph api.

Without an attached file the following code works perfectly:

On the client side

await axios.get(
    '/extApi/sp/sendMail', {
    params: {
        userId: 'me',
        sendMail: {
            message: {
                subject: 'Meet for lunch?',
                body: {
                    contentType: 'Text',
                    content: 'The new cafeteria is open.'
                },
                toRecipients: [
                    {
                        emailAddress: {
                            address: 'myadress@gmail.com'
                        }
                    }
                ],
            },
            saveToSentItems: 'false',
        }
    }
});

Server side

async function sendMail(req, res) {
    const options = {
        authProvider,
     };

    const client = Client.init(options);
    const sendMail = req.query.sendMail
    await client.api('/me/sendMail')
        .post(sendMail);
}

Now I would like to attach a pdf file. So I tried without success (same server side code):

Client side

let selectedFile = document.getElementById('file').files[0];
function getBase64(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });
}
var encoded = await getBase64(selectedFile)
console.log(encoded)
await axios.get(
    '/extApi/sp/sendMail', {
    params: {
        userId: 'me',
        sendMail: {
            message: {
                subject: 'Meet for lunch?',
                body: {
                    contentType: 'Text',
                    content: 'The new cafeteria is open.'
                },
                attachments: [{
                    "@odata.type": "#microsoft.graph.fileAttachment",
                    "name": "attachment.pdf",
                    "contentType": "application/pdf",
                    "contentBytes": encoded
                }],
                toRecipients: [
                    {
                        emailAddress: {
                            address: 'myadress@gmail.com'
                        }
                    }
                ],
            },
            saveToSentItems: 'false',
        }
    }
});

The error here is "net::ERR_CONNECTION_RESET 431 (Request Header Fields Too Large)" due to the "encoded" variable being very long when sending on the server side. How else can I make it work?

Thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!