Send email from DaloyJS with Mailgun
Mailgun (now Sinch Mailgun) offers high-volume transactional and bulk email, address validation, and routing. This guide uses the official mailgun.js SDK.
Route handlermg clientMailgun APIWebhooks
- 01requestRoute handlermg clientmg.messages.create(DOMAIN, { from, to, subject })client keyed with MAILGUN_API_KEY
- 02requestmg clientMailgun APIPOST /v3/{domain}/messagesUS api.mailgun.net or EU api.eu.mailgun.net (fixed per domain)
- 03responseMailgun APIRoute handler{ id, message }return { id } on success
- 04asyncMailgun APIWebhooksdelivery, bounce, complaint eventsverify HMAC-SHA256(timestamp + token) before trusting
1. Provision
- Add and verify your sending domain under Sending → Domains → Add new domain. Add the SPF/DKIM TXT records and the MX records Mailgun lists.
- Choose your region:
https://api.mailgun.net(US, default) orhttps://api.eu.mailgun.net(EU). The region is fixed per domain. - Create a Sending API key from API Security and store it as
MAILGUN_API_KEY.
2. Install
form-data is the multipart implementation Mailgun expects on Node.
3. Environment variables
4. Plugin
5. Use it in a route
Templates
Create a stored template in Sending → Templates with Handlebars syntax, then reference it by name and pass variables as a JSON string:
Runtimes
- Node / Bun / AWS Lambda: works with the configuration above.
- Cloudflare Workers / Vercel: pass
useFetch: trueso the SDK uses the platform's nativefetchinstead ofrequest(which depends on Node's HTTP module):WhenuseFetchis enabled, you can also drop theform-datadependency in favour of the globalFormData.
Webhooks
Mailgun signs webhook payloads with HMAC-SHA256. When you accept delivery, bounce, or complaint events, verify signature.signature against timestamp + token using your webhook signing key before trusting the body.
See also AWS SES, Resend, and the email integrations overview.