====== 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 =====
- Create a new [[https://script.google.com|Google Apps Script]] project.
- Paste the provided script for the desired sender/domain (see below).
- Run the script and authorize permissions.
- 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 archiveEmailsByDomain(domain, folderLabel) {
var searchQuery = "from:" + domain + " OR to:" + domain;
var threads = GmailApp.search(searchQuery);
var folderName = "Gmail PDFs - " + folderLabel;
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 message = messages[j];
var sender = message.getFrom().toLowerCase();
var recipient = message.getTo().toLowerCase();
var isFrom = sender.includes(domain.toLowerCase());
var isTo = recipient.includes(domain.toLowerCase());
if (!isFrom && !isTo) continue;
var date = Utilities.formatDate(message.getDate(), "GMT", "yyyy-MM-dd");
var subject = message.getSubject().replace(/[^a-zA-Z0-9 ]/g, '').substring(0, 50);
var direction = isFrom ? "FROM" : "TO";
var contact = isFrom ? sender : recipient;
var match = contact.match(/<(.+)>/);
var email = match ? match[1] : contact;
var filename = `${date} - ${direction} ${email} - ${subject}.pdf`;
var blob = Utilities.newBlob(message.getBody(), "text/html", filename).getAs("application/pdf");
folder.createFile(blob);
}
}
}
===== Configured Example Functions =====
function archiveShariBradix() {
archiveEmailsByDomain("Shari.Bradix@cna.com", "Shari Bradix (Clean)");
}
function archiveCNA() {
archiveEmailsByDomain("@cna.com", "CNA (Clean)");
}
function archiveJsheld() {
archiveEmailsByDomain("@jsheld.com", "J.S. Held (Clean)");
}
function archiveBockmon() {
archiveEmailsByDomain("@bockmoninsurance.com", "Bockmon Insurance (Clean)");
}
function archiveMarshallTexas() {
archiveEmailsByDomain("@marshalltexas.net", "Marshall Texas (Clean)");
}
function archiveEricPowell() {
archiveEmailsByDomain("Powell.eric@marshalltexas.net", "Eric Powell (Clean)");
}
===== 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.''