Skip to content

Instantly share code, notes, and snippets.

@jesseh
Last active June 16, 2017 16:07

Revisions

  1. jesseh revised this gist Mar 11, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Gmail_to_RTM.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@ 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 (c) 2013(s), Jesse Heitler <jesseh ____ i-iterate.com>
    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
    @@ -27,7 +28,7 @@ PERFORMANCE OF THIS SOFTWARE.


    var LABEL_NAME = 'rtm'; // This string is the Gmail label that triggers a task to be created.
    var TARGET_EMAIL = '[email protected]';
    var TARGET_EMAIL = '[email protected]'; // 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

  2. jesseh renamed this gist Mar 11, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jesseh revised this gist Mar 8, 2013. No changes.
  4. jesseh created this gist Mar 8, 2013.
    103 changes: 103 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    /*
    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 (c) 2013(s), 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 = '[email protected]';
    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);
    };