-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
68 lines (53 loc) · 2.06 KB
/
src.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const { google } = require('googleapis');
// function for triggering mail
async function runSample(auth, incomingThreadID, fromAddress, subject, messageId) {
const gmail = google.gmail({ version: 'v1', auth });
// You can use UTF-8 encoding for the subject using the method below.
// You can also just use a plain string if you don't need anything fancy.
const utf8Subject = `=?utf-8?B?${Buffer.from(subject).toString('base64')}?=`;
// supply the message parts
const messageParts = [
'From: Jack Sparrow <[email protected]>',
`To: ${fromAddress}`,
'Content-Type: text/html; charset=utf-8',
'MIME-Version: 1.0',
`Reference: ${messageId}`,
`In-Reply-To: ${messageId}`,
`Subject: ${utf8Subject}`,
'',
`🌴 Vacation Notice 🌴
Hello,
I hope this email finds you well. I wanted to let you know that I am currently on vacation. During this time, I will have limited access to email and may not be able to respond promptly.
Thank you for your understanding. I will get back to you as soon as I return.
Best regards,
Jack`,
];
// join the message parts as single string
const message = messageParts.join('\n');
// The body needs to be base64url encoded.
const encodedMessage = Buffer.from(message)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
// initiate the send email mechanism
const res = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage,
threadId: incomingThreadID
},
});
// attach a new label to sent email
const updatedGmail = await gmail.users.messages.modify({
userId: "me",
id: res.data.id,
requestBody: {
addLabelIds: ["Label_5091976681185583145"],
}
})
// console.log("after Update", updatedGmail.data);
// return the response
return res.data;
}
module.exports = runSample