Skip to content

Instantly share code, notes, and snippets.

@jesseh
Last active June 16, 2017 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jesseh/5117006 to your computer and use it in GitHub Desktop.
Save jesseh/5117006 to your computer and use it in GitHub Desktop.
Create Remember The Milk tasks from Gmail threads by simply adding a label. A Google App Script
/*
This script watches a gmail account for any thread labeled with 'rtm'. When such a thread is found it
sends a task-creation email to Remember the Milk that includes a link back to the original thread.
To follow the link back to the email you have to be logged in to the originating Gmail account (which
is only an issue if you have multiple gmail accounts). Otherwise Google claims the email cannot be
found.
To install it go into Google Drive and create a new "Script" (the link is a bit hidden in the create submenu).
Copy this code into the new project and set a time-based trigger.
Copyright 2013, Jesse Heitler <jesseh i-iterate.com>
@
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
var LABEL_NAME = 'rtm'; // This string is the Gmail label that triggers a task to be created.
var TARGET_EMAIL = 'CHANGE_ME_TO_INBOX_ADDRESS@rmilk.com'; // From your RTM settings.
var RTM_LIST = ''; // Use to post the message to a specific RTM list
var RTM_TAGS = ''; // Use to add tags to the task in RTM
/**
* Process all the threads with a given label and remove that label
* and archive it so that it is not processesed again.
**/
function processLabel(labelName) {
var label = GmailApp.getUserLabelByName(labelName);
var threads;
var thread;
if (label) {
threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
thread = threads[i];
handleThread(thread);
thread.removeLabel(label);
thread.moveToArchive();
}
}
};
/**
* Get from whom was the most recent unread, or the latest message in a thread.
**/
function threadFrom(thread) {
var message;
var messages = thread.getMessages();
var from = messages[messages.length - 1].getFrom();
for(var i = 0; i < messages.length; i++) {
message = messages[i];
if (message.isUnread()) {
from = message.getFrom();
}
}
return from;
};
function emailTask(details) {
var recipient = TARGET_EMAIL;
var subject = "Reply to '" + details['subject'] + "' " + details['from'] + " (" + details['count'] + ')';
var body = "";
if (details['priority']) {
body += "Priority: 3\n";
}
if (RTM_TAGS) {
body += "Tags: " + RTM_TAGS + "\n";
}
// body += "URL:" + details['permalink'] + "\n";
body += "URL:https://mail.google.com/mail/?view=cv&fs=1&th=" + details['threadId'] + "&search=all\n"
if (RTM_LIST) {
body += "List: " + RTM_LIST + "\n";
}
Logger.log("Sent email with subject: " + subject + "\nBody:\n" + body);
GmailApp.sendEmail(recipient, subject, body)
};
function handleThread(thread) {
var details = {
threadId: thread.getId(),
subject: thread.getFirstMessageSubject(),
count: thread.getMessageCount(),
permalink: thread.getPermalink(),
priority: thread.hasStarredMessages(),
from: threadFrom(thread)
};
emailTask(details)
};
function main() {
var labelName = LABEL_NAME;
processLabel(labelName);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment