-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgmail.js
More file actions
207 lines (192 loc) · 7.42 KB
/
gmail.js
File metadata and controls
207 lines (192 loc) · 7.42 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { getGmailClient } from './google-clients.js';
import { decodeBase64 } from './handlers-utils.js';
export { getLabels, createLabel, updateLabel, deleteLabel, listFilters, getFilter, createFilter, deleteFilter, replaceFilter, listMessageIdsByQuery, bulkModifyLabels, bulkModifyLabelsByQuery } from './gmail-manage.js';
function getGmail(auth) {
return getGmailClient(auth);
}
function extractBody(payload) {
let body = '';
if (payload.body && payload.body.data) {
body = decodeBase64(payload.body.data);
} else if (payload.parts) {
for (const part of payload.parts) {
if (part.mimeType === 'text/plain' && part.body && part.body.data) {
body = decodeBase64(part.body.data);
break;
} else if (part.mimeType === 'text/html' && part.body && part.body.data && !body) {
body = decodeBase64(part.body.data);
} else if (part.parts) {
const nestedBody = extractBody(part);
if (nestedBody && !body) body = nestedBody;
}
}
}
return body;
}
function extractHeaders(headers) {
const result = {};
const fields = ['From', 'To', 'Subject', 'Date', 'Cc', 'Bcc', 'Reply-To'];
for (const header of headers || []) {
if (fields.includes(header.name)) {
result[header.name.toLowerCase().replace('-', '_')] = header.value;
}
}
return result;
}
export async function listEmails(auth, maxResults = 20, query = null, labelIds = null) {
const gmail = getGmail(auth);
const params = { userId: 'me', maxResults };
if (query) params.q = query;
if (labelIds) params.labelIds = Array.isArray(labelIds) ? labelIds : [labelIds];
const listRes = await gmail.users.messages.list(params);
const messages = listRes.data.messages || [];
if (messages.length === 0) return { emails: [], count: 0 };
const emails = await Promise.all(
messages.map(async (msg) => {
const detail = await gmail.users.messages.get({
userId: 'me',
id: msg.id,
format: 'metadata',
metadataHeaders: ['From', 'To', 'Subject', 'Date']
});
const headers = extractHeaders(detail.data.payload.headers);
return {
id: msg.id,
threadId: msg.threadId,
snippet: detail.data.snippet,
...headers,
labelIds: detail.data.labelIds || []
};
})
);
return { emails, count: emails.length };
}
export async function searchEmails(auth, query, maxResults = 20) {
return listEmails(auth, maxResults, query);
}
export async function readEmail(auth, messageId, format = 'full') {
const gmail = getGmail(auth);
const res = await gmail.users.messages.get({ userId: 'me', id: messageId, format });
const message = res.data;
const headers = extractHeaders(message.payload.headers);
const body = extractBody(message.payload);
return {
id: message.id,
threadId: message.threadId,
labelIds: message.labelIds || [],
snippet: message.snippet,
...headers,
body,
internalDate: message.internalDate
};
}
export async function getEmailAttachments(auth, messageId) {
const gmail = getGmail(auth);
const res = await gmail.users.messages.get({ userId: 'me', id: messageId, format: 'full' });
const attachments = [];
function findAttachments(payload) {
if (payload.parts) {
for (const part of payload.parts) {
if (part.filename && part.body && part.body.attachmentId) {
attachments.push({
filename: part.filename,
mimeType: part.mimeType,
attachmentId: part.body.attachmentId,
size: part.body.size
});
}
if (part.parts) findAttachments(part);
}
}
}
findAttachments(res.data.payload);
return { messageId, attachments };
}
export async function downloadAttachment(auth, messageId, attachmentId) {
const gmail = getGmail(auth);
const res = await gmail.users.messages.attachments.get({ userId: 'me', messageId, id: attachmentId });
return { data: res.data.data, size: res.data.size };
}
export async function sendEmail(auth, to, subject, body, cc = null, bcc = null) {
const gasEndpoint = process.env.GAS_EMAIL_ENDPOINT;
if (gasEndpoint) {
const authHeaders = await auth.getRequestHeaders();
const payload = { to, subject, body };
if (cc) payload.cc = cc;
if (bcc) payload.bcc = bcc;
const res = await fetch(gasEndpoint, {
method: 'POST',
headers: { ...authHeaders, 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const text = await res.text();
let data;
try { data = JSON.parse(text); } catch { data = { response: text }; }
if (!res.ok) throw new Error(`GAS endpoint error ${res.status}: ${text}`);
return data;
}
const gmail = getGmail(auth);
const lines = [
`To: ${to}`,
`Subject: ${subject}`
];
if (cc) lines.push(`Cc: ${cc}`);
if (bcc) lines.push(`Bcc: ${bcc}`);
lines.push('Content-Type: text/plain; charset=utf-8');
lines.push('');
lines.push(body);
const message = lines.join('\r\n');
const encoded = Buffer.from(message).toString('base64url');
const res = await gmail.users.messages.send({ userId: 'me', requestBody: { raw: encoded } });
return { id: res.data.id, threadId: res.data.threadId, labelIds: res.data.labelIds };
}
export async function deleteEmail(auth, messageId) {
const gmail = getGmail(auth);
await gmail.users.messages.delete({ userId: 'me', id: messageId });
return { deleted: messageId };
}
export async function trashEmail(auth, messageId) {
const gmail = getGmail(auth);
const res = await gmail.users.messages.trash({ userId: 'me', id: messageId });
return { id: res.data.id, threadId: res.data.threadId, labelIds: res.data.labelIds };
}
export async function modifyLabels(auth, messageId, addLabels = [], removeLabels = []) {
const gmail = getGmail(auth);
const res = await gmail.users.messages.modify({
userId: 'me',
id: messageId,
requestBody: { addLabelIds: addLabels, removeLabelIds: removeLabels }
});
return { id: res.data.id, threadId: res.data.threadId, labelIds: res.data.labelIds };
}
export async function draftEmail(auth, to, subject, body, cc = null, bcc = null) {
const gmail = getGmail(auth);
const lines = [`To: ${to}`, `Subject: ${subject}`];
if (cc) lines.push(`Cc: ${cc}`);
if (bcc) lines.push(`Bcc: ${bcc}`);
lines.push('Content-Type: text/plain; charset=utf-8', '', body);
const encoded = Buffer.from(lines.join('\r\n')).toString('base64url');
const res = await gmail.users.drafts.create({ userId: 'me', requestBody: { message: { raw: encoded } } });
return { id: res.data.id, messageId: res.data.message?.id };
}
export async function getThreadContent(auth, threadId, format = 'full') {
const gmail = getGmail(auth);
const res = await gmail.users.threads.get({ userId: 'me', id: threadId, format });
const messages = (res.data.messages || []).map(m => ({
id: m.id, snippet: m.snippet, labelIds: m.labelIds || [],
...extractHeaders(m.payload.headers), body: extractBody(m.payload)
}));
return { threadId: res.data.id, messages };
}
export async function getMessagesBatch(auth, messageIds, format = 'full') {
const gmail = getGmail(auth);
const results = await Promise.all(
messageIds.map(async id => {
const res = await gmail.users.messages.get({ userId: 'me', id, format });
const m = res.data;
return { id: m.id, threadId: m.threadId, snippet: m.snippet,
...extractHeaders(m.payload.headers), body: extractBody(m.payload) };
})
);
return { messages: results, count: results.length };
}