// Quotebook Inbox for Gmail (plan T-110 PR-03, T-111). Paste this into a new project at // https://script.google.com and run `setup` once. Nothing to edit: the vendor domains come from the // file quotebook-vendors.txt that Quotebook keeps in the Drive folder "Quotebook Inbox" (from your // projects), so adding a vendor in Quotebook is all it takes. // It runs in your own Google account only: every 15 minutes it saves new mail from those domains // as .eml files into that folder. It sends nothing anywhere, and Quotebook's servers never see your // mail. Quotebook (in Chrome or Edge) reads that folder on your PC through Google Drive for desktop. const FOLDER_NAME = "Quotebook Inbox"; const VENDOR_FILE = "quotebook-vendors.txt"; const RECENT_DAYS = 2; // each 15-minute run looks this far back; already-saved mail is skipped const BACKFILL_DAYS = 365; // `backfill` looks this far back const PAGE_THREADS = 100; // threads asked for at a time const MAX_RUN_MS = 4.5 * 60 * 1000; // a run stops itself here, inside Apps Script's 6-minute limit /** Run once: makes the folder, schedules `saveRecent` every 15 minutes and saves the last days. */ function setup() { ScriptApp.getProjectTriggers() .filter((t) => t.getHandlerFunction() === "saveRecent") .forEach((t) => ScriptApp.deleteTrigger(t)); ScriptApp.newTrigger("saveRecent").timeBased().everyMinutes(15).create(); saveRecent(); } /** Scheduled by `setup`: saves vendor mail from the last RECENT_DAYS days. */ function saveRecent() { saveVendorMail_(RECENT_DAYS); } /** Optional, run by hand: saves up to a year of past vendor mail. A run that ends with "run * backfill again" ran out of time: run it again, until the log says "All past mail checked". */ function backfill() { saveVendorMail_(BACKFILL_DAYS); } function saveVendorMail_(days) { const folder = folder_(); const domains = vendorDomains_(folder); if (!domains.length) { console.log("No vendor domains yet: open Quotebook, choose this folder on Mail sync, and add vendor domains to a project."); return; } const query = "(" + domains.map((d) => "from:" + d).join(" OR ") + ") newer_than:" + days + "d"; const result = saveThreads_(query, folder, domains, Date.now() + MAX_RUN_MS); console.log(result.finished ? result.saved + " saved. All past mail checked." : result.saved + " saved so far. Out of time: run backfill again to carry on."); } /** Every thread the search finds, a page at a time, newest first. Gmail's search returns one page * per call, so stopping at the first page (as this script once did) saved the newest PAGE_THREADS * threads and never reached older mail, however often it was run. Mail already in the folder is * skipped without being read again, which is what lets a second run carry on past it. `search` * and `now` are passed in by the tests; the script itself uses Gmail and the clock. */ function saveThreads_(query, folder, domains, deadline, search, now) { const find = search || ((q, start, max) => GmailApp.search(q, start, max)); const clock = now || (() => Date.now()); let saved = 0; for (let start = 0; ; start += PAGE_THREADS) { const threads = find(query, start, PAGE_THREADS); for (const thread of threads) { if (clock() > deadline) return { saved: saved, finished: false }; saved += saveThread_(thread, folder, domains); } if (threads.length < PAGE_THREADS) return { saved: saved, finished: true }; } } function saveThread_(thread, folder, domains) { let saved = 0; for (const message of thread.getMessages()) { if (!fromVendor_(message.getFrom(), domains)) continue; const name = Utilities.formatDate(message.getDate(), "UTC", "yyyy-MM-dd-HHmmss") + "-" + message.getId() + ".eml"; if (folder.getFilesByName(name).hasNext()) continue; folder.createFile(Utilities.newBlob(message.getRawContent(), "message/rfc822", name)); saved++; } return saved; } /** Line 2 of the vendor file is |d1|d2|…|, written by Quotebook; anything not a plain domain is * ignored. */ function vendorDomains_(folder) { const files = folder.getFilesByName(VENDOR_FILE); if (!files.hasNext()) return []; const line = files.next().getBlob().getDataAsString().split(/\r?\n/).find((l) => l.indexOf("|") === 0) || ""; return line.split("|").map((d) => d.trim().toLowerCase()).filter((d) => /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(d)); } function fromVendor_(from, domains) { const match = /<([^>]+)>/.exec(from); const domain = (match ? match[1] : from).trim().toLowerCase().split("@")[1] || ""; return domains.some((d) => domain === d || domain.endsWith("." + d)); } function folder_() { const found = DriveApp.getFoldersByName(FOLDER_NAME); return found.hasNext() ? found.next() : DriveApp.createFolder(FOLDER_NAME); }