-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailToTaskSystems.js
144 lines (110 loc) · 5.1 KB
/
EmailToTaskSystems.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
// ________________Add Your Configuration Here_________________
// Add your email address(es) and label(s):
// paste your task system ingest email here:
var taskEmail = 'YOUR_TASK_SYSTEM_EMAIL_HERE';
// update this label if you don't like the default:
var taskProcessedLabelString = 'Script_Processed/Tasks';
// update this prefix if you don't like the default:
var taskPrefix = '[Task] '
// ________________You're Done Configuration!_________________
// No need to read below this line unless you want to!
// If you've put your email and label information in above, you're good to go :)
// using just one service, wrap up as email-label pair:
// (this structure allows usage of multiple services for advanced users)
var emailLabelPairs = [[taskEmail, taskProcessedLabelString]];
// Let's be gross and use global constants
var HEADER_FIELD_TO_USE_AS_MESSAGE_ID = 'Message-Id'.toLowerCase();
var UNABLE_TO_PARSE_MESSAGE_ID = 'Unable to parse universal Message-Id';
function getUniversalMessageId(message) {
Logger.log('Getting universal message id for message %s', message);
var googleId = message.getId();
Logger.log('Google internal message id (different from header): %s', googleId);
var rawContent = message.getRawContent();
// Logger.log('raw_content: %s', rawContent);
var rawContentLines = rawContent.split('\n');
// Logger.log('raw_content_lines: %s', rawContentLines);
for (var i = 0; i < rawContentLines.length; i++){
var line = rawContentLines[i];
if (line.substring(0, 10).toLowerCase() === HEADER_FIELD_TO_USE_AS_MESSAGE_ID) {
Logger.log('Found the message id header field');
var messageIdComponents = line.split(' ');
Logger.log('messageIdComponents: %s', messageIdComponents);
// at this point, either messageIdComponents[1] holds the Message-ID
// value (with brackets), or the sender (eg outlook which behaves
// differently) has a newline following the Message-ID key and the actual
// value is on the next line (with this line being either undefined or a
// newline)
if ((typeof messageIdComponents[1] === "undefined") || (messageIdComponents[1].length <= 1)) {
Logger.log("Message-ID header line appears to be split into two lines");
Logger.log("Next line: " + rawContentLines[i + 1]);
var idWithAligatorBrackets = rawContentLines[i + 1];
} else {
var idWithAligatorBrackets = messageIdComponents[1];
};
Logger.log('id_with_aligagtor_brackets: %s', idWithAligatorBrackets);
var messageId = idWithAligatorBrackets.split('<')[1].split('>')[0];
Logger.log('Universal Message-ID parsed from header: %s', messageId);
return messageId;
};
};
// If we can't parse out a message id but don't care to debug it, we can just
// return our failure
return UNABLE_TO_PARSE_MESSAGE_ID;
};
function SendNewMessageWithCustomBody(originalMessage) {
// Create subject and body of new message to send
var prefixedSubject = taskPrefix + originalMessage.getSubject();
var body = originalMessage.getPlainBody() ? originalMessage.getPlainBody() : originalMessage.getBody();
// Try to get the universal message id
var messageId = getUniversalMessageId(originalMessage);
if (messageId === UNABLE_TO_PARSE_MESSAGE_ID) {
var messageLink = 'Unable to parse out message id';
} else {
var messageLink = 'message:<' + messageId + '>';
};
// Modify message body to include message link
var bodyWithLink = 'View Message in Mail: '+ messageLink + '\n\n' + body;
// Send out the email to all emails listed in email_label_pairs
Logger.log('emailLabelPairs in send func: %s', emailLabelPairs);
emailLabelPairs.forEach(function(emailLabelPair) {
GmailApp.sendEmail(emailLabelPair[0], prefixedSubject, bodyWithLink);
});
};
function markImportantAddLabelsUnstarMessagesArchiveThread(thread) {
Logger.log('Marking thread important');
// Mark the thread important
thread.markImportant();
Logger.log('Adding processed labels');
// Add each processed label to the thread
emailLabelPairs.forEach(function(emailLabelPair) {
// (we delay getting the actual label as long as possible because of rate
// limits:)
var actualLabel = GmailApp.getUserLabelByName(emailLabelPair[1]);
thread.addLabel(actualLabel);
});
Logger.log('Unstarring messages in thread');
// Unstar each message in the thread
var thread_messages = thread.getMessages();
thread_messages.forEach(function(message) {
message.unstar();
});
Logger.log('Archiving the thread');
// Archive the thread
GmailApp.moveThreadToArchive(thread);
Logger.log('Leaving markImportant... function');
};
function main() {
Logger.log('Beginning main');
// Go through the inbox threads:
var inbox_threads = GmailApp.getInboxThreads();
inbox_threads.forEach(function(thread) {
var messages = thread.getMessages().reverse();
messages.forEach(function(message) {
if (message.isStarred()) {
// For the most recent (if any) starred message in each thread:
SendNewMessageWithCustomBody(message);
markImportantAddLabelsUnstarMessagesArchiveThread(thread);
};
});
});
};