Email-to-ticket
Clients can open and answer support tickets by email, and staff can answer from their inbox. Set SUPPORT_INBOUND_ADDRESS to the support mailbox, such as support@example.com, then pick one way for mail to reach Tradehall: a webhook or IMAP polling.
How mail is routed
Section titled “How mail is routed”- Replies. Every ticket email Tradehall sends carries a Reply-To address made for that ticket and that person, such as
support+t42-u7-0f1e2d3c4b5a6978@example.com. The token is signed withAPP_KEY, so only someone who received the email knows it. A reply to it lands on the ticket as that person, whether they’re a client contact or staff. A colleague at the same client answering a forwarded copy is credited as themselves; anyone else is turned away. The mailbox must accept plus addresses (Cloudflare Email Routing calls this subaddressing; Google Workspace and Microsoft 365 accept them by default). - New tickets. Mail with no token, from an active client contact, opens a ticket for their client. They get an email with the ticket number and their reply address. A subject containing
[#42]from a contact at ticket 42’s client is added to that ticket instead. - Turned away. Senders with no login, staff writing without a token, mail whose domain fails SPF, DKIM, and DMARC (while
SUPPORT_INBOUND_REQUIRE_AUTHis on, the default), and mail with no new text. Auto-replies, bounces, and mailing lists are ignored. Tickets, Email log lists the latest 100 messages and what became of each, so staff can follow up. - Quoted text. Only the new text is kept: Tradehall cuts at its “Reply above this line” marker, “On … wrote:”, Outlook’s header block, signatures, and “Sent from my iPhone”. Attachments aren’t imported yet; the reply lists their names.
Each Message-ID is handled once, so a retried webhook or a mailbox fetched twice never posts twice.
Webhook
Section titled “Webhook”POST /support/inbound-email takes the token in SUPPORT_INBOUND_TOKEN as a bearer token, a Basic auth password, or a token query parameter. It answers 200 for mail it turned away, so the provider doesn’t retry it.
Cloudflare Email Routing. Route the support address (and its plus addresses) to an Email Worker that posts the raw message:
export default { async email(message, env) { const res = await fetch('https://portal.example.com/support/inbound-email', { method: 'POST', headers: { Authorization: `Bearer ${env.TRADEHALL_INBOUND_TOKEN}`, 'Content-Type': 'message/rfc822', 'X-Envelope-To': message.to, }, body: message.raw, }); if (!res.ok) message.setReject(`Tradehall answered ${res.status}`); },};SendGrid Inbound Parse. Point the parse URL at https://portal.example.com/support/inbound-email?token=.... Either form works; checking “POST the raw, full MIME message” keeps more detail.
For a mailbox at Google Workspace, Microsoft 365, or any IMAP host, set SUPPORT_IMAP_HOST, SUPPORT_IMAP_USERNAME, and SUPPORT_IMAP_PASSWORD (an app password). The scheduler runs support:fetch-mail every minute: it reads up to 50 unread messages from SUPPORT_IMAP_MAILBOX, handles each, and marks it read. A message that fails stays unread for the next run. Give Tradehall a mailbox of its own, since it marks everything it reads.