Table of Contents

Automated Gmail-to-PDF Archiving System

Archive Update

Purpose

This system automatically searches Gmail for emails sent to or from specific contacts or domains, converts them into PDFs, and stores them in named folders in Google Drive. It's used to keep a searchable, organized, and timestamped record of key communications for future reference, especially across insurance and claim-related projects.

What It Does

How It Works

  1. Create a new Google Apps Script project.
  2. Paste the provided script for the desired sender/domain (see below).
  3. Run the script and authorize permissions.
  4. The script auto-creates a Drive folder (if not existing) and fills it with PDFs.

Script Template

Replace @example.com or user@example.com with your target domain or address:

function archiveAllEmailsWithDomain() {
  var domain = "@example.com";
  var threads = GmailApp.search("from:" + domain + " OR to:" + domain);
 
  var folderName = "Gmail PDFs - Example (All Emails)";
  var folders = DriveApp.getFoldersByName(folderName);
  var folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folderName);
 
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var msg = messages[j];
      var sender = msg.getFrom();
      var recipient = msg.getTo();
      var date = Utilities.formatDate(msg.getDate(), "GMT", "yyyy-MM-dd");
      var subject = msg.getSubject().replace(/[^a-zA-Z0-9 ]/g, '').substring(0, 50);
 
      var direction = sender.includes(domain) ? "FROM" : "TO";
      var contact = sender.includes(domain) ? sender : recipient;
      var contactClean = contact.match(/<(.+)>/) ? contact.match(/<(.+)>/)[1] : contact;
 
      var filename = `${date} - ${direction} ${contactClean} - ${subject}.pdf`;
      var blob = Utilities.newBlob(msg.getBody(), "text/html", filename).getAs("application/pdf");
 
      folder.createFile(blob);
    }
  }
}

Configured Examples

Next Steps (Optional)


This system is designed for accuracy, automation, and fast retrieval of critical email communication records.

Email Organization Overview

This page documents how our Gmail-to-PDF email archiving system is structured for project organization, historical tracking, and AI analysis. Each category corresponds to a specific contact, domain, or role, with emails exported and stored in clearly named Google Drive folders.

Folder Structure and Categories

Notes on Script Execution

Scripts are designed to run one-time only (manually), and generate a PDF of each email retrieved by the Gmail query. All scripts follow the same structure: loop through threads → extract each message → convert to PDF → save in Drive folder.

Each script is mapped to a unique folder name for safe targeting and easy retrieval.

Special Case: Eric Powell Email Filtering

Eric Powell’s original archive script was missing some emails. This was due to Gmail’s `.getTo()` and `.getFrom()` fields sometimes being blank or incomplete in:

Gmail correctly returned the threads, but the script was skipping emails inside those threads based on this check: ```javascript if (!(sender.includes(email) || recipient.includes(email))) {

continue;

} ```

Fix: We replaced this logic with a cleaner, dual-check method: ```javascript var isFrom = sender.includes(email.toLowerCase()); var isTo = recipient.includes(email.toLowerCase()); if (!isFrom && !isTo) continue; ``` This ensures that every message directly involving Powell is captured — no more skipped BCCs or sent replies.

Other scripts (like domain-based searches for `@cna.com`) are not affected because they cast a wider net and don’t rely on a single recipient match.

Status

All seven archiving scripts are now finalized and verified:

We are ready to move to the summarization/AI review phase.