-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselfCheckTest.gs
38 lines (35 loc) · 1.61 KB
/
selfCheckTest.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
function main() {
// CHANGE THESE
const emailFrom = "[email protected]"; // email address from which 'message pending' message is received
const codeWord = "SelfCheckTest"; // word that is in the message sent from test email sender
const labelName = "Checked"; // label to create for tested emails
const recipient = '[email protected]'; // email to send the test result to
if (!GmailApp.getUserLabelByName(labelName)) GmailApp.createLabel(labelName);
const label = GmailApp.getUserLabelByName(labelName);
var threads = GmailApp.search("newer_than:1d from:" + emailFrom + " +" + codeWord,0,10); // find specific mail received in the last 24 hours
// result of the test depending on if pending message is found
if(threads.length) {
label.addToThreads(threads);
const status = 'SUCCESS - ';
const body = 'Self check test successful';
sendMail(recipient,status,body);
}
else {
const status = 'FAIL - ';
const body = 'Self check test failed. Check group mail moderation settings.';
sendMail(recipient,status,body);
}
deleteOldTest(labelName);
}
// send mail with results
function sendMail(recipient,status,body){
var today = new Date();
today = Utilities.formatDate(today, 'Etc/GMT', 'yyyy/MM/dd');
const subjEnd = 'Self check google groups ' + today; //last part of subject always the same
GmailApp.sendEmail(recipient, status + subjEnd, body);
}
// deleting previous test mails
function deleteOldTest(labelName){
const threadsToDel = GmailApp.search('older_than:1d label:' + labelName,0,10);
GmailApp.moveThreadsToTrash(threadsToDel);
}