Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda