Skip to content
Darin Larimore
All posts

Turning email replies into ticket comments with inbound webhooks

6 min read
On this page
  1. The address is the routing
  2. SendGrid's Inbound Parse
  3. Moving to Resend
  4. What I would tell myself at the start

Notedis is my website feedback tool. Someone clicks a spot on a page, types a note, and the site owner gets an email with a ticket. The first version of that email was a dead end: to answer, the owner had to open the app. People do not do that. They hit reply in their mail client, and the reply went to a no-reply address and vanished. So the feature I wanted was simple to say: reply to the email, and your reply becomes a comment on the ticket, and the person who left the feedback gets it. Building it took two providers and about five months of small lessons.

The address is the routing

Every ticket needs its own reply address, because the inbound side has to know which ticket a reply belongs to without parsing the subject. Each ticket already had an obfuscated public id, so the outgoing notification sets a reply-to of ticket-<id>@ on a dedicated reply domain, and the webhook handler does one regex on the recipient:

if (preg_match('/ticket-([a-z0-9]+)@/', $to, $matches)) {
    $feedback = Feedback::where('obfuscated_id', $matches[1])->first();
    ...
}

The same mailbox also catches support@ and security@, which become tickets of their own rather than comments. Everything else that lands on the domain is mail nobody asked for: bounces, spam, people writing to a plain address. That turned out to matter later.

SendGrid's Inbound Parse

The first implementation, in October 2025, used SendGrid's Inbound Parse. You point the domain's MX at SendGrid, and it POSTs every message to your URL as multipart/form-data: the text body, the HTML body, the headers, and each attachment as an uploaded file. That is convenient, since Laravel reads it like any form, and the first version of the handler was a controller that took the parts, matched the ticket, and created a comment.

Two things bit immediately. The first was that replies carried the entire quoted thread. A comment reading "Yes, fixed, thanks" followed by two hundred lines of > On Tuesday, so-and-so wrote: is useless, so the handler got a quote stripper: walk the lines, stop at the first one that looks like a quote marker. The list of markers grew for a week as different mail clients showed up: > prefixes, On ... wrote: in four date formats, Outlook's -----Original Message-----, and From: / Sent: / To: / Subject: blocks. It is a heuristic and it will always be one. A reply from a client I have not seen yet will still leak a quote now and then.

The second was verification. SendGrid signs its JSON event webhooks, and the signature check worked for those. Inbound Parse is multipart, and the signed payload is the raw body, which PHP has already consumed and parsed by the time the request reaches the controller. I spent an evening on "fix webhook signature verification for multipart" commits before accepting it was not going to verify. The fallback was an IP allowlist of SendGrid's published ranges, and within a month a real reply arrived from a range that was not on the list and was rejected. I added the range, downgraded the log from warning to error so I would notice next time, and wrote a comment above the list saying the ranges change. That is not security. It is a reminder that the list is wrong.

Moving to Resend

By March 2026 I was moving every site's outbound mail to Resend anyway, but inbound is the part that pushed me. The SendGrid side had become the least stable piece of Notedis: an allowlist I had to keep patching, a signature I could not check, and no way to tell a dropped reply from a quiet day. Resend's inbound was the more stable way to run the reply webhooks, so the whole feature moved with the outbound mail. Resend's model is also different in a way I now prefer. The webhook is a small JSON event, email.received, with the message id and the envelope. The body is not in it. The handler fetches the full message from Resend's API with the id, and attachments come as download links that expire after an hour, which the handler pulls into S3 before they go stale.

if (($event['type'] ?? '') !== 'email.received') {
    return response('OK', 200);
}

$email = $this->fetchEmailContent($event['data']['email_id']);

Two round trips instead of one, but the webhook is signed with Svix headers over a JSON body that PHP has not touched, so verification is a library call and the IP list is gone. The multipart problem simply does not exist when the payload is a pointer.

The same week I fixed threading, which I had been ignoring. Every ticket email now carries a deterministic Message-ID built from the ticket id, and every follow-up sets In-Reply-To and References to it, so Gmail and Outlook stack the ticket's whole history in one conversation instead of scattering it. The gotcha there was angle brackets: Laravel's header helpers wrap the message id in <> for you, and a raw text header does not, and the mix produced an RFC 2822 compliance error that only showed up in one mail client.

What I would tell myself at the start

  • Put the routing in the address, not the subject. Subjects get edited. A per-ticket reply-to with an id in it survives every mail client.
  • Strip quotes, and accept that the stripper is a list of patterns you will keep adding to.
  • Prefer a provider whose inbound webhook is a signed JSON event with a fetch. Multipart payloads cannot be verified cleanly in PHP, and an IP allowlist is a bug you have scheduled for later.
  • Set the threading headers on day one. They cost three lines per mail and the conversation view is most of what makes email replies feel like a feature rather than a hack.
  • Decide what "received, not for me" returns before you ship. Bounces and stray mail will hit the same endpoint as real replies, and the status code you return for them is a message to the provider's retry system, not to a person. I got that one wrong, and that deserves a post of its own.