-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_script.txt
More file actions
49 lines (38 loc) · 1.39 KB
/
google_script.txt
File metadata and controls
49 lines (38 loc) · 1.39 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
// POST email contents to our Rails database
function sendHttpPost() {
var storedLabel = GmailApp.getUserLabelByName("stored");
var notStoredLabel = GmailApp.getUserLabelByName("not stored");
var threads = notStoredLabel.getThreads();
// var headers = ;
for(var i in threads){
var msg = threads[i].getFirstMessageSubject();
var msgs = threads[i].getMessages();
for(var m = 0; m < msgs.length; m +=1){
var body = msgs[m].getPlainBody();
var to = msgs[m].getTo();
var from = msgs[m].getFrom();
var payload =
// This JSON gets sent to the DB in the POST request
{
"title" : msg,
"body": body,
"sender": from,
"recipient": to
};
// Because payload is a JavaScript object, it will be interpreted as
// an HTML form. (We do not need to specify contentType; it will
// automatically default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options =
{
"method" : "post",
"Content-Type" : "application/json",
"Accept" : "application/json",
"payload" : payload
};
UrlFetchApp.fetch("http://3ef4397b.ngrok.com/messages", options);
}
threads[i].removeLabel(notStoredLabel);
threads[i].addLabel(storedLabel);
}
}