Skip to content

Instantly share code, notes, and snippets.

@dueyfinster
Created May 13, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dueyfinster/a66238c3f99c883ffd8d264b539dc2d8 to your computer and use it in GitHub Desktop.
Save dueyfinster/a66238c3f99c883ffd8d264b539dc2d8 to your computer and use it in GitHub Desktop.
Delete old newsletters in Gmail as soon as the the latest issue arrives, using Google Script
function processInbox() {
Logger.log("Starting");
var mailingLists = [
"list:'faa8eb4ef3a111cef92c4f3d4mc list <faa8eb4ef3a111cef92c4f3d4.583821.list-id.mcsv.net>'",
"list:'e2e180baf855ac797ef407fc7mc list <e2e180baf855ac797ef407fc7.654029.list-id.mcsv.net>'",
"list:'9f4b80a35728f7271fe3ea6ffmc list <9f4b80a35728f7271fe3ea6ff.511493.list-id.mcsv.net>'"
];
for(var i = 0; i < mailingLists.length; i++){
// process all threads in the Inbox
var threads = searchMessages(mailingLists[i]);
if(threads.length > 1){
Logger.log("Found: " + threads.length + " messages to process, for " + mailingLists[i]);
var msgToSort = getMessagesFromThreads(threads);
msgToSort.sort(function(a,b) {
return new Date(b.date).getTime() - new Date(a.date).getTime();
});
msgToSort.splice(0, 1); // Remove latest mail
for (var i = 0; i < msgToSort.length; i++) {
Logger.log("Moving message: " + msgToSort[i].subject + " to trash");
msgToSort[i].message.moveToTrash();
}
Logger.log(msgToSort);
}
}
}
function searchMessages(filter){
return GmailApp.search(filter);
}
function getMessagesFromThreads(threads){
var msgsToSort=[];
for (var i = 0; i < threads.length; i++) {
// get all messages in a given thread
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
msgsToSort.push(processMessage(messages[j]));
}
}
return msgsToSort;
}
function processMessage(message) {
var subject = message.getSubject();
var date = message.getDate();
Logger.log(date + " " + subject);
return {"subject":subject, "message": message, "date": date }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment