如果电子邮件在 Gmail 的主题或正文中包含特定字词,则使用脚本对其进行标记
Script to label emails if it contains specific words in the subject or body on Gmail
更新:
非常感谢endothermic_dragon!你太聪明了!现在一切正常。
我以前创建过脚本,但从未为 Gmail 创建过。我很难创建一个脚本来过滤传入的电子邮件,如果它们有特定的词,则对其进行标记和存档。我们每天从不同的发件人处收到大约 100 封与一组公司有关的电子邮件。每个公司都有自己的标签。公司名称通常在电子邮件的主题或正文中。我正在尝试构建一个获取每封电子邮件的脚本,检查带有主题的数组或带有 plainBody 的数组是否包含来自 getUserLabels( ),如果是,则为其分配相应的标签。我已经尝试了一切,从 filter() 到 forEach() 到 indexOf() 但似乎没有任何效果。我觉得来自 getUserLabels() 和 getInoboxThreads() 的数组是二维的,我正在努力使用它们。我已经多次重写代码,但它让我无处可去。我真的希望你们中的一位能对此有所启发。
示例:
假设我们与 Apple、Amazon 和 Netflix 合作。这些公司中的每一个都有自己的标签。
如果收到一封电子邮件并且主题或正文中包含“Apple”,则为其添加标签“Apple”并将其存档。如果主题或正文中有“Amazon”和“Netflix”,则添加标签 Amazon 和 Netflix。
最终代码:
function labelEmails() {
var labels=[]
GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
var threads = GmailApp.search('is:unread').forEach(function(email){
email.getMessages().forEach(function(thread){
//Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
//Example used - if body contains 'hi', mark as read.
labels.forEach(function(label){
if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
labelToAssign = GmailApp.getUserLabelByName(label);
labelToAssign.addToThread(email)
} else if (thread.getSubject().toLowerCase().includes(label.toLowerCase())) {
labelToAssign = GmailApp.getUserLabelByName(label);
labelToAssign.addToThread(email)
}
})
})
});
}
我们的一些标签是公司名称的缩写,所以我还需要一个函数来处理这些标签。您也可以在下面查看:
(代码中的公司为随机公司,并非真实公司)
function labelExceptions() {
var exceptions = [{exc:'apple', labeln: 'APPL'}, {exc: 'amazon', labeln: 'AMZ'}, {exc: 'Bank of America', labeln: 'BOFA'}]
var threads = GmailApp.search('is:unread').forEach(function (thread){
thread.getMessages().forEach(function(message){
exceptions.forEach(function(exception){
if (message.getBody().toLowerCase().includes(exception.exc)){
labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
labeltoAssign.addToThread(thread)
} else if (message.getSubject().toLowerCase().includes(exception.exc)){
labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
labeltoAssign.addToThread(thread)
}
})
})}
);
}
非常感谢!
用我的 gmail 检查过,有效!
function myFunction() {
var labels=[]
GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
var threads = GmailApp.search('is:unread').forEach(function(email){
email.getMessages().forEach(function(thread){
//Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
//Example used - if body contains 'hi', mark as read.
labels.forEach(function(label){
if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
email.markRead()
}
})
})
});
}
更新: 非常感谢endothermic_dragon!你太聪明了!现在一切正常。
我以前创建过脚本,但从未为 Gmail 创建过。我很难创建一个脚本来过滤传入的电子邮件,如果它们有特定的词,则对其进行标记和存档。我们每天从不同的发件人处收到大约 100 封与一组公司有关的电子邮件。每个公司都有自己的标签。公司名称通常在电子邮件的主题或正文中。我正在尝试构建一个获取每封电子邮件的脚本,检查带有主题的数组或带有 plainBody 的数组是否包含来自 getUserLabels( ),如果是,则为其分配相应的标签。我已经尝试了一切,从 filter() 到 forEach() 到 indexOf() 但似乎没有任何效果。我觉得来自 getUserLabels() 和 getInoboxThreads() 的数组是二维的,我正在努力使用它们。我已经多次重写代码,但它让我无处可去。我真的希望你们中的一位能对此有所启发。
示例: 假设我们与 Apple、Amazon 和 Netflix 合作。这些公司中的每一个都有自己的标签。 如果收到一封电子邮件并且主题或正文中包含“Apple”,则为其添加标签“Apple”并将其存档。如果主题或正文中有“Amazon”和“Netflix”,则添加标签 Amazon 和 Netflix。
最终代码:
function labelEmails() {
var labels=[]
GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
var threads = GmailApp.search('is:unread').forEach(function(email){
email.getMessages().forEach(function(thread){
//Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
//Example used - if body contains 'hi', mark as read.
labels.forEach(function(label){
if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
labelToAssign = GmailApp.getUserLabelByName(label);
labelToAssign.addToThread(email)
} else if (thread.getSubject().toLowerCase().includes(label.toLowerCase())) {
labelToAssign = GmailApp.getUserLabelByName(label);
labelToAssign.addToThread(email)
}
})
})
});
}
我们的一些标签是公司名称的缩写,所以我还需要一个函数来处理这些标签。您也可以在下面查看: (代码中的公司为随机公司,并非真实公司)
function labelExceptions() {
var exceptions = [{exc:'apple', labeln: 'APPL'}, {exc: 'amazon', labeln: 'AMZ'}, {exc: 'Bank of America', labeln: 'BOFA'}]
var threads = GmailApp.search('is:unread').forEach(function (thread){
thread.getMessages().forEach(function(message){
exceptions.forEach(function(exception){
if (message.getBody().toLowerCase().includes(exception.exc)){
labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
labeltoAssign.addToThread(thread)
} else if (message.getSubject().toLowerCase().includes(exception.exc)){
labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
labeltoAssign.addToThread(thread)
}
})
})}
);
}
非常感谢!
用我的 gmail 检查过,有效!
function myFunction() {
var labels=[]
GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
var threads = GmailApp.search('is:unread').forEach(function(email){
email.getMessages().forEach(function(thread){
//Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
//Example used - if body contains 'hi', mark as read.
labels.forEach(function(label){
if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
email.markRead()
}
})
})
});
}