I am using a script based on this one here to automatically archive emails older than 7 days that are not starred. Starred emails stay in the inbox until unstarred, at that point emails older than 7 days meet the rules for getting archived.
My actual script:
function GmailArchive() {
var batchSize = 100 // Process up to 100 threads at once
var threads = GmailApp.search('label:"inbox" -label:"starred" older_than:7d');
for (j = 0; j < threads.length; j+=batchSize) {
GmailApp.moveThreadsToArchive(threads.slice(j, j+batchSize));
}
}
What I'd like to do is setup a similar parallel function in the script that archives emails that are older than 1 days and from emails not in my google contacts. If we could avoid from having to apply a label that would be great, no biggie if we do.
My thoughts so far:
Checking against getTo()
getCc()
or getBcc()
fields, if more than one address in any one of those fields archive if more than 2 days old.
var thread = GmailApp.getInboxThreads(0,1)[0]; // get first thread in inbox
var message = thread.getMessages()[0]; // get first message
Logger.log(message.getTo()); // log the recipient of message
I'm not sure where the return of this goes to, Logger.log? Would that be a file or a console? How do I capture the return and use it in a if >1 then archive otherwise continue to next message/thread etc until it runs out of messages/threads etc and exits.
I don't have formal training in coding, my apologies.The reference material that I'm looking at is here.
For starters, I suggest you first check out the whole Google Apps Script documentation so things will be easier to understand.
Logger.log would be the console messages if you're using the Logger class from the Apps Script API. In most programming languages, you can capture return values of methods by assigning the function calls to a variable or by simply just using the function calls in your operations.
With a mix of using the Gmail and the Contacts Script API, an approach I thought of is to retrieve all of your inbox emails older than 1d.
Loop to each email then retrieve the sender's email address via getFrom()
then use the email retrieved to getContactsByEmailAddress()
. If it returns null, then Archive the email.
var threads = GmailApp.search('label:"inbox" older_than:1d');
for (j = 0; j < threads.length; j+=batchSize) {
/* If (getContactsByEmailAddress('address from getFrom()') = null)
GmailApp.moveThreadsToArchive(threads.slice(j, j+batchSize)) */
}