Let me preface my question by saying that I am not a JS dev, or even a web dev, so bear with me! I'm trying to create a gs script that deletes messages that are older than n months that have a certain label and are from a given sender.
I think I have it down, but the Gmail API getFrom() method seems to return the sender's address in the format "First Last" <[email protected]> rather than just [email protected]. Currently I can work around this by the fact that I know this information, but is there a better way of doing this so that the code operates solely on the actual email address?
My current code:
function auto_delete_emails() {
var label = GmailApp.getUserLabelByName("foo");
var sender = "\"Foo Bar\" <[email protected]>";
if (label != null) {
var delayDays = 30
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
var from = messages[0].getFrom();
if (from == sender) {
if (threads[i].getLastMessageDate < maxDate) {
threads[i].moveToTrash();
}
}
}
}
}
I have seen a question that used a regex to fix this, but I barely understand regex, never mind how to apply in this context or how the example worked (or didn't!).
Suggestions for other improvements on this code also welcome.
var from = messages[0].getFrom().replace(/^.+<([^>]+)>$/, "$1");- iJay