-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpireEmail.gs
52 lines (40 loc) · 1.26 KB
/
ExpireEmail.gs
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
// Title: Expire Emails
// Author: Brian Chong ([email protected])
// Description: Move Tagged Gmail Threads to trash after a certain number of days has passed
// URL: Google Drive version http://goo.gl/Ffy5K
function expire() {
expireEmails('Expires/Tomorrow');
expireEmails('Expires/Week',7);
expireEmails('Expires/30 Days',30);
}
function expireEmails(ExpireLabelString,Days){
var ExpireLabelSearch = GmailApp.search('before:' + modifiedDate(Days) + ' label:"' + ExpireLabelString + '"') ;
if (ExpireLabelSearch.length > 0) {
GmailApp.moveThreadsToTrash(ExpireLabelSearch);
}
}
function modifiedDate (Days) {
var dateObj = new Date();
if (!Days) {Days = 0;}
dateObj.setDate(dateObj.getDate() - Days);
return Utilities.formatDate(dateObj, "GMT", "yyyy/MM/dd");
}
function onInstall() {
try {
// Create Labels
GmailApp.createLabel('Expires');
GmailApp.createLabel('Expires/Tomorrow');
GmailApp.createLabel('Expires/Week');
GmailApp.createLabel('Expires/30 Days');
// Create Trigger to call expire() everyday
createTrigger('expire');
} catch (e) {
Browser.msgBox(e.message);
}
}
function createTrigger(t) {
ScriptApp.newTrigger(t)
.timeBased()
.everyDays(1)
.create();
}