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