Gmail Processor
website·Indexed 31 days ago
https://ahochsteger.github.io/gmail-processor/A tool for processing Gmail threads, messages, and attachments, featuring capabilities to store attachments to Google Drive, extract text, and decrypt PDFs. It supports a configurable system of global and attachment actions, string and date expressions, regular expressions, and environment placeholders for string substitution.
What's inside Gmail Processor
- Gmail Processor is an open-source Google Apps Script library written in TypeScript that automates the processing of Gmail messages and attachments. It uses a JSON configuration to match threads, messages, and attachments against specific criteria and trigger corresponding actions, such as storing attachments in Google Drive or logging data to Google Spreadsheets.
Understand gmail-processor action processing scopes
Actions in gmail-processor are triggered within specific processing scopes, which determine the context available to the action:
global.*: Globally available; can be placed anywhere in the configuration.thread.*: Runs in the context of a thread (includesmessageandattachmentcontext).message.*: Runs in the context of a message (includesattachmentcontext).attachment.*: Runs specifically in the context of an attachment.
Identify context types using ContextType
The
ContextTypeenum identifies the type of context available for processing:attachment: Contains attachment configuration and info about the current attachment.env: Contains environment information and references to environment objects.message: Contains message configuration and info about the current message.proc: Contains processing info (configuration, integration adapters, action registry, and timer).thread: Contains thread configuration and info about the current thread.
Automate Gmail processing and actions
The Gmail Processor allows you to automate the handling of emails by using a configuration to match specific threads, messages, and attachments, which then triggers defined actions based on those matches.Use Attachment placeholders during Gmail attachment processing
The following placeholders are available when the gmail-processor is processing a Gmail attachment. These can be used to access metadata about the file or capture specific parts of the filename and content type using regex match groups defined in the match configuration.Key Description Example attachment.contentTypeThe content type of the attachment "application/pdf" attachment.contentType.match.1Regex match group 1 from content type "pdf" attachment.contentType.match.appTypeNamed regex match group appTypefrom content type"pdf" attachment.hashSHA1 content hash of the attachment "aa0b8cc192a5d8d5b5d8ecda24fd096..." attachment.index0-based index of the attachment "0" attachment.isGoogleTypetrueif the attachment is a Google Workspace file"false" attachment.matchedOverall matching result for all conditions in match config "true" attachment.nameThe name of the attachment "attachment1.pdf" attachment.name.match.1Regex match group 1 from attachment name "1" attachment.name.match.attNrNamed regex match group attNrfrom attachment name"1" attachment.sizeThe size of the attachment "18" attachmentConfig.index0-based index of the attachment configuration "0" Set up the gmail-processor development environment
To start developing for gmail-processor, clone the repository and set up your environment using either Devbox for a consistent toolset or a traditional Node.js installation. Ensure you have Node.js 22+ installed if not using Devbox.# 1. Clone the repository git clone https://github.com/ahochsteger/gmail-processor.git cd gmail-processor # 2. Setup environment # Option A: Using Devbox (Recommended) devbox shell # Option B: Traditional (Ensure Node.js 22+ is installed) # 3. Install dependencies npm run all:reinstallExtract values from email subjects using Regular Expressions
You can use named capture groups in a regular expression within thematch.subjectconfiguration to extract specific data from an email's subject line. Extracted values are accessible via themessage.subject.matchobject. For example, a capture group defined as(?<school>...)will be available as{{message.subject.match.school}}.{ "messages": [ { "name": "extract-message-fields-from-subject", "match": { "subject": "regularExpressions (?<school>[^-]+)-(?<reportType>[^-]+)-(?<reportSubType>[^-]+)-(?<date>[0-9-]+)" }, "attachments": [ { "actions": [ { "name": "attachment.store", "args": { "location": "/Reports/{{message.subject.match.school}}/All Reports/{{message.subject.match.reportType}}/{{message.subject.match.reportSubType}}/{{message.subject.match.date}}-{{attachment.name}}" } } ] } ] } ] }Migrate configuration validation to Zod v4
As of version 2.17.4, the project has migrated its configuration validation and serialization fromclass-transformerandajvto Zod v4. Developers should update any custom configuration schemas or validation logic to use Zod v4 syntax.// Example Zod v4 schema usage import { z } from 'zod'; const ConfigSchema = z.object({ apiKey: z.string().min(1), timeout: z.number().default(3000), }); type Config = z.infer<typeof ConfigSchema>;Store specific Gmail attachments to Google Drive
This example demonstrates how to filter for unread threads with attachments containing a specific subject, filter for messages sent by the user, and store attachments named 'invoice.pdf' to a dynamic path in Google Drive using placeholder substitution.{ "description": "Use this to get started with Gmail Processor. It stores an attachment to Google Drive.", "settings": { "markProcessedMethod": "mark-read" }, "global": { "thread": { "match": { "query": "has:attachment -in:trash -in:drafts -in:spam after:{{date.now|formatDate('yyyy-MM-dd')}} is:unread subject:\"[GmailProcessor-Test] simple\"" } } }, "threads": [ { "match": { "query": "from:${user.email}" }, "messages": [ { "attachments": [ { "match": { "name": "^invoice\\.pdf$" }, "actions": [ { "name": "attachment.store", "args": { "location": "/GmailProcessor-Tests/e2e/{{date.now|formatDate('yyyy-MM-dd')}}/basics/simple/{{message.subject}}-{{attachment.name}}", "conflictStrategy": "keep" } } ] } ] } ] } ] }Automate Gmail processing with Gmail Processor
Gmail Processor provides several core capabilities for email automation:
- Google Drive Integration: Store attachments, PDFs of messages, or entire threads in Google Drive.
- OCR Text Extraction: Extract text (e.g., invoice numbers) from JPEG, PNG, GIF, and PDF attachments.
- Spreadsheet Logging: Log information about processed threads, messages, and attachments into a Google Spreadsheet.
- JSON-based Configuration: Define matching rules and actions via a flexible JSON configuration.
Manually copy GmailProcessor library code (Advanced)
For users who need full control over the execution or wish to modify the library code, the library can be copied directly into the project instead of using a reference:
- Create a new Script file in your project (e.g.,
GmailProcessorLib.gs). - Replace the file contents with the code from the latest GmailProcessorLib.js release asset.
- Create a new Script file in your project (e.g.,
Access extracted attachment text in subsequent actions
Values extracted by theattachment.extractTextaction are stored in theattachment.extracted.matchobject. You can reference these named capture groups using double-curly brace syntax in later actions.{ "name": "attachment.store", "args": { "location": "/folder/{{attachment.name.match.basename}}-number-{{attachment.extracted.match.invoiceNumber}}.pdf", "description": "Invoice number: {{attachment.extracted.match.invoiceNumber}}" } }