Managing your gmail mailbox

I found out a way to get full control of my heavily loaded gmail mailbox… just plain code…without complicated looking (not easy to find) user menu’s to configure settings for archiving and cleaning my mailbox. Login to your gmail account (with your browser)…and then go to https://script.google.com/

Here you can put your (javascript) code and even debug it. Even better…after you are finished, you can expose this code as a (web)service or schedule it with the built in timer, woa …excellent dude!!!

Here is a snippet of the code I am using:

[javascript]

function cleanUp() {
var delayDays = 0; // Enter # of days before messages are moved to trash
var maxDate = new Date();
cleanupCandidates = ["meetup.com","alison.com","voordeelvanger.nl","email.campaign@sg.booking.com","alibaba@email.alibaba.com","linkedin.com","ezine.ns.nl","tix.nl","gorillasports.nl","tommyteleshopping.com","poldercasino.com","hortonworks.com","connect.wso2.com","implicit-explicit.com","geocaching.com","autodna.mail"];

maxDate.setDate(maxDate.getDate()-delayDays);
gthreads = GmailApp.getInboxThreads();
for (var fc=0; fc<cleanupCandidates.length; fc++){
for (var i=0; i<gthreads.length; i++){
messages = gthreads[i].getMessages();
for (var j=0; j<messages.length; j++){
if(messages[j].getFrom().indexOf(cleanupCandidates[fc])>-1){
if (messages[j].getDate()<=maxDate){
Logger.log("Message older than maxdate ("+delayDays+"days)…removing it: ");
Logger.log("THREAD ID"+gthreads[i].getId()+" FROM: "+messages[j].getFrom()+" subject: "+messages[j].getSubject());
messages[j].moveToTrash();
}
}
}
}
}
}

function labelStuff(){
archiveCandidates = ["slack.com","googleplay-noreply@google.com","microsoft.com","klm-mail.com","redhat.com","transip.nl","eve-dev1.datalinks.nl","eve-dev.datalinks.nl","bonque.nl","itaq.nl","email.apple.com","Niels Westmeijer <niels@eve.nu>"];
labelCandidate = ["Archief/eve","Archief/playstore","Archief/microsoft","Archief/reizen","Archief/redhat","Archief/hosting","Archief/hosting-sec","Archief/hosting-sec","Archief","Archief","Archief/iTunes","Archief/eve"];

gthreads = GmailApp.getInboxThreads();
for (var i=0; i<gthreads.length; i++){
messages = gthreads[i].getMessages();
if(messages.length!=null){
for (var j=0; j<messages.length; j++){
fromMail=messages[j].getFrom();
Logger.log("Evaluating message frpm: "+fromMail);
foundAtLocation=archiveCandidates.indexOf(fromMail);
if(foundAtLocation>0){
label=labelCandidate[foundAtLocation];
from=archiveCandidates[foundAtLocation];
Logger.log("Found at:"+foundAtLocation+" Applying label "+label+" for: THREAD ID"+gthreads[i].getId()+" FROM: "+from+" subject: "+messages[j].getSubject());
eveLabel = GmailApp.createLabel(label);
gthreads[i].addLabel(eveLabel);
gthreads[i].moveToArchive();

}
}
}
}
}
[/javascript]

here a screeshot:

Schermafbeelding 2016 02 15 om 21 52 35

screenshot of the trigger/ scheduler…cronjob editor

screeshot_trigger