jasdefer
jasdefer

Reputation: 25

Gmail Script: Edit filter

Is it possible to edit or create filters in Gmail with a Google apps script? Background: I have a filter, that deletes emails from specified addresses. It is annoying to add addresses manually to the filter. Therefore I want to select an email, press a button to add the sender to the filter.

Upvotes: 1

Views: 712

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11268

It is not possible to create or edit filters inside Gmail using Google Scripts. You can however create a Google Script function that triggers every minute or so on incoming email and it will work like a filter.

Here's a snippet. You can refer to Advanced Gmail filters for more examples.

function gmailRule() {
  var blacklist = ["email1", "email2", "email3"];  
  var threads = GmailApp.search("is:unread label:inbox");
  for (var t in threads) {
    var msg = threads[t].getMessages()[0];
    if (msg.getFrom().indexOf(blacklist) !== -1)
      threads[t].moveToTrash();
  }
}

Upvotes: 2

Related Questions