Forwarding incoming WhatsApp messages to email with webhooks
Not every team lives in Slack or Teams. Plenty of small businesses — a garage, an accounting firm, a two-location hair salon — still run on a shared mailbox like info@ or support@. When a customer replies on WhatsApp to an appointment reminder or an invoice, you want that reply to land there too, without anyone having to keep the TextMeFlow inbox open all day. This article builds a small endpoint that turns every incoming WhatsApp message into an email, via a TextMeFlow webhook.
The principle is identical to forwarding to Slack or Microsoft Teams: TextMeFlow posts the raw message data to your server, and you verify and translate it into the channel your team already uses.
What you need
- A TextMeFlow account with a linked WhatsApp number and a
webhook_urlconfigured in/admin/webhook - A way to send email from code: an SMTP account (your own mail server, or Gmail/Outlook with an app password) or a transactional email API such as Postmark, Mailgun or Resend
- A small server (Node/Express in this example)
Step 1: set up email sending
For production, use a transactional email API rather than your own mail server — it gives you bounce handling and better deliverability. With Nodemailer, any SMTP provider works the same way:
const nodemailer = require('nodemailer');
const mailer = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 587,
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
});
Step 2: verify the webhook endpoint
For every incoming message, TextMeFlow posts a JSON body, signed with HMAC-SHA256 in the X-TextMeFlow-Signature header (format: sha256=<hex>). Always verify it before trusting the payload — otherwise anyone who guesses your endpoint URL can send fake messages. The full explanation of this header lives in the article on verifying webhook signatures.
const express = require('express');
const crypto = require('crypto');
const app = express();
app.use(express.raw({ type: 'application/json' }));
const SECRET = process.env.TMF_WEBHOOK_SECRET;
const seen = new Set(); // replace with a DB table in production
app.post('/textmeflow-inbound', async (req, res) => {
const raw = req.body;
const expected = 'sha256=' + crypto.createHmac('sha256', SECRET).update(raw).digest('hex');
const received = req.headers['x-textmeflow-signature'] || '';
if (!crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(received))) {
return res.sendStatus(401);
}
const eventId = req.headers['x-textmeflow-event-id'];
if (seen.has(eventId)) return res.sendStatus(200); // retry, already processed
seen.add(eventId);
res.sendStatus(200); // acknowledge first, process after
forwardToEmail(JSON.parse(raw));
});
app.listen(3000);
Always acknowledge with 200 before processing — TextMeFlow expects a response within 10 seconds, and a slow SMTP call shouldn't put that deadline at risk.
Step 3: turn the message into an email
The payload includes type, from, from_name, to and message — the same fields as TextMeBot, so an existing integration migrates one-to-one. Build a readable email from it:
async function forwardToEmail(payload) {
const { type, from, from_name, message } = payload;
const label = type === 'text' ? '' : ` (${type})`;
await mailer.sendMail({
from: '"WhatsApp inbox" <[email protected]>',
to: '[email protected]',
replyTo: '[email protected]',
subject: `New WhatsApp message from ${from_name || from}${label}`,
text: `${from_name || from} (+${from}) wrote:\n\n${message}`,
});
}
For image, document or location messages, the filename or "<lat>,<lng>" is in message — for now just mention the type in the subject, so your colleague knows an attachment is on its way.
Keeping threads per customer
One real downside of email versus Slack: there's no channel, so messages from the same customer arrive as separate emails. A simple fix is a fixed subject line per phone number (e.g. WhatsApp — +32470123456), so Gmail and Outlook group them into one thread automatically:
subject: `WhatsApp — +${from}`,
That keeps the whole conversation with that customer readable in a single mail thread, even when the shared mailbox is managed by several people.
Retries and idempotency
A failed delivery (timeout, 5xx, 429) is retried up to 5 times with increasing backoff (1m, 5m, 30m, 2h, 6h). Without the seen check above, every retry lands a duplicate email in your mailbox. In production, replace the Set with a unique constraint on X-TextMeFlow-Event-Id in your database, so a duplicate insert simply fails and gets ignored.
Taking it further
This basic flow extends quickly: route emails to a different address based on the to number (useful with multiple linked devices from the Business plan), or add simple rules that mail an invoice-related message to accounting and an appointment question to the front desk. The full field reference lives in the webhook documentation.
Want to try this yourself? The free plan gives you 50 messages a month, forever — plenty to build and test this whole flow before you move to a paid plan. Start for free.
Want to send WhatsApp messages via API yourself?
Free forever up to 50 messages/month. Scan a QR and send your first message within 5 minutes.
Free forever