SendGrid stub and email events
New to testing this locally? Start with How to test SendGrid emails locally, which compares MessagePit with the provider’s own test mode.
MessagePit exposes a SendGrid v3 Mail Send stub on port 8100:
POST /v3/mail/send
Authorization: Bearer <MP_SENDGRID_API_KEY>
Content-Type: application/jsonPoint your application’s SendGrid SDK at the stub by setting the API base URL to http://localhost:8100. The endpoint accepts the standard SendGrid v3 JSON payload (personalizations, from, subject, content, custom_args) and stores each message in the MessagePit mailbox. Authentication is skipped when MP_SENDGRID_API_KEY is empty.
The SendGrid server defaults to 127.0.0.1:8100 (loopback only). Set MP_SENDGRID_BIND_ADDR=0.0.0.0:8100 to expose it on all interfaces (e.g. inside Docker). Pass --sendgrid "" to disable it entirely (an empty MP_SENDGRID_BIND_ADDR is ignored).
Email event webhooks
Coming in the next release. The full event lifecycle, events for every message, flattened
custom_args,category, delivery scenarios,X-SMTPAPIsupport and--email-webhook-event-delayare onmainand listed under Unreleased in the changelog. MessagePit v1.3.0 sends a singledeliveredevent, and only for messages with anotification_idincustom_args. Request signing works the same in both.
When MP_EMAIL_WEBHOOK_URL is set, MessagePit replays the full SendGrid event lifecycle for every captured email, one POST per event, in order:
processed → deliveredReal SendGrid fires its event webhook for everything it accepts, over both the v3 API and its SMTP relay, so MessagePit does the same. No custom_args are required.
Each request body is a JSON array holding a single event. SendGrid batches events that land in the same window; MessagePit sends them individually so the ordering stays visible in development.
[
{
"email": "recipient@example.com",
"timestamp": 1714000000,
"smtp-id": "<abc123@messagepit>",
"event": "delivered",
"response": "250 2.0.0 OK",
"category": ["welcome"],
"sg_event_id": "rbtnWrG1DVDGGGFHFdun0A",
"sg_message_id": "142d9f3f351f2dad77c8.messagepit",
"notification_id": "<value from custom_args>"
}
]Every key in custom_args is flattened onto each event as a top-level field, exactly as real SendGrid does, so notification_id keeps working unchanged. categories from the v3 payload arrive as category.
Event-specific fields match the real API: response on delivered/deferred, reason + status + type + bounce_classification on bounce, reason + status on dropped, useragent + ip on open/click, and url + url_offset on click.
Delivery scenarios
The default lifecycle is processed → delivered. To exercise the failure and engagement branches, select a scenario either with a mp_scenario custom arg or by addressing the recipient with a matching local part or plus-tag (bounce@example.com, user+bounce@example.com). An explicit mp_scenario wins over the address.
| Scenario | Event sequence |
|---|---|
delivered (default) | processed → delivered |
open | processed → delivered → open |
click | processed → delivered → open → click |
deferred | processed → deferred → delivered |
bounce | processed → bounce (type: bounce, 5.1.1) |
blocked | processed → bounce (type: blocked, 5.7.1) |
dropped | processed → dropped |
spamreport | processed → delivered → spamreport |
unsubscribe | processed → delivered → unsubscribe |
group_unsubscribe | processed → delivered → group_unsubscribe |
group_resubscribe | processed → delivered → group_resubscribe |
{
"personalizations": [{ "to": [{ "email": "user@example.com" }] }],
"custom_args": { "notification_id": "42", "mp_scenario": "bounce" }
}Set --email-webhook-event-delay (MP_EMAIL_WEBHOOK_EVENT_DELAY, e.g. 2s) to space the events out. The default is 0.
Signature verification
Webhooks are signed using ECDSA P-256 / SHA-256, with the signature and timestamp in the same headers real SendGrid uses:
| Header | Description |
|---|---|
X-Twilio-Email-Event-Webhook-Signature | Base64-encoded ECDSA signature |
X-Twilio-Email-Event-Webhook-Timestamp | Unix timestamp as a string |
The payload that is signed is timestamp + body (the timestamp string concatenated with the raw JSON body).
Key pair setup
Provide a stable key pair so the public key doesn’t change across restarts:
# Generate private key (SEC1 DER, base64) — set as MP_EMAIL_WEBHOOK_SIGNING_KEY
openssl ecparam -name prime256v1 -genkey -noout \
| openssl ec -outform DER 2>/dev/null \
| base64
# Derive the matching public key (PKIX DER, base64) — set as SENDGRID_WEBHOOK_PUBLIC_KEY in your app
echo "$MP_EMAIL_WEBHOOK_SIGNING_KEY" \
| base64 -d \
| openssl ec -inform DER -pubout -outform DER 2>/dev/null \
| base64If MP_EMAIL_WEBHOOK_SIGNING_KEY is empty and MP_EMAIL_WEBHOOK_URL is set, MessagePit auto-generates a key pair at startup and logs the public key. That’s useful for one-off testing but not stable across restarts.
SMTP vs. SendGrid v3
Events fire for both paths, mirroring SendGrid’s own SMTP relay. Messages sent over SMTP simply have no custom_args to flatten onto the events unless you set them yourself: MessagePit reads SendGrid’s X-SMTPAPI header ({"category":[...],"unique_args":{...}}), which the v3 handler also uses internally to carry that metadata through storage. Scenario selection by recipient address works over SMTP too, or set X-MessagePit-Scenario directly.