-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode.js
56 lines (52 loc) · 1.42 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
/**
* Disposable Gmail Address
* ========================
*
* Written by Amit Agarwal
* Email: [email protected]
* Web: https://www.labnol.org
* Twitter: @labnol
*
* Under MIT License
*/
const RECIPIENT = '[email protected]';
/**
* Run a trigger every 15 minutes that checks for
* new emails in the Gmail inbox folder
*/
const initialize = () => {
ScriptApp.getProjectTriggers().forEach((trigger) => {
ScriptApp.deleteTrigger(trigger);
});
ScriptApp.newTrigger('checkTemporaryInbox').timeBased().everyMinutes(5).create();
};
/** Check if an email message should be forward from the
* temporary inbox to the main Gmail inbox based on the
* date in the TO field of the incoming message
*/
const isAllowed_ = (email = '') => {
const [, mm, dd, yyyy] = email.match(/\+(\d{2})(\d{2})(\d{4})?@/) || [];
if (mm) {
const now = new Date();
const date = new Date([yyyy || now.getFullYear(), mm, dd].join('/'));
date.setHours(23);
date.setMinutes(59);
return date > now;
}
return false;
};
/**
* Fetch the 10 most recent threads from Gmail inbox,
* parse the To field of each message and either forward it
* or archive the emssage
*/
const checkTemporaryInbox = () => {
GmailApp.getInboxThreads(0, 10).forEach((thread) => {
thread.getMessages().forEach((message) => {
if (isAllowed_(message.getTo())) {
message.forward(RECIPIENT);
}
});
thread.moveToArchive();
});
};