-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.gs
More file actions
79 lines (65 loc) · 1.93 KB
/
report.gs
File metadata and controls
79 lines (65 loc) · 1.93 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
function report() {
const mailto = [ "to@test.mail" ];
const d = new Date;
const subject = "Mail report " + d.getFullYear() + "/" + d.getMonth() + "/" + d.getDay() +
" " + d.getHours() + ":" + d.getMinutes();
const nmails = 100;
const limit = 10;
const label_name = "reported";
const query = "in:inbox NOT label:" + label_name;
let label = get_label(label_name);
if (label == null)
return;
let year = get_label(d.getFullYear());
if (year == null)
return;
let trash180 = get_label("trash180");
if (trash180 == null)
return;
let label = GmailApp.getUserLabelByName(label_name);
if (label == null) {
GmailApp.createLabel(label_name);
label = GmailApp.getUserLabelByName(label_name);
if (label == null) {
Logger.log("couldn't create label: " + label_name);
return;
}
}
for (let n = 0; n < limit; n++) {
var body = new Array;
var threads = GmailApp.search(query, 0, nmails);
var l = threads.length;
if (l == 0)
break;
for (let i in threads) {
var messages = threads[i].getMessages();
for (var j in messages) {
var m = messages[j];
s = "[" + i+", "+j + "]:" + m.getSubject() + " from " + m.getFrom();
body.push(s);
}
}
mailto.forEach(function sendmail(to){
let mail = GmailApp.createDraft(to, subject, body.join("\n"));
mail.send();
Logger.log("sent to " + to + " reported " + body.length + " mailes");
})
label.addToThreads(threads);
year.addToThreads(threads);
trash180.addToThreads(threads);
GmailApp.moveThreadsToArchive(threads);
}
}
function get_label(l) {
let label = GmailApp.getUserLabelByName(l);
if (label != null)
return label;
GmailApp.createLabel(l);
label = GmailApp.getUserLabelByName(l);
if (label == null) {
Logger.log("couldn't create label " + l);
} else {
Logger.log("created label " + l);
}
return label;
}