Skip to content

Commit

Permalink
Add readme and js file
Browse files Browse the repository at this point in the history
  • Loading branch information
PlanetEfficacy committed Sep 7, 2018
1 parent 09ee345 commit 76982aa
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# gmailFilter
A Google Apps Script for Creating Filters

## Usage
1. Set this script to run every 10 minutes.
2. Label an email in your gmail inbox as: `filter`

## About
When run, this script will go through your inbox and find all threads labeled 'filter'.
The script will get the sender of each thread marked 'filter' and create a new gmail filter to be applied to future emails from the same sender.
The filter will mark the email as read and archive it. Lastly, the script will apply these same actions to the thread labeled 'filter'.

## To Do
1. Better logging - e.g. to spreadsheet
2. More filter options - e.g. based on additional criteria than just the sender
3. Turn this into an add on
4. Generate reports with summaries of number of filters created over a period of time
5. Prevent duplicate filters from getting created
88 changes: 88 additions & 0 deletions createFilters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
This function is meant to run on a time-based trigger (I have it set for every 10 minutes).
When run, the function will create a filter
for all emails with the same sender as the email thread labeled 'filter'.
The filter will automatically mark all future emails from the sender as read and archive them.
The script will apply the same actions (mark as read, and archive) to the email labeled fitler.
For this function to run correctly you must enable the Advanced Gmail Service
see: https://developers.google.com/apps-script/advanced/gmail
*/
function createFilters() {
// Wrap the entire function in a try / catch, in case there is an error, log it.
try {

// Get the most recent 50 threads in your inbox
var threads = GmailApp.search("in:inbox", 0, 50);

// If there are threads
if (threads.length > 0) {

// For each thread
for (var t=threads.length-1; t>=0; t--) {

// Get the labels of the thread
var labels = threads[t].getLabels();

// For each label
for (var i in labels) {

// If the label name is 'filter'
// If you'd like to use a different filter name, just change the string 'filter' below to whatever you want.
if (labels[i].getName() == 'filter') {

// Get the current thread we are iterating over
var thread = threads[t];

// Get the first message in the thread
var message = thread.getMessages()[0];

// Get the sender's email address
var from = message.getFrom();

// Create a new filter object
var filter = Gmail.newFilter();

/*
Filters in gmail have criteria
The criteria is the basis on which the filter is applied (e.g. the rule that enables the filter's action).
In this case the criteria is that the email is sent from the same address as the sender of the current thread.
*/
filter.criteria = Gmail.newFilterCriteria();
filter.criteria.from = from;

/*
Filters in gmail have actions
The action is what will be performed when the criteria is met.
In this case the action is that the email is marked as read and archived.
*/
filter.action = Gmail.newFilterAction();
filter.action.removeLabelIds = ['INBOX', 'UNREAD'];

// Log that the filter was created
Logger.log("Creating Filter " + message.getDate() + "::" + message.getSubject() + " :: " + from);

/*
get the email address of the person whose account is running the script
In my case, this would be me. In your case, this will be your email.
We could also just use the string 'myemail@example.com', but I'm trying to avoid putting my personal email
in this code.
*/
var me = Session.getEffectiveUser().getEmail();

// Create the filter
Gmail.Users.Settings.Filters.create(filter, me)

// Mark the thread as read and archive it
thread.markRead().moveToArchive();
}
}
}
}
} catch (e) {
Logger.log(e.toString());
}
}

0 comments on commit 76982aa

Please sign in to comment.