使用Google Apps脚本在Gmail中创建原生过滤器

时间:2016-11-23 20:03:46

标签: google-apps-script gmail gmail-api

我想为某些参数设置一个gmail原生的过滤器。

基本上我经常使用google别名功能(电子邮件后面的+号)。我想自动创建一个过滤器,读取“To”行,然后查找“+”。如果找到a“+”,它将标记“+”之后的内容。然后它将创建一个专用/本机过滤器,它将:跳过收件箱并应用“+”之后的标签。

我查看了gmail脚本,但没有找到制作本机过滤器的方法。我知道这个功能可能刚刚实现。

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); 
     } 
   }
}

任何帮助都会很棒。

解决方案:

// 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')
}

}

谢谢,恩德

1 个答案:

答案 0 :(得分:13)

该功能似乎存在,但需要启用完整的Gmail API。

对于您的Apps脚本,请按照https://developers.google.com/apps-script/guides/services/advanced上的说明启用高级Google服务。 当我第一次尝试使用Gmail.Users.Settings.Filters.list('me')电话时,我收到了授权错误,该错误为我提供了一个链接,可以在开发者控制台中启用Gmail API,这恰好可以翻转一个开关。

启用此功能后,您可以编写脚本来制作过滤器,例如

//
// 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')
}

我无法弄清楚如何创建一个自动追溯标记邮件的过滤器,因为Google似乎没有在其API中公开这些邮件。