Skip to main content
coreydaley.dev

How to test SendGrid emails locally without sending them

To test SendGrid emails locally, point your app’s SendGrid client at a local server that accepts the same v3 Mail Send API and shows you each email in a web inbox. Nothing reaches a real inbox. If you only need to know the request is valid, SendGrid’s own sandbox mode does that without a local server.

Mailpit is the usual local inbox for email. It receives mail over SMTP and has its own HTTP send API, but neither speaks SendGrid’s. If your app sends through SendGrid’s v3 API (POST /v3/mail/send), you need something that accepts that request as it is.

Option 1: catch the emails locally (MessagePit)

MessagePit, my open-source fork of Mailpit, adds a SendGrid v3 Mail Send stub on port 8100. It accepts the same JSON your app sends to SendGrid and puts each message in the inbox at http://localhost:8025, next to anything sent over SMTP.

curl -X POST http://localhost:8100/v3/mail/send \
  -H "Authorization: Bearer SG.test" \
  -H "Content-Type: application/json" \
  -d '{"personalizations":[{"to":[{"email":"user@example.com"}]}],
       "from":{"email":"app@example.com"},
       "subject":"Reset your password",
       "content":[{"type":"text/plain","value":"Your reset link: https://example.com/r/abc"}]}'

It answers 202 Accepted, as SendGrid does, and the email shows up in the inbox. In your app, point the SendGrid client at http://localhost:8100 instead of SendGrid’s API; how you set that depends on your library.

Two settings to know:

  • Listening address. The SendGrid stub listens on 127.0.0.1:8100 by default. In Docker, set MP_SENDGRID_BIND_ADDR=0.0.0.0:8100 so the port can be reached from outside the container.
  • API key. With MP_SENDGRID_API_KEY unset, any key is accepted. Set it to require the key your app sends; a wrong key gets 401 Unauthorized.

The SendGrid docs also cover SendGrid-style event webhooks; the full event lifecycle and scenarios for bounces and deferrals are coming in the next release.

Option 2: SendGrid’s sandbox mode

SendGrid’s v3 API has a sandbox mode: add this to the request and SendGrid checks it without delivering the email.

"mail_settings": { "sandbox_mode": { "enable": true } }

That confirms the request is well formed against the real API, but there’s no message to read. It’s handy in CI; less so when you want to see what a user would get.

Which to use

  • Reading what your app sends (subject, body, links, who gets what): catch it locally.
  • Checking requests against SendGrid’s real validation: sandbox mode.
  • Before going live: send a few real emails to your own address with your live key, and check they don’t land in spam. Neither option above tests real delivery.

I tested the MessagePit request above on macOS 27, building MessagePit from its current source.