-
Notifications
You must be signed in to change notification settings - Fork 0
/
GmailBulkForward.gs
53 lines (47 loc) · 1.73 KB
/
GmailBulkForward.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
// A B C
// 1 Name BulkForwardTo GmailSearchOperator
// 2 ComSys [email protected] "newer_than:1d from:([email protected]) to:([email protected]) subject:[vuls] (c-ap OR c-db) -High:0"
// 3 NetSys [email protected] "newer_than:1d from:([email protected]) to:([email protected]) subject:[vuls] (n-ap OR n-db) -High:0"
function GmailBulkForward(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var start = 0;
var max = 100;
var lastRow = sheet.getLastRow();
Logger.log("lastRow:" + lastRow);
for ( var r = 2; r <= lastRow ; r++ ) {
var subject = sheet.getRange(r, 1).getValue();
Logger.log("subject:" + subject);
var recipient = sheet.getRange(r, 2).getValue();
Logger.log("recipient:" + recipient);
var search = sheet.getRange(r, 3).getValue();
Logger.log("search:" + search);
if ( !subject || !recipient || !search ) {
continue; // empty
}
var threads = GmailApp.search(search, start, max);
Logger.log("threads.length:" + threads.length);
if ( !threads.length ) {
continue; // not found
}
var arryBlob = [];
var summary = [];
for (var t in threads) {
var thread = threads[t];
var msgs = thread.getMessages();
for(var m in msgs){
var msg = msgs[m];
var name = msg.getSubject();
arryBlob.push(Utilities.newBlob(msg.getPlainBody()).setName(name));
}
summary.push(name);
}
arryBlob.sort(function (a, b) {
return a.getName() > b.getName() ? 1 : -1;
});
GmailApp.sendEmail(recipient,
subject,
"total:" + summary.length "\n" + summary.sort().join('\n') + "\n-- GmailBulkForward\n" , // body
{ noReply: true, attachments: arryBlob });
Utilities.sleep(1000);
}
}