Created
May 11, 2023 17:40
-
-
Save ergosteur/ef751c37eedaa2750cf119f7211a71bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Processes emails in a given label in Gmail. | |
* Can mark emails as read and/or archive them based on the specified action. | |
* Only processes emails older than the specified number of days. | |
* Can log the subject of each email it processes if verbose mode is enabled. | |
* | |
* @param {string} labelName - The name of the label to process emails in. | |
* @param {number} days - The number of days old an email must be to be processed. | |
* @param {number} action - The action to perform on each email (0 = none, 1 = mark as read, 2 = archive, 3 = mark as read and archive). | |
* @param {boolean} verboseMode - Whether or not to log the subject of each email processed. | |
* | |
* @version 1.0 | |
* @date 2023-05-11 | |
* @author Matthieu, OpenAI, Bing | |
*/ | |
function processEmails(labelName, days, action, verboseMode) { | |
var label = GmailApp.getUserLabelByName(labelName); | |
var unreadCount = label.getUnreadCount(); | |
if (unreadCount == 0 && action == 1) { | |
if (verboseMode) { | |
Logger.log("INFO: The label '" + labelName + "' has no unread threads. Exiting."); | |
} | |
return; | |
} | |
else { | |
if (verboseMode) { | |
Logger.log("Processing label '" + labelName + "'."); | |
} | |
} | |
var batchSize = 50; | |
var start = 0; | |
var cutOffDate = new Date(); | |
cutOffDate.setDate(cutOffDate.getDate() - days); | |
var markAsReadCount = 0; | |
while (true) { | |
var threads = label.getThreads(start, batchSize); | |
if (threads.length == 0) { | |
break; | |
} | |
for (var i = 0; i < threads.length; i++) { | |
var message = threads[i].getMessages()[0]; | |
if (unreadCount > 0 && threads[i].isUnread() && threads[i].getLastMessageDate() < cutOffDate){ | |
if (action == 1 || action == 3) { | |
if (verboseMode) { | |
Logger.log("ACTION: Marking as read: " + message.getSubject() + " from " + message.getDate() + " by " + message.getFrom()); | |
} | |
threads[i].markRead(); | |
markAsReadCount++; | |
if (markAsReadCount == unreadCount) { | |
if (verboseMode) { | |
Logger.log("INFO: No more unread messages in label " + labelName); | |
} | |
break; | |
} | |
} | |
} else if (verboseMode) { | |
Logger.log("INFO: No mark as read action taken on thread: " + message.getSubject() + " from " + message.getDate() + " by " + message.getFrom()); | |
} | |
if ((action == 2 || action == 3) && threads[i].isInInbox()) { | |
if (verboseMode) { | |
Logger.log("ACTION: Archiving: " + message.getSubject() + " from " + message.getDate() + " by " + message.getFrom()); | |
} | |
threads[i].moveToArchive(); | |
} else if (verboseMode) { | |
Logger.log("INFO: No archive action taken on thread: " + message.getSubject() + " from " + message.getDate() + " by " + message.getFrom()); | |
} | |
} | |
if (markAsReadCount == unreadCount) { | |
break; | |
} | |
start += batchSize; | |
} | |
} | |
/** | |
* This function takes in two parameters: labelName and days. | |
* It first checks if the zArchive/[labelName] label exists and creates it if it doesn’t. | |
* Then it gets all the threads with the specified label and checks if their last message date is older than the specified number of days. | |
* If it is, it adds the zArchive/[labelName] label to the thread and removes the original label. | |
*/ | |
function archiveThreads(labelName, days, verboseMode) { | |
var label = GmailApp.getUserLabelByName(labelName); | |
var archiveLabel = GmailApp.getUserLabelByName("zArchive/" + labelName); | |
if (!archiveLabel) { | |
archiveLabel = GmailApp.createLabel("zArchive/" + labelName); | |
} | |
var threads = label.getThreads(); | |
var cutOffDate = new Date(); | |
cutOffDate.setDate(cutOffDate.getDate() - days); | |
for (var i = 0; i < threads.length; i++) { | |
if (threads[i].getLastMessageDate() < cutOffDate) { | |
threads[i].addLabel(archiveLabel); | |
threads[i].removeLabel(label); | |
if (verboseMode) { | |
var messages = threads[i].getMessages(); | |
for (var j = 0; j < messages.length; j++) { | |
var message = messages[j]; | |
Logger.log("ACTION: Moving: " + message.getSubject() + " from " + message.getDate() + " by " + message.getFrom() + " from label " + labelName + " to label zArchive/" + labelName); | |
} | |
} | |
} | |
} | |
} | |
function mainFunction () { | |
archiveThreads("Logs", 14, true); | |
processEmails("Logs", 7, 3, true); | |
processEmails("SomeLabel", 7, 3 , true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment