Create your own filter in Gmail using Google Apps Script

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() { 
  // Log the To line of up to the first 50 emails in your Inbox 
  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:

// Creates a filter to put all email from ${toAddress} into
// Gmail label ${labelName}
function createToFilter(toAddress, labelName) {

// Lists all the filters for the user running the script, 'me'
var labels = Gmail.Users.Settings.Filters.list('me')

// Search through the existing filters for ${toAddress}
var label = true
labels.filter.forEach(function(l) {
    if (l.criteria.to === toAddress) {
        label = null
    }
})

// If the filter does exist, return
if (label === null) return
else { 
// Create the new label 
GmailApp.createLabel(labelName)

// Lists all the labels for the user running the script, 'me'
var labelids = Gmail.Users.Labels.list('me')

// Search through the existing labels for ${labelName}
// this operation is still needed to get the label ID 
var labelid = false
labelids.labels.forEach(function(a) {
    if (a.name === labelName) {
        labelid = a
    }
})
Logger.log(labelid);
Logger.log(labelid.id);
// Create a new filter object (really just POD)
var filter = Gmail.newFilter()

// Make the filter activate when the to address is ${toAddress}
filter.criteria = Gmail.newFilterCriteria()
filter.criteria.to = toAddress

// Make the filter remove the label id of ${"INBOX"}
filter.action = Gmail.newFilterAction()
filter.action.removeLabelIds = ["INBOX"];
// Make the filter apply the label id of ${labelName} 
filter.action.addLabelIds = [labelid.id];

// Add the filter to the user ('me') settings
Gmail.Users.Settings.Filters.create(filter, 'me')
}

}

Thanks Ender

+4
1

, , , API Gmail.

script https://developers.google.com/apps-script/guides/services/advanced, Google. Gmail.Users.Settings.Filters.list('me'), , , API Gmail , .

, script ,

//
// Creates a filter to put all email from ${toAddress} into
// Gmail label ${labelName}
//
function createToFilter (toAddress, labelName) {

  // Lists all the labels for the user running the script, 'me'
  var labels = Gmail.Users.Labels.list('me')

  // Search through the existing labels for ${labelName}
  var label = null
  labels.labels.forEach(function (l) {
    if (l.name === labelName) {
      label = l
    }
  })

  // If the label doesn't exist, return
  if (label === null) return

  // Create a new filter object (really just POD)
  var filter = Gmail.newFilter()

  // Make the filter activate when the to address is ${toAddress}
  filter.criteria = Gmail.newFilterCriteria()
  filter.criteria.to = toAddress

  // Make the filter apply the label id of ${labelName}
  filter.action = Gmail.newFilterAction()
  filter.action.addLabelIds = [label.id]

  // Add the filter to the user ('me') settings
  Gmail.Users.Settings.Filters.create(filter, 'me')

}


function main () {
  createToFilter('youruser+foo@gmail.com', 'Aliases/foo')
}

, , , Google, , API.

+6

Source: https://habr.com/ru/post/1661758/


All Articles