I want to create a filter that is native to gmail for a specific set of parameters.
I mainly use the google alias function (+ sign after your email). I want to automate the process of creating a filter that reads the string “To” and then searches for “+”. If "+" is found, it will make a label of what is after "+". Then he will create a dedicated / own filter, which will: skip the Inbox folder and apply the label of what is after the "+".
I looked through gmail scripts and did not find a way to create my own filter. I understand that this functionality may have just been implemented.
function getTo() {
var email = Session.getActiveUser().getEmail();
Logger.log(email);
var threads = GmailApp.getInboxThreads(0, 50);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
Logger.log(messages[j].getTo()||email);
}
}
}
Any help would be great.
Decision:
function createToFilter(toAddress, labelName) {
var labels = Gmail.Users.Settings.Filters.list('me')
var label = true
labels.filter.forEach(function(l) {
if (l.criteria.to === toAddress) {
label = null
}
})
if (label === null) return
else {
GmailApp.createLabel(labelName)
var labelids = Gmail.Users.Labels.list('me')
var labelid = false
labelids.labels.forEach(function(a) {
if (a.name === labelName) {
labelid = a
}
})
Logger.log(labelid);
Logger.log(labelid.id);
var filter = Gmail.newFilter()
filter.criteria = Gmail.newFilterCriteria()
filter.criteria.to = toAddress
filter.action = Gmail.newFilterAction()
filter.action.removeLabelIds = ["INBOX"];
filter.action.addLabelIds = [labelid.id];
Gmail.Users.Settings.Filters.create(filter, 'me')
}
}
Thanks Ender