我想爲某些參數設置一個gmail本機過濾器。使用Google Apps腳本在Gmail中創建本機過濾器
基本上我使用谷歌別名功能很多(郵件後面的+號)。我想自動創建一個讀取「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')
}
}
感謝,安德
你想讓它通過你的收件箱嗎?或者當電子郵件進來?你到目前爲止嘗試過哪些代碼? – Brian
所以我希望它經常運行以查找尚未過濾的新電子郵件。本機過濾器應該始終運行。我在高級函數中發現了一些過濾器設置:https://developers.google.com/gmail/api/v1/reference/users/settings/filters –
我正在測試.getTo。我正試圖消除我的電子郵件和要檢查的電子郵件之間的差異。區別將是標籤。然後,一旦我有我想要的標籤,我會做出過濾器。我還沒有嘗試過濾器。 –