Email Newsletters with Google Apps Script

Published in: Gmail - 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 TOTAL = 10;

function Install() {

function readStarredMessages() {

    var thread, subject, link, body, from,
        date, html, emails, color, index = [],

    var mySheet = SpreadsheetApp.getActiveSpreadsheet();

    emails ="label:" + LABEL);
    var count = emails.length;

    if (count == 0)

    if (count > TOTAL)
        index = getIndex(TOTAL, 0, count);
    else {
        for (i = 0; i < count; 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='";
            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> ";

        emails.length + " pending messages in Gmail", "", {
            htmlBody: html

// Pick random messages from the Gmail label

function getIndex(count, min, max) {
    var results = [],
    while (count > 0) {
        randNumber = Math.round(min + Math.random() * (max - min));
        if (results.indexOf(randNumber) == -1) {
    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);
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube