-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
43 lines (33 loc) · 921 Bytes
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const graph = require("@microsoft/microsoft-graph-client");
require("isomorphic-fetch");
const sendEmail = async (responseToken, mail) => {
try {
const userEmail = mail.from.emailAddress.address;
const client = getAuthenticatedClient(responseToken);
await client.api(`/users/${userEmail}/sendMail`).post({ message: mail });
console.log('Email was sent successfully', mail.toRecipients)
return 'sent';
} catch (error) {
console.log('Could not send mail', error);
}
};
function getAuthenticatedClient(token) {
if (!token) {
throw new Error(`Invalid MSAL state`);
}
// Initialize Graph client
const client = graph.Client.init({
authProvider: async (done) => {
try {
return done(null, token.accessToken);
} catch (err) {
console.log(JSON.stringify(err, Object.getOwnPropertyNames(err)));
done(err, null);
}
},
});
return client;
}
module.exports = {
sendEmail,
};