Create a native filter in Gmail using Google Apps

2019-03-21 08:53发布

I want to make a filter that is native to gmail for certain set of parameters.

Basically I use the google alias function a lot (the + sign after your email). I want to automate the process of creating a filter that reads the "To" line then looks for a "+". If the a "+" is found it will make a label of what is after the "+". Then it will create a dedicated/native filter that will: skip the inbox and apply the label of what is after the "+".

I have looked through the gmail scripts and have not found a way to make a native filter. I understand that this functionality might 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.

Solution:

// 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's ('me') settings
Gmail.Users.Settings.Filters.create(filter, 'me')
}

}

Thanks, Ender

1条回答
小情绪 Triste *
2楼-- · 2019-03-21 09:12

The functionality seems to exist, but requires enabling the full Gmail API.

For your Apps script, follow the directions at https://developers.google.com/apps-script/guides/services/advanced to enable Advanced Google Services. When I first tried using the Gmail.Users.Settings.Filters.list('me') call I got an authorization error which gave me a link to enable the Gmail API in the Developer Console which just amounted to flipping a switch.

Once you have this enabled, you can write a script to make filters, like

//
// 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's ('me') settings
  Gmail.Users.Settings.Filters.create(filter, 'me')

}


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

I wasn't able to figure out how to create a filter that retroactively labels messages automatically since Google doesn't seem to expose that in their API.

查看更多
登录 后发表回答