« Back to home

Using script to archive Gmail to Evernote

Today, I was looking for a way to automatically save any emails that are labeled "Articles" to Evernote on my Gmail account.
I have filters to specifically target the email addresses that I've specified and automatically label them with "Articles". This makes it
convenient for me so everything is in one place when I have time to read them. I've been using Evernote lately and happen to like
how it takes a snapshot of articles that I want to be saved and read it (even if the original source goes away).

There's Zapier and IFTTT which I use for automating my Instagram, and other online services. I realized soon that
the emails being forwarded are in plain text which makes the articles unreadable and useless.

So the hunt continues for that service that will automate the archiving of articles from Gmail to Evernote.
While searching, I encountered this article in which he use Google Drive to create Google App scripts to interact
with Gmail. I've seen several people used the script before but never had an opportunity to play around with it until now.
I decided to give it a shot and seems to be a perfect opportunity to play with something new ;-).
It basically uses JavaScript to call Gmail classes and the Google script interface allows you to debug and build projects
so it can be automated on a scheduled basis.

The script below was taken from this article and modifying a few parameters should work out of the box. The "Misc/Articles" is
a nested label that I use to tag incoming emails from specific sources identified by a filter that I created. The "specialevernoteemail"
is a special email that Evernote assigned to you for convenience so you can send an email as a note to their service. You can find
this in your Evernote settings page. The last two strings are the hash tags that I want to use to identify those items in Evernote.

The 'forwardEmails' function is the entry point on the script which then calls the 'forwardThreads' function passing in the custom parameters.
The only issue that I've had was regarding the label. The label that is provided has to exist, otherwise, 'labels.getThreads()' will receive
a null which terminates the execution of the script.


  1. Go to your Google Drive and Create>Script (if Script is not available, you have to choose "connect more apps").
  2. This will open up the Google Script page which then you'll create a new "Blank Project".
  3. Paste the code below and pick "forwardEmails" as the function to execute, then hit run to test that it works properly.
  4. Click on the clock icon to create a trigger to schedule the function to run at specific times (hourly, etc).

The script

    function forwardEmails() {
      forwardThreads("Misc/Articles", "special_evernote_email@m.evernote.com", "@Articles/Subscriptions #archived #articles");

    function forwardThreads(label, addr, subjSuffix) {
      var maxSubjLength = 250; 
      var applylabel = GmailApp.getUserLabelByName("EN_Archive");

      // Send individual and threaded emails.
      var msgs, msg, i, j, subject, options, labels, page;
      labels = GmailApp.getUserLabelByName(label);
      var threads = labels.getThreads();
      for (i=0; i < threads.length; i++) {
        msgs = threads[i].getMessages();
        for (j=0; j < msgs.length; j++) {       
          msg = msgs[j];       
          subject = msg.getSubject();       
          if (subject.length + subjSuffix.length > maxSubjLength) {
            subject = subject.substring(0, maxSubjLength - subjSuffix.length);

          options = { htmlBody: msg.getBody(), attachments : msg.getAttachments() };

          GmailApp.sendEmail(addr, subject +" "+ subjSuffix, msg.getBody(), options);

      while(!page || page.length == 100) {
        /* Get the threads anywhere from 0 to 100. */
        page = labels.getThreads(0, 100);

        //pause to keep rates to gmail down (Goog apps errors)

        // Apply new label; move the thread out of other label


comments powered by Disqus