-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.js
204 lines (170 loc) · 5.56 KB
/
Code.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
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
/**
* https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get
* https://developers.google.com/gmail/api/reference/rest/v1/users.messages/delete
*/
function listMessages() {
var messageList = Gmail.Users.Messages.list('me', {
includeSpamTrash: true,
//labelIds: ['SPAM'],
//q: '!label:inbox from:([email protected]) after:2019/04/21',
q: 'label:DeleteForever',
maxResults: 10
});
if (!messageList.messages || !messageList.messages[0]) {
Logger.log("No messages found.");
return;
}
messageList.messages.forEach(function(message) {
var messageObject = Gmail.Users.Messages.get('me', message.id);
var senderAddress = getHeaderValue(messageObject, "From").match(/[^@<\s]+@[^@\s>]+/);
logString(senderAddress);
Gmail.Users.Messages.remove('me', message.id);
incCounter();
var autoResponseDraft = getDraft();
var draftSubject = getHeaderValue(autoResponseDraft, "Subject");
var draftBody = getBodyHtml(autoResponseDraft);
//Logger.log(draftBody);
sendMail(senderAddress, draftSubject, draftBody);
});
}
/**
* https://stackoverflow.com/questions/24811008/gmail-api-decoding-messages-in-javascript
*/
function getDraft() {
var draftList = Gmail.Users.Drafts.list('me', {
includeSpamTrash: true,
//labelIds: ['SPAM'],
q: 'label:AutoResponderTemplate',
maxResults: 1
});
if (!draftList.drafts || !draftList.drafts[0]) {
Logger.log("No drafts found.");
return;
}
var msg = "";
for (var i = 0; i < draftList.drafts.length; i++) {
var draft = draftList.drafts[i];
var draftObject = Gmail.Users.Drafts.get('me', draft.id, {format: "full"});
var msg = draftObject.message;
};
return msg;
}
/**
* https://stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+Gmail.Users.messages.send
* https://gist.github.com/oshliaer/7e96514c3cb3075dde02#file-message-gs
*/
function sendMail(recipient, subject, body) {
var raw = Utilities.base64EncodeWebSafe("Subject: =?UTF-8?B?" + Utilities.base64Encode(subject, Utilities.Charset.UTF_8) + "?=\r\n" +
"From: " + Session.getActiveUser().getEmail() + "\r\n" +
"To:" +recipient+"\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
body+"\r\n\r\n", Utilities.Charset.UTF_8);
var message = Gmail.newMessage();
message.raw = raw;
var result = Gmail.Users.Messages.send(message, 'me');
//Logger.log(JSON.stringify(result));
}
function getHeaderValue(messageObject, headerName) {
return messageObject.payload.headers.filter(function(header) {
return header.name === headerName;
})[0].value;
/*for (var i = 0; i < messageObject.payload.headers.length; i++) {
var header = messageObject.payload.headers[i];
if(header.name == headerName) {
return header.value;
}
}*/
}
function getBodyHtml(message) {
var part = message.payload.parts.filter(function(part) {
return part.mimeType == 'text/html';
})[0];
var messageBlob = Utilities.newBlob(part.body.data, Utilities.Charset.UTF_8);
var messageBody = messageBlob.getDataAsString("UTF-8");
return messageBody;
}
function readValue(key) {
var userProperties = PropertiesService.getUserProperties();
return userProperties.getProperty(key);
}
function storeValue(key, value) {
if(typeof value === 'object') {
value = JSON.stringify(value);
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty(key, value);
}
function incCounter() {
var runCounter = readValue('run_counter');
if(runCounter == null) {
storeValue('run_counter', 1)
}
else {
runCounter = parseInt(runCounter, 10);
storeValue('run_counter', runCounter+1);
}
}
function logString(text) {
var logList = readValue('logList');
if(logList == null) {
var logList = new Array();
}
else {
logList = JSON.parse(logList)
}
logList.push(text);
storeValue('logList', logList)
}
function readAllValues() {
var userProperties = PropertiesService.getUserProperties();
var data = userProperties.getProperties();
for (var key in data) {
Logger.log('Key: %s, Value: %s', key, data[key]);
}
}
function flushAll() {
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteAllProperties();
}
/**
* https://developers.google.com/gmail/api/v1/reference/users/threads/get
* https://github.com/gsuitedevs/apps-script-samples/blob/master/advanced/gmail.gs
*/
/*function listThreads() {
var threadList = Gmail.Users.Threads.list('me', {
includeSpamTrash: true,
labelIds: ['SPAM'],
//q: 'label:inbox'
});
if (!threadList.threads || !threadList.threads[0]) {
Logger.log('No sent threads found.');
return;
}
Logger.log("Threads:");
threadList.threads.forEach(function(thread) {
Logger.log('Snippet: %s', thread.snippet);
});
}*/
/**
* Lists the user's labels, including name, type,
* ID and visibility information.
*
* https://developers.google.com/apps-script/advanced/gmail
*/
/*function listLabelInfo() {
var response =
Gmail.Users.Labels.list('me');
for (var i = 0; i < response.labels.length; i++) {
var label = response.labels[i];
Logger.log(JSON.stringify(label));
}
}*/
/**
* https://stackoverflow.com/questions/15995031
*/
/*function deleteForever(userId, labelName) {
var threads = GmailApp.search("in:trash label:" + labelName);
for (var i = 0; i < threads.length; i++) {
Gmail.Users.Messages.remove(userId, threads[i].getId());
}
}*/