Categories
Code

Email Newsletters with Google Apps Script

This Google Script sends a daily newsletter containing a summary of your Starred e-mails in Gmail. You can extend it to attach other information like RSS feeds, summary reports, etc.

 
var LABEL = "STARRED";
var TOTAL = 10;

function Install() {
  ScriptApp.newTrigger("readStarredMessages").timeBased().everyDays(1).create();  
}

function readStarredMessages() {
  
  var thread, subject, link, body, from, 
      date, html, emails, color, index = [], i;

  var mySheet = SpreadsheetApp.getActiveSpreadsheet();  

  emails = GmailApp.search("label:" + LABEL);  
  var count = emails.length;
  
  if (count == 0)
    return;
  
  if (count > TOTAL) 
    index  = getIndex(TOTAL, 0, count);
  else {    
    for (i=0; i" + from + " wrote: ";
      html += "" + subject + "

"; html += body + " Click to read »

"; } } html += "

click here and choose Gmail > unsubscribe.

"; GmailApp.sendEmail(Session.getActiveUser(), emails.length + " pending messages in Gmail", "", { htmlBody: html }); } // Pick random messages from the Gmail label function getIndex(count, min, max) { var results = [], index; while ( count > 0) { randNumber = Math.round(min + Math.random() * (max - min)); if (results.indexOf(randNumber) == -1) { results.push(randNumber); count--; } } return results; } // Remove HTML tags from the Gmail messages function processHTML(html, count) { html = html.replace(/<(?:.|\n)*?>/gm, ''); html = html.replace(/^\s+|\s+$/g, ''); return html.substring(0, count); }