Skip to main content
When an email arrives on your inbound domain, it is received and parsed by SparkPost, the mail infrastructure behind Lettr’s inbound servers, and posted to your forwarding URL as JSON. This page explains every field in that payload and how to read the parts of the email that are not broken out into fields, such as attachments.

Payload Structure

Inbound emails are posted to your inbound domain’s forwarding URL as a JSON array of events. Each event contains a relay message with the email’s content:
Payloads are always a JSON array of events. A single delivery may contain one or more events, so loop over the array instead of reading only the first item.

Field Reference

There is no attachments field. Attachments are only available inside email_rfc822. See Raw MIME Parsing and Attachments.

Reading the Payload

The examples on the rest of this page assume relay is one event.msys.relay_message object.

Sender Information

From Address

The relay message provides two sender fields:
  • msg_from: the envelope sender (SMTP MAIL FROM). For mailing lists and bulk senders this is often a bounce address, not the person who wrote the email.
  • friendly_from: the display name and address from the From header. This is usually the address you want to show or reply to.
To extract just the email address or display name from friendly_from:

Reply-To Address

The Reply-To address is in the content.headers array, when the sender set one (getHeader is defined under Email Headers):

Recipients

The relay message provides:
  • rcpt_to: the envelope recipient, the address on your inbound domain that received the email
  • content.to: the addresses from the To header
  • content.cc: the addresses from the Cc header, when there are any
Use rcpt_to to decide which of your addresses the email was meant for. It can differ from the To header, for example when your address was in Bcc or the email was forwarded.
BCC recipients are not visible in received emails. If your address was BCC’d, it appears only in rcpt_to.

Subject Line

The subject is in content.subject:
The original header is also in content.headers under Subject.

Email Body

Plain Text and HTML

Emails can contain plain text, HTML, or both. The bodies are in content.text and content.html, and either one can be missing:

Extracting Text from HTML

Handling Quoted Content

Strip quoted replies to get just the new content:

Email Headers

Standard Headers

Headers are in content.headers as an array of objects, each with a single key (the header name) and a string value. A header that appears several times in the email, such as Received, appears several times in the array. Which headers are present depends on the sender. Common ones: Header names keep the capitalization the sender used (Message-ID, Message-Id), so look them up case-insensitively:
Message-ID is also the right key for deduplication: the same email can be delivered to your endpoint more than once, so store the Message-IDs you have processed and skip repeats.

Custom Headers

Access any header using the helper:

Date and Time

Sent Timestamp

The original send time is in the Date header:
The Date header is set by the sender’s client and can be missing or wrong. Record your own received time as well.

Character Encoding

content.subject, content.text and content.html are JSON strings, so you read them as ordinary Unicode text. If you need exact control over how a part’s declared charset is decoded, parse email_rfc822 with a MIME library, which reads each part’s charset and Content-Transfer-Encoding itself.

Multipart Emails

Most emails are multipart: a plain text version, an HTML version, and possibly attachments and inline images. The payload breaks out only the bodies:
Attachments and inline images are not extracted into separate fields. To get them, parse email_rfc822.

Raw MIME Parsing

content.email_rfc822 holds the complete raw message. Parse it with a MIME library when you need attachments, inline images, or MIME structure that the structured fields don’t cover. When content.email_rfc822_is_base64 is true, base64-decode the field first.
See Attachments for extracting, validating and storing files.

Parsing Examples

Extract Email Signature

Detect Email Client

Parse Structured Data

Handling Edge Cases

Empty Content

An email with an empty body can still carry attachments, so check email_rfc822 before discarding it.

Malformed Addresses

Inbound Setup

Create an inbound domain and forwarding URL

Attachments

Extract files from the raw message

Reply Tracking

Track conversation threads

Routing

Route emails based on content