Last active
December 16, 2015 20:39
-
-
Save AntoineAugusti/5494564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This function moves long emails in your Inbox to the Archive folder and warn the sender that you don't want to read too long emails. | |
// It could be very useful if you want to keep a clean inbox and spend less time reading and writring emails | |
// Just run it every 5 minutes (for example) and you'll be fine! | |
// | |
// Copyleft - Antoine AUGUSTI - www.antoine-augusti.fr - 2013 | |
// | |
function filterLongEmails() | |
{ | |
// Determine how much messages we want to target in the Inbox | |
var num_messages = 1; | |
// What is a "too long email" according to you? | |
var word_limit = 50; | |
// About the email that will be send if we find a "too long email" | |
var subject = "Shorter emails will be read"; | |
var content = "Hello, I am a pretty busy person and will appreciate if you can keep your email under " + word_limit + " words. Please edit and resend!"; | |
// Get the last num_messages messages in the Inbox | |
var threads = GmailApp.getInboxThreads(0, num_messages); | |
for (var i = 0; i < threads.length; i++) | |
{ | |
// Check if we have only a message in the current thread (so that we do not stop an ongoing conversation) | |
if (threads[i].isUnread() && threads[i].getMessageCount() == 1) | |
{ | |
var message = threads[i].getMessages()[0]; | |
var word_count = message.getBody().split(' ').length; | |
if (word_count > word_limit) | |
{ | |
// If the message is NOT from a "no reply" email address, we send a message | |
// If we reply to a "no reply" email address, we could have a reply from the server saying that this email address doesn't exist (and we definitely don't want that) | |
if (!message.getFrom().match(/no-reply|noreply/)) | |
{ | |
GmailApp.sendEmail(message.getFrom(), subject, content); | |
} | |
// Keep our inbox clean: move this mail to the Archive folder | |
GmailApp.moveThreadToArchive(threads[i]); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment