Header Matching
This example demonstrates how to use the rawHeaders
property in the message matching configuration.
It allows matching messages based on the content of their raw headers using regular expressions.
In this specific example:
- It first matches threads sent by the current user (
from:${user.email}
). - Within those threads, it matches messages where the raw headers contain a
From:
line exactly matching the user's email address ((?m)^From: ${user.email}$
). Note the use of(?m)
at the beginning of the regex to allow matching^
and$
at start/end of line (multi-line matching). - For matching messages, it processes attachments named
invoice.pdf
. - Matching attachments are stored in Google Drive using a dynamically generated path based on message and attachment details.
👉 Edit this example in the playground.
- Config
- Script
{
"description": "This example demonstrates how to match raw message headers.",
"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] headerMatching\""
}
}
},
"threads": [
{
"match": {
"query": "from:${user.email}"
},
"messages": [
{
"match": {
"rawHeaders": "(?m)^From: ${user.email}$"
},
"attachments": [
{
"match": {
"name": "^invoice\\.pdf$"
},
"actions": [
{
"name": "attachment.store",
"args": {
"location": "/GmailProcessor-Tests/e2e/headerMatching/{{message.date|formatDate('yyyy-MM-dd')}}/{{message.subject}}-{{attachment.name}}",
"conflictStrategy": "keep"
}
}
]
}
]
}
]
}
]
}
function headerMatchingRun() {
const config = {
description: "This example demonstrates how to match raw message headers.",
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] headerMatching\"",
},
},
},
threads: [
{
match: {
query: "from:${user.email}",
},
messages: [
{
match: {
rawHeaders: "(?m)^From: ${user.email}$",
},
attachments: [
{
match: {
name: "^invoice\\.pdf$",
},
actions: [
{
name: "attachment.store",
args: {
location:
"/GmailProcessor-Tests/e2e/headerMatching/{{message.date|formatDate('yyyy-MM-dd')}}/{{message.subject}}-{{attachment.name}}",
conflictStrategy: "keep",
},
},
],
},
],
},
],
},
],
}
return GmailProcessorLib.run(config, "dry-run")
}
Source: headerMatching.ts