User Tools

Site Tools


email_organization

This is an old revision of the document!


Automated Gmail-to-PDF Archiving System

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

  • Searches Gmail for all emails to or from a specific sender or domain.
  • Converts each email into a timestamped .pdf file using Google Apps Script.
  • Stores the PDFs in organized folders within Google Drive, named after the contact or domain.
  • Filenames follow this format: YYYY-MM-DD - TO/FROM email@domain.com - Subject.pdf

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

  • Shari Bradix (specific address): Shari.Bradix@cna.com
  • All CNA contacts: @cna.com
  • All J.S. Held engineers: @jsheld.com
  • Bockmon Insurance: @bockmoninsurance.com

Next Steps (Optional)

  • Add automatic scheduling to run these scripts daily/weekly.
  • Enhance script to export attachments or summarize content with AI.
  • Back up Drive folders to external storage/cloud systems.

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

email_organization.1742678917.txt.gz · Last modified: 2025/03/22 21:28 by lwattsii