2016-11-23 136 views
3

我想爲某些參數設置一個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') 
} 

}

感謝,安德

+0

你想讓它通過你的收件箱嗎?或者當電子郵件進來?你到目前爲止嘗試過哪些代碼? – Brian

+0

所以我希望它經常運行以查找尚未過濾的新電子郵件。本機過濾器應該始終運行。我在高級函數中發現了一些過濾器設置:https://developers.google.com/gmail/api/v1/reference/users/settings/filters –

+0

我正在測試.getTo。我正試圖消除我的電子郵件和要檢查的電子郵件之間的差異。區別將是標籤。然後,一旦我有我想要的標籤,我會做出過濾器。我還沒有嘗試過濾器。 –

回答

5

的功能似乎存在,但需要啓用完整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('[email protected]', 'Aliases/foo') 
} 

我無法弄清楚如何,因爲谷歌沒有創建過濾器,自動追溯標籤的郵件似乎在他們的API中暴露了這一點。

+0

哇這是超級有用!非常感謝你!我從來沒有得到過這種優質的幫助。我會投票,但我沒有代表。 –

+0

我正計劃在我的收件箱中查找所有.to文件,將它們與我的電子郵件進行比較,以獲取標籤部分。然後檢查是否已爲其製作了標籤或過濾器。然後用「+」之後的標籤生成過濾器。如果您編輯了過濾器,則會有一個選項將其應用於整個電子郵件。如果我無法在創建標籤時找到將它們全部移到正確標籤的方法,就必須這樣做。 –

+0

所以我花了一段時間來試圖實現它的代碼,但我並不真正瞭解你在用IF語句做什麼。我看到你正在檢查是否已經創建過濾器,但我不知道如何?還有我運行的所有測試都返回了空標籤,所以我不確定如果我做錯了什麼。評論將有助於這是我第一次使用java腳本和谷歌腳本。 –