email-service
A serverless service that sends emails for the Pennsieve platform.
Producers (other Pennsieve services) request an email by putting a message on an
SQS queue. They specify which email (a slug-style messageId), who it
goes to, and the values to fill into the template. A Lambda consumer renders
the template and delivers via SES. Producers never call SES themselves —
templating, branding, delivery, idempotency, and the audit trail all live here.
How it works
producer (Go/Scala client) email-service AWS
────────────────────────── ───────────── ───
EmailRequest{messageId, recipients, context}
│ enqueue (SQS SendMessage)
▼
┌─────────────┐ SQS ┌────────────────────────────┐
│ send queue │ ───────▶ │ queue lambda (per record): │
└─────────────┘ │ │
│ 1. look up messageId ──────┼──▶ DynamoDB email-message-templates
│ → template file + subject │ (messageId → file, default subject)
│ 2. fetch template ──────┼──▶ S3 custom/O{orgId}/{file}
│ (org branding, else │ → fallback default/{file}
│ default) │
│ 3. render body + subject │ (Go html/template; subject text/template)
│ 4. per recipient: │
│ claim → send → mark ─────┼──▶ DynamoDB email-message-log (journal)
│ │──▶ SES SendEmail
└──────────────┬───────────────┘
│ failures (after retries)
▼
┌─────────────┐
│ dead-letter │
└─────────────┘
Per SQS record the consumer:
- Resolves the template — looks up
messageId in the email-message-templates
table to get the template file name and the default subject.
- Fetches the template from S3 — tries the org-branded path
custom/O{organizationId}/{file} first (when context.organizationId is
set), falling back to default/{file}.
- Renders the body (Go
html/template, auto-escaped) and the subject
(text/template; a context.subject overrides the default).
- Sends and journals, per recipient — a conditional write claims the
send (the idempotency guard), then SES delivers, then the row is marked
SENT/FAILED. If a send control is active the delivery is
skipped and the row is marked log-only instead — every request is journaled
regardless.
Idempotency & retries. Each (message, recipient) has a dedupe key. The claim
is a conditional PutItem: a duplicate SQS redelivery of an already-SENT/
QUEUED row is skipped (no double-send), while a FAILED row is allowed to be
retried. The consumer reports per-record batch-item failures, so one bad message
doesn't fail a whole batch; messages that keep failing land in the dead-letter
queue.
Templates live in a separate repo — see Templates below.
Using the clients
Producers should use a client rather than build the SQS message by hand — the
clients own the queue wiring, JSON contract, dedupe id, and typed per-message
builders. Both clients produce the same wire payload (enforced by shared
fixtures in contract/).
import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
emailclient "github.com/pennsieve/email-service/client"
)
cfg, _ := config.LoadDefaultConfig(ctx)
c := emailclient.New(sqs.NewFromConfig(cfg), queueURL) // EMAIL_SERVICE_QUEUE_URL
req := emailclient.DatasetPublicationAccepted(
emailclient.To{Name: "Alice", Email: "alice@example.com"},
emailclient.DatasetPublicationAcceptedArgs{
DatasetName: "My Dataset",
ReviewerName: "Bob",
Date: "2026-06-22",
},
).WithOrganization(367) // optional: use the org's branded template
if err := c.Send(ctx, req); err != nil { /* handle enqueue error */ }
See client/README.md for the full API. Builders are
generated from the template manifest (make generate).
import com.pennsieve.email.client.{EmailClient, Messages, Recipient}
import software.amazon.awssdk.services.sqs.SqsClient
val client = new EmailClient(SqsClient.create(), queueUrl) // EMAIL_SERVICE_QUEUE_URL
val request = Messages
.datasetPublicationAccepted(
Recipient("Alice", "alice@example.com"),
datasetName = "My Dataset",
reviewerName = "Bob",
date = "2026-06-22"
)
.withOrganization(367L)
client.send(request) // returns Try[String]
Published to Pennsieve's CodeArtifact as com.pennsieve %% email-client-scala
(release versions are cut from vX.Y.Z git tags). See
client-scala/README.md.
The request contract
Every send is an EmailRequest:
{
"messageId": "dataset-publication-accepted", // which template
"dedupeId": "optional-stable-id", // optional idempotency id
"recipients": [{ "name": "Alice", "email": "alice@example.com" }],
"context": { // template variables + reserved keys
"organizationId": 367, // optional → branded template
"subject": "override", // optional → overrides default subject
"datasetName": "My Dataset", "reviewerName": "Bob", "date": "2026-06-22"
}
}
The context keys a template needs are listed per messageId in the
email-templates repo's template-variables.json (the typed builders encode them
for you).
Templates
Email templates are not in this repo — they live in
Pennsieve/email-templates.
Keeping them separate lets template copy/branding change and deploy on its own
cadence, without touching or redeploying this service.
That repo holds the MJML source for each email (~25 templates), shared
header/footer partials, and per-organization branding under
mjml/custom/O{orgId}/. Its CI compiles MJML → HTML and aws s3 syncs the
output into this service's S3_BUCKET (default/ for the standard templates,
custom/O{orgId}/ for branded overrides) — which is exactly the layout step 2
of How it works reads from.
Two files in email-templates are the contract between the repos:
template-mapping-seed.json — seeds the email-message-templates
DynamoDB table (messageId → template file + default subject), so a new
template can be wired up without a code change here.
template-variables.json — the per-messageId list of context keys
each template expects. It is the source of truth the client builders are
generated from (make generate), so the producers, the templates, and the
rendered output stay in lockstep.
Adding or changing an email is therefore mostly an email-templates change
(add/edit the MJML, update the two manifests); the only change here is
regenerating the client builders from the updated template-variables.json.
Internals
Entry point
The service is two lambdas:
- queue lambda (
cmd/queue) — consumes the SQS send queue
{env}-email-service-queue-use1 via an event source mapping with
ReportBatchItemFailures; renders and delivers each message.
- bounce lambda (
cmd/bounce) — subscribed to the
{env}-email-service-bounce-topic-use1 SNS topic that SES publishes
bounce/complaint notifications to; auto-suppresses the affected addresses (see
Bounce & complaint auto-suppression).
Environment variables
| Variable |
Description |
PENNSIEVE_DOMAIN |
DNS domain the service runs in; sets the sender Pennsieve <support@{domain}> and Reply-To: support@{domain} |
S3_BUCKET |
S3 bucket holding the compiled email templates |
TEMPLATES_TABLE |
DynamoDB table mapping messageId → template file + subject |
JOURNAL_TABLE |
DynamoDB table journaling sent messages (email-message-log) |
JOURNAL_TTL_DAYS |
retention for journal rows before TTL expiry (default 90) |
LOG_LEVEL |
slog level: DEBUG/INFO/WARN/ERROR (default INFO) |
SEND_ENABLED |
service-level send switch; false = log-only for the whole service (default enabled) |
SUPPRESSION_TABLE |
DynamoDB table of suppressed recipient addresses (email-suppression) |
RATE_LIMIT_TABLE |
DynamoDB table of send-rate window counters (email-rate-counter) |
SEND_RATE_LIMIT_PER_MINUTE |
account-wide cap on emails handed to SES per minute; 0 disables (default 300) |
PER_MESSAGE_RATE_LIMIT_PER_MINUTE |
optional per-messageId cap per minute; 0 disables (default 120) |
Send controls
Three independent controls can suppress actual delivery. Under all of them the
request is still journaled — a suppressed send is logged-only, not dropped —
and its journal row carries LoggedOnly=true (absent = truly sent). Any one
control being active makes the send log-only:
| Level |
Mechanism |
Scope |
| Service |
SEND_ENABLED=false env var |
whole service |
| Template |
SendDisabled=true attribute on the email-message-templates item |
one messageId |
| Address |
a row in the email-suppression table (keyed by email) |
one recipient |
Service and template controls make the whole message log-only; address
suppression is per-recipient (others on the same message still send).
Rate safeguard
A fourth guard protects the SES account (whose reputation/quota is expensive
to recover if suspended) from a producer stuck in a loop. Dedupe only stops
identical repeats; a loop that varies context (dates, ids) or fans out across
recipients produces distinct sends that dedupe won't catch — this is the backstop.
Before each SES send the handler counts it against a per-minute window in the
email-rate-counter table (an account-wide counter, plus an optional
per-messageId counter). If a cap is exceeded — or the counter check itself
errors — the send is made log-only rather than delivered. It fails closed
on purpose: if we can't confirm we're under the cap, we don't send. A tripped
limit logs rate limit exceeded (WARN), which a CloudWatch metric filter + alarm
turn into a page.
Caps are env vars (SEND_RATE_LIMIT_PER_MINUTE, PER_MESSAGE_RATE_LIMIT_PER_MINUTE;
0 disables), tunable per environment without a code deploy.
Bounce & complaint auto-suppression
The address suppression list above is also fed automatically. SES publishes
bounce and complaint notifications for the Pennsieve domain identity to an
SNS topic, and a second lambda (cmd/bounce, subscribed to that topic) adds the
affected addresses to the email-suppression table so the service stops sending
to them:
- Permanent bounces → suppressed with
Reason=bounce. These are hard failures
(address doesn't exist); repeatedly sending to them is what wrecks the account's
bounce rate.
- Complaints (recipient hit "mark as spam") → suppressed with
Reason=complaint.
Complaint rate is the other metric AWS suspends over.
- Transient bounces (mailbox full, greylisting) are ignored — suppressing a
temporarily-unavailable address would permanently blacklist it.
The handler is idempotent (re-suppressing refreshes the row) and skips unparseable
notifications rather than failing the SNS delivery. This closes the loop: bad
addresses are removed from circulation without operator intervention.
Table: email-message-templates
Maps messageId to a template file in S3 and the default subject line. The
default subject may be overridden by a subject in the message context.
Item Attributes
MessageId: String, slug-style
Subject: String, the default subject line for the email message
TemplateFile: String, the name of the template file
SendDisabled: Bool (optional) — when true, this message type is log-only (not delivered). Absent = sending enabled.
Keys
Search Indexes
none
Table: email-message-log
The journal of email messages handled by the service: one row per (message,
recipient). It serves three purposes — an audit record, the idempotency guard
against SQS redelivery, and operational troubleshooting ("I never got the
email"). Rows expire via a DynamoDB TTL after JOURNAL_TTL_DAYS (default 90).
Item Attributes
Id: String, the idempotency / dedupe key — {dedupeId}:{recipient} when the producer supplies a dedupeId, otherwise a SHA-256 of messageId + recipient + canonicalized context
MessageId: String, slug-style id of the email
Recipient: String (email address)
Status: String — QUEUED (claimed), SENT (accepted by SES), or FAILED
Timestamp: Int64 (Unix epoch seconds)
SentAtKey: String, zero-padded Unix epoch — the GSI sort key (lexicographic order == time order)
MessageSent: String (RFC3339 timestamp), set on SENT
SesMessageId: String, the SES message id, set on SENT
Error: String, the failure detail, set on FAILED
Context: Map of String (name -> value)
ExpiresAt: Int64 (Unix epoch seconds), DynamoDB TTL attribute
LoggedOnly: Bool (optional) — true when a send control suppressed delivery (the row was journaled but not sent via SES). Absent = truly sent.
Table: email-suppression
The address-level send control: a recipient with a row here is not delivered to
(the request is still journaled, LoggedOnly). Edited operationally without a
deploy.
Item Attributes
Email: String, the suppressed recipient address
Reason: String (optional), why it was suppressed
CreatedAt: String (optional), when it was added
Keys
Table: email-rate-counter
The rate safeguard's counters: one short-lived row per (counter key, time
window). The handler does an atomic ADD per send and compares to the cap; rows
expire via TTL so only current windows persist.
Item Attributes
Bucket: String, <key>#<window-epoch> (e.g. global#28..., msg#welcome#28...)
Count: Number, sends counted in this window
ExpiresAt: Int64 (Unix epoch seconds), DynamoDB TTL attribute
Keys
Keys
Search Indexes
- RecipientSentAtIndex:
Recipient (HASH) + SentAtKey (RANGE) — find all emails to a recipient, newest-first (query with ScanIndexForward=false). MessageId is projected (ALL) so it can be filtered client-side or with a FilterExpression.
Troubleshooting
To investigate whether an email was sent to a user, query the journal by
recipient with scripts/email-log.sh:
# All emails to a recipient, most-recent first
ENV=dev ./scripts/email-log.sh recipient jane@example.com
# Only a specific email type
ENV=dev ./scripts/email-log.sh recipient jane@example.com --message-id welcome
# Just the latest email to a recipient
ENV=dev ./scripts/email-log.sh latest jane@example.com
# A single row by its Id (dedupe key)
ENV=dev ./scripts/email-log.sh id 'abc123:jane@example.com'
Read the Status on the returned row(s): no row means the send was never
queued; FAILED shows the Error; SENT includes the SesMessageId to trace
delivery in SES.
Manually invoking the lambda (AWS Console "Test")
The handler receives an SQS event envelope, not a raw EmailRequest. The
EmailRequest JSON must be the (string-escaped) body of a record:
{ "Records": [ { "body": "{\"messageId\":\"...\", ...}" } ] }
A ready-to-use sample lives at testdata/sqs-event.json
— paste it into the Lambda Test event (edit the body for your case).
A raw EmailRequest (no Records wrapper) is not an error — the handler
just finds zero records and returns silently (no journal row, no send). The
processing SQS event log line reports recordCount; a recordCount=0 means
the payload wasn't wrapped correctly. The handler also logs each step
(parsed request → resolved mapping → fetched template → rendered → claiming
send → email sent) at INFO, so a real invocation is fully traceable in
CloudWatch.
Possible future additions
- Synchronous API endpoint (e.g.
POST /email/send, service-token
protected) for callers that need an immediate send rather than enqueuing. Not
implemented today — the service is SQS-only. The original scaffold included an
API lambda; it was removed in favor of the queue-only design and would be
re-added here if a synchronous path is needed.