-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
76 lines (66 loc) · 2.24 KB
/
Code.gs
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
function processInbox() {
var pageToken;
var processedCount = 0;
const maxToProcess = 500; // Adjust as needed
do {
try {
var threads = GmailApp.search("newer_than:7d", 0, 100);
for (var i = 0; i < threads.length && processedCount < maxToProcess; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length && processedCount < maxToProcess; j++) {
processMessage(messages[j]);
processedCount++;
}
}
if (threads.length < 100) {
break;
}
pageToken = threads[threads.length - 1].getId();
} catch (e) {
console.error('Error processing inbox:', e);
break;
}
} while (processedCount < maxToProcess);
console.log('Processed ' + processedCount + ' messages');
}
function processMessage(message) {
try {
var header = message.getHeader("X-PHISHTEST");
if (header == "This is a phishing security test from KnowBe4 that has been authorized by the recipient organization") {
var label = GmailApp.getUserLabelByName("knowbe4");
if (!label) {
label = GmailApp.createLabel("knowbe4");
console.log('Created new label: knowbe4');
}
var thread = message.getThread();
thread.addLabel(label);
archiveThread(thread);
console.log('Labeled and archived message: ' + message.getSubject());
}
} catch (e) {
console.error('Error processing message:', e);
}
}
function archiveThread(thread) {
thread.moveToArchive();
// Double-check if the thread is still in the inbox
Utilities.sleep(1000); // Wait for 1 second
if (thread.isInInbox()) {
console.log('Thread still in inbox after first attempt, trying again...');
thread.moveToArchive();
// Final check
Utilities.sleep(1000);
if (thread.isInInbox()) {
console.error('Failed to archive thread: ' + thread.getFirstMessageSubject());
} else {
console.log('Successfully archived on second attempt: ' + thread.getFirstMessageSubject());
}
} else {
console.log('Successfully archived: ' + thread.getFirstMessageSubject());
}
}
function main() {
console.log('Starting inbox processing...');
processInbox();
console.log('Finished inbox processing.');
}