Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda