Email & Webhook Integration Guide
This guide explains how to use the email service and webhook handler in your Mindful Auth template for sending transactional emails via Postmark API.
The template includes two key components for email integration:
src/lib/email.ts– Email service that sends transactional emails via Postmarksrc/pages/api/public/webhooks/mauth-webhook.ts– Webhook handler that receives events from Mindful Auth and triggers emails
Prerequisites
Section titled “Prerequisites”- Postmark Account – Create an account at postmarkapp.com (affiliate link)
- Verified Sender – Add and verify your sender email address in Postmark
- API Token – Generate a server API token from Postmark
- Mindful Auth Account – Have your Mindful Auth instance configured and running
Setup Instructions
Section titled “Setup Instructions”1. Configure Environment Variables
Section titled “1. Configure Environment Variables”Add the following environment variables to your .env or deployment configuration:
POSTMARK_API_TOKEN=your_postmark_server_api_tokenEMAIL_FROM=Your App Name <no-reply@example.com>Notes:
- Replace
your_postmark_server_api_tokenwith your actual Postmark server API token EMAIL_FROMmust be a verified sender address in Postmark (format:Display Name <email@example.com>)
2. Configure Webhook in Mindful Auth
Section titled “2. Configure Webhook in Mindful Auth”In your Mindful Auth dashboard:
- Go to Webhooks settings
- Add a new webhook with:
- URL:
https://app.yourdomain.com/api/public/webhooks/mauth-webhook - Events: Select
verify_email,password_reset, andmagic_login
- URL:
- Save the webhook configuration
Replace yourdomain.com with your actual domain.
3. Deploy
Section titled “3. Deploy”Deploy your application to make the webhook endpoint publicly accessible:
npm run buildnpm run deploy # or your deployment commandHow It Works
Section titled “How It Works”Email Service (src/lib/email.ts)
Section titled “Email Service (src/lib/email.ts)”The email service provides three main functions for sending transactional emails:
sendVerificationEmail()
Section titled “sendVerificationEmail()”Sends an email with a link for users to verify their email address.
await sendVerificationEmail( apiToken, 'no-reply@example.com', 'user@example.com', 'John Doe', 'https://app.example.com/email-verified/abc123');sendPasswordResetEmail()
Section titled “sendPasswordResetEmail()”Sends an email with a link for users to reset their password.
await sendPasswordResetEmail( apiToken, 'no-reply@example.com', 'user@example.com', 'John Doe', 'https://app.example.com/reset-password/token123');sendMagicLinkEmail()
Section titled “sendMagicLinkEmail()”Sends a magic link that allows users to sign in without a password.
await sendMagicLinkEmail( apiToken, 'no-reply@example.com', 'user@example.com', 'John Doe', 'https://app.example.com/verify-magic-link/token123');Webhook Handler (src/pages/api/public/webhooks/mauth-webhook.ts)
Section titled “Webhook Handler (src/pages/api/public/webhooks/mauth-webhook.ts)”The webhook handler receives events from Mindful Auth and automatically sends the appropriate emails.
Supported Events
Section titled “Supported Events”1. verify_email
- Triggered when a user signs up or requests email verification
- Sends a verification email with confirmation link
2. password_reset
- Triggered when a user requests a password reset
- Sends an email with a password reset link
3. magic_login
- Triggered when a user requests a magic link login
- Sends an email with a one-time sign-in link
Webhook Payload Structure
Section titled “Webhook Payload Structure”All webhook payloads follow this base structure:
{ "event_type": "verify_email|password_reset|magic_login", "recordid": "unique_record_id", "email": "user@example.com", "name": "User Name", "verificationLink": "https://...", // for verify_email "resetLink": "https://...", // for password_reset "magicLoginLink": "https://..." // for magic_login}Testing
Section titled “Testing”Test the Webhook Endpoint
Section titled “Test the Webhook Endpoint”You can test if the webhook endpoint is accessible by visiting:
https://app.yourdomain.com/api/public/webhooks/mauth-webhookA successful response will return:
{ "status": "Webhook endpoint is active", "method": "POST", "note": "This endpoint accepts POST requests from Mindful Auth"}Send a Test Email
Section titled “Send a Test Email”To manually test the email service, create a test script:
import { sendVerificationEmail } from '@/lib/email';
const apiToken = process.env.POSTMARK_API_TOKEN;const emailFrom = process.env.EMAIL_FROM;
await sendVerificationEmail( apiToken, emailFrom, 'test@example.com', 'Test User', 'https://app.example.com/verify/test123');Postmark Test Mode
Section titled “Postmark Test Mode”For development, you can use Postmark’s test API tokens to send emails without actually delivering them. Check Postmark’s documentation for test token setup.
Environment Variables Reference
Section titled “Environment Variables Reference”| Variable | Required | Description |
|---|---|---|
POSTMARK_API_TOKEN | Yes | Your Postmark server API token |
EMAIL_FROM | Yes | Verified sender address (format: Name <email@domain.com>) |
Troubleshooting
Section titled “Troubleshooting”Emails Not Being Sent
Section titled “Emails Not Being Sent”- Check environment variables – Verify
POSTMARK_API_TOKENandEMAIL_FROMare set correctly - Verify sender address – Ensure
EMAIL_FROMis verified in Postmark - Check logs – Review application logs for error messages
- Test Postmark connectivity – Make a direct API call to verify the token works
Webhook Not Being Called
Section titled “Webhook Not Being Called”- Verify webhook URL – Test the endpoint is publicly accessible
- Check webhook configuration – Confirm the URL is correct in Mindful Auth
- Enable events – Ensure required events are selected in webhook settings
- Review logs – Check application logs for incoming webhook requests
Email Delivery Issues
Section titled “Email Delivery Issues”- Check spam folder – Verify emails aren’t being filtered
- Review Postmark activity – Check Postmark dashboard for delivery status
- Validate email addresses – Ensure recipient addresses are correct
- HTML/Text content – Review email templates in
src/lib/email.ts
Customization
Section titled “Customization”Modify Email Templates
Section titled “Modify Email Templates”Edit the email content in src/lib/email.ts. Each function has HtmlBody and TextBody properties:
export async function sendVerificationEmail(...) { await sendEmail(apiToken, { // ... other properties HtmlBody: ` <h2>Custom Subject</h2> <p>Your custom message here</p> `, TextBody: `Custom text version`, });}