-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
43 lines (37 loc) · 1.22 KB
/
index.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 { google } = require('googleapis');
const MailComposer = require('nodemailer/lib/mail-composer');
const { removeUndefined } = require('@strapi/utils');
function toWebsafeBase64(s) {
return s.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
async function createMail(options) {
const mailComposer = new MailComposer(options);
const message = await mailComposer.compile().build();
return toWebsafeBase64(message.toString('base64'));
}
async function sendMail(providerOptions, settings, options) {
const auth = new google.auth.OAuth2(
providerOptions.auth.clientId,
providerOptions.auth.clientSecret
);
auth.setCredentials({
refresh_token: providerOptions.auth.refreshToken,
});
removeUndefined(options);
options.from = options.from || settings.defaultFrom;
options.replyTo = options.replyTo || settings.defaultReplyTo;
const raw = await createMail(options);
const gmail = google.gmail({ version: 'v1', auth });
return gmail.users.messages.send({
auth,
userId: providerOptions.auth.userId,
resource: {
raw,
},
});
}
module.exports = {
init: (providerOptions = {}, settings = {}) => ({
send: (options) => sendMail(providerOptions, settings, options),
}),
};