2025-02-12 Automation, Productivity

Automating Email Summaries with Google Apps Script

By O. Wolfson

Email management can become overwhelming, especially if you receive a high volume of messages daily. Instead of manually checking emails, you can automate the process by using Google Apps Script to collect, summarize, and send a daily email digest. This article will guide you through creating a script that fetches emails from your Gmail inbox and sends a summary to a designated recipient.

What is Google Apps Script?

Google Apps Script is a cloud-based scripting language that allows you to extend and automate Google Workspace applications, such as Gmail, Google Drive, and Google Sheets. With just a few lines of JavaScript-based code, you can create custom workflows that enhance productivity.

Setting Up the Email Summary Script

The script below searches for emails received in the last 24 hours, extracts key details (sender, subject, timestamp, and excerpt), and emails a summary to a designated address. The summary message is conditional it should read "No new emails" if there are no new emails, or "Here is a summary of new emails received for myaddress@mydomain.com in the last 24 hours:" if there are new emails.

Steps to Implement:

  1. Open Google Apps Script:

  2. Copy and paste the following code into the script editor:

javascript
function sendDailyEmailSummary() {
  var masterEmail = "mymasteremail@gmail.com";
  var now = new Date();
  var yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);

  var threads = GmailApp.search(
    "in:inbox after:" + Math.floor(yesterday.getTime() / 1000)
  );

  let messageBody = "";

  if (threads.length === 0) {
    messageBody = "<p>No new emails.</p>";
  } else {
    messageBody =
      "<p>Here is a summary of new emails received for myemail@mydomain.com in the last 24 hours:</p>";
    messageBody +=
      "<p><strong>Alternative Emails:</strong> otheremail@mydomain.com, sales@mydomain.com, support@mydomain.com, info@mydomain.com</p>";

    // if you have alternative emails, you can add them to the messageBody like this:
    // messageBody +=
    //   "<p><strong>Alternative Emails:</strong> otheremail@mydomain.com, sales@mydomain.com, support@mydomain.com, info@mydomain.com</p>";

    let count = 1;

    threads.forEach(function (thread) {
      var messages = thread.getMessages();
      messages.forEach(function (message) {
        if (!message.isDraft()) {
          var date = message.getDate();
          var subject = message.getSubject();
          var sender = message.getFrom();
          var body = message.getPlainBody();
          var excerpt = body
            ? body.substring(0, 200).replace(/\n/g, " ") + "..."
            : "[No preview available]";

          messageBody += `<p><strong>Message ${count}:</strong><br>
                          <strong>From:</strong> ${sender} <br>
                          <strong>Subject:</strong> ${subject} <br>
                          <strong>Received:</strong> ${date} <br>
                          <strong>Excerpt:</strong> ${excerpt}</p>`;

          count++;
        }
      });
    });
  }

  var now = new Date();
  var subject =
    "Daily Email Summary - " +
    now.toLocaleDateString() +
    " " +
    now.toLocaleTimeString();
  GmailApp.sendEmail(
    masterEmail,
    subject,
    messageBody.replace(/<[^>]*>/g, ""),
    {
      from: "myemail@mydomain.com",
      name: "My Name",
      htmlBody: messageBody,
    }
  );
}
  1. Customize the Script:

    • Replace your-email@example.com with your own email address.
    • Modify the search filter (in:inbox after:) as needed. You can adjust it to filter unread emails (is:unread) or specific labels (label:work).
  2. Set Up an Automated Trigger:

    • Click on the clock icon in Apps Script (Triggers).
    • Select sendDailyEmailSummary as the function to run.
    • Choose a time-based trigger (e.g., every day at 8 AM).

Enhancements

  • Filter Emails by Sender or Subject: Modify the search query to refine results:

    javascript
    var threads = GmailApp.search(
      "in:inbox after:" +
        Math.floor(yesterday.getTime() / 1000) +
        " from:important-client@example.com"
    );
    
  • Store Email Data in Google Sheets: Instead of sending an email, you can log email details in a Google Sheet for reference.

Conclusion

With Google Apps Script, automating email summaries is straightforward and can save you time managing your inbox. Whether you want a quick daily digest or a detailed report of new messages, this script provides an efficient solution for staying organized without manually sorting through emails.