Read a Gmail message resource (as returned by get-message with
format: "full", the default): headers, the plain-text and HTML
bodies, and the attachment list, by walking the MIME parts tree.
Gmail returns part data base64url encoded; these helpers decode it.
All functions are pure.
to-email turns a message into a flat Email record, the shape
agents and workflows usually want.
Body text is decoded as UTF-8. Gmail undoes the transfer encoding
but not the charset, so a part in another charset (e.g.
iso-8859-1) may show replacement characters; check charset.
Functions
all-parts
fn (message: Map): Vec
Every MIME part of a message, depth first, starting with the top-level
payload. Empty for format: "raw" / "minimal" messages.
Example
message {payload: {partId: "", parts: [{partId: "0"}, {partId: "1"}]}}
map(::google::gmail::parse/all-parts(message), %.partId) // ["", "0", "1"]
::google::gmail::parse/all-parts({raw: "SGk"}) // []
attachments
fn (message: Map): Vec
Every attachment of a message (a Vec of Attachment), found by walking
the parts tree.
Example
message {id: "m1", payload: {parts: [
{partId: "0", mimeType: "text/plain", filename: "", body: {data: "SGk"}},
{partId: "1", mimeType: "application/pdf", filename: "report.pdf", body: {attachmentId: "att1", size: 1024}}
]}}
found ::google::gmail::parse/attachments(message)
length(found) // 1
first(found).filename // "report.pdf"
first(found).attachment-id // "att1"
charset
fn (message: Map): Str?
The declared charset of the plain-text body (else the HTML body), lowercased, or null.
Example
message {payload: {mimeType: "text/plain", headers: [
{name: "Content-Type", value: "text/plain; charset=\"ISO-8859-1\""}
]}}
::google::gmail::parse/charset(message) // "iso-8859-1"
decode-data
fn (data: Any): Bytes
Decode a Gmail base64url data string (padded or not) to Bytes; null or
"" give empty Bytes.
Example
Str(::google::gmail::parse/decode-data("SGVsbG8")) // "Hello"
length(::google::gmail::parse/decode-data(null)) // 0
header
fn (message: Map, name: Str): Str?
One header value by case-insensitive name (first occurrence), or null:
header(message, "Subject").
Example
message {payload: {headers: [
{name: "Subject", value: "Q3 plan"},
{name: "Received", value: "by a"},
{name: "Received", value: "by b"}
]}}
::google::gmail::parse/header(message, "subject") // "Q3 plan"
::google::gmail::parse/header(message, "X-Missing") // null
header-values
fn (message: Map, name: Str): Vec
Every value of a header, in order (e.g. all Received lines).
Example
message {payload: {headers: [
{name: "Subject", value: "Q3 plan"},
{name: "Received", value: "by a"},
{name: "Received", value: "by b"}
]}}
::google::gmail::parse/header-values(message, "Received") // ["by a", "by b"]
headers
fn (message: Map): Map
The message's (or a part's) headers as a Map of lowercased name to
value; the first occurrence wins. Accepts a message resource or a
payload part.
Example
message {payload: {headers: [
{name: "Subject", value: "Q3 plan"},
{name: "Received", value: "by a"},
{name: "Received", value: "by b"}
]}}
::google::gmail::parse/headers(message) // {subject: "Q3 plan", received: "by a"}
html-body
fn (message: Map): Str?
The first text/html body (not an attachment), decoded to a Str, or
null.
Example
message {payload: {mimeType: "multipart/alternative", parts: [
{mimeType: "text/plain", body: {data: "SGVsbG8"}},
{mimeType: "text/html", body: {data: "PHA-SGk8L3A-"}}
]}}
::google::gmail::parse/html-body(message) // "<p>Hi</p>"
is-attachment
fn (part: Map): Bool
True for a part that is a file: it has a filename, or
Content-Disposition: attachment.
Example
::google::gmail::parse/is-attachment({filename: "report.pdf", mimeType: "application/pdf"}) // true
::google::gmail::parse/is-attachment({filename: "", mimeType: "text/plain"}) // false
param
fn (value: Str?, name: Str): Str?
A parameter from a header value such as text/plain; charset="utf-8":
param(value, "charset") is "utf-8". Case-insensitive; null when
absent.
Example
::google::gmail::parse/param("text/plain; charset=\"utf-8\"", "charset") // "utf-8"
::google::gmail::parse/param("text/plain", "charset") // null
text-body
fn (message: Map): Str?
The first text/plain body (not an attachment), decoded to a Str, or
null.
Example
message {payload: {mimeType: "multipart/alternative", parts: [
{mimeType: "text/plain", body: {data: "SGVsbG8"}},
{mimeType: "text/html", body: {data: "PHA-SGk8L3A-"}}
]}}
::google::gmail::parse/text-body(message) // "Hello"
to-email
fn (message: Map): Email
Flatten a message resource into an Email.
// A resource from ::google::gmail::messages/get-message
message {id: "m1", labelIds: ["INBOX", "UNREAD"], payload: {
mimeType: "text/plain",
headers: [{name: "From", value: "Ada <ADA@example.com>"}, {name: "Subject", value: "Q3 plan"}],
body: {data: "SGVsbG8"}
}}
email ::google::gmail::parse/to-email(message)
email.from-email // "ada@example.com"
email.subject // "Q3 plan"
email.text // "Hello"
email.unread // true
walk-parts
fn (part: Map): Vec
A part and all its descendants, depth first (pure).
Example
tree {partId: "0", parts: [{partId: "0.0", parts: [{partId: "0.0.0"}]}, {partId: "0.1"}]}
map(::google::gmail::parse/walk-parts(tree), %.partId) // ["0", "0.0", "0.0.0", "0.1"]
Types
Attachment
Attachment type {
message-id: Str?,
part-id: Str?,
filename: Str,
mime-type: Str?,
size: Int?,
attachment-id: Str?,
data: Bytes?,
content-id: Str?,
inline: Bool
}
A file attached to a message.
message-id,part-id,filename,mime-type,size(bytes)attachment-id- pass to::google::gmail::messages/get-attachment(null when the data is inline in the part)data- the bytes when Gmail included them inline, else nullcontent-id- for inline images referenced ascid:in HTMLinline-Content-Disposition: inline
Email
Email type {
id: Str,
thread-id: Str?,
history-id: Str?,
label-ids: Vec,
snippet: Str?,
message-id: Str?,
from: Str?,
from-email: Str?,
to: Str?,
to-emails: Vec,
cc: Str?,
cc-emails: Vec,
reply-to: Str?,
subject: Str?,
date: Str?,
internal-date: Int?,
unread: Bool,
starred: Bool,
in-inbox: Bool,
text: Str?,
html: Str?,
attachments: Vec,
raw: Map
}
A Gmail message flattened for workflows and agents.
id,thread-id,history-id,label-ids,snippetmessage-id- the RFC 5322Message-IDheaderfrom,to,cc,reply-to,subject,date- header values as Gmail returns themfrom-email- the sender's address, lowercasedto-emails,cc-emails- recipient addresses, lowercasedinternal-date- when Gmail received it (epoch ms, Int)unread,starred,in-inboxtext,html- decoded bodies (null when absent)attachments-Attachmentsraw- the message resource