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 < count; i++) index.push(i);
  }

  for (i = 0; i < TOTAL; i++) {
    var n = index[i];

    if (emails[n]) {
      thread = emails[n].getMessages()[0];

      subject = thread.getSubject();
      body = processHTML(thread.getBody(), 250);
      link = thread.getId(); // can also use GetPermalink()
      from = thread.getFrom();
      date = Utilities.formatDate(thread.getDate(), Session.getTimeZone(), 'MMM dd, yyyy');

      if (i % 2 == 0) color = '#f0f0f0';
      else color = '#f9f9f9';

      html += '<p>On ' + date + ', <i>' + from + '</i> wrote: ';
      html += '<strong>' + subject + '</strong><br /><br />';
      html += body + " <a href='https://mail.google.com/mail/#all/";
      html += link + "'>Click to read &raquo;</a></p>";
    }
  }

  html += "<p><a href='" + SpreadsheetApp.getActiveSpreadsheet().getUrl();
  html += "'>click here</a> and choose Gmail &gt; unsubscribe.</p> ";

  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);
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.