【问题标题】:Create a native filter in Gmail using Google Apps Script使用 Google Apps 脚本在 Gmail 中创建本机过滤器
【发布时间】:2017-04-07 23:32:17
【问题描述】:

我想为某些参数集制作一个 gmail 原生的过滤器。

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

我查看了 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')
}

}

谢谢,安德

【问题讨论】:

  • 您希望它现在通过您的收件箱运行吗?或者当电子邮件进来时?到目前为止,您尝试了哪些代码?
  • 所以我希望它经常运行以查找尚未过滤的新电子邮件。本机过滤器应该一直在运行。我在高级功能中发现了一些过滤器设置:developers.google.com/gmail/api/v1/reference/users/settings/…
  • 我正在测试.getTo。我试图消除我的电子邮件和要检查的电子邮件之间的区别。不同之处在于标签。然后,一旦我有了我想要的标签,我就会制作过滤器。我还没有尝试制作过滤器。
  • function getsubject() { // 记录收件箱中前 50 封电子邮件的主题行 var email = Session.getActiveUser().getEmail(); Logger.log(电子邮件); var 线程 = GmailApp.getInboxThreads(0, 10); for (var i = 0; i

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


【解决方案1】:

该功能似乎存在,但需要启用完整的 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 中公开它。

【讨论】:

  • 哇,这超级有用!!!!非常感谢!我从来没有得到过这种高质量的帮助。我会投票,但我没有代表。
  • 我正计划在我的收件箱中查找所有 .to 文件,将它们与我的电子邮件进行比较,以获得标签部分。然后检查是否已经为其制作了标签或过滤器。然后生成带有“+”之后标签的过滤器。一旦有过滤器,如果您对其进行编辑,则可以选择将其应用于整个电子邮件。如果我在创建标签时找不到将它们全部移动到正确标签中的方法,则必须这样做。
  • 所以我花了一些时间尝试实现它的代码,但我真的不明白你在用 IF 语句做什么。我看到您正在检查过滤器是否已创建,但我不确定如何创建?此外,我运行的所有测试都为标签返回 null,所以我不确定我是否做错了什么。评论会有所帮助这是我第一次使用 java-script 和 google 脚本。
  • 您确定标签存在吗?您可以在创建过滤器之前尝试运行GmailApp.createLabel(labelName) 以创建标签。您的权限也都井井有条吗?我花了一些试验和错误来让这些工作。
  • 所以我已经弄清楚我想做什么了。我确实需要创建标签,但只有在我检查过滤器以查看是否为特定地址设置了过滤器之后。一旦我知道地址没有过滤器,它将制作标签并删除收件箱标签并应用电子邮件标签。权限不是问题。对这一行的理解更加深入:labels.labels.forEach(function (l) 在我更好地掌握了基于对象的东西之后,我能够将代码放在一起。我应该在哪里发布我为此完成的代码?作为另一个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2021-06-20
相关资源
最近更新 更多