Automated Emails with Supabase Database Triggers
Learn how to connect your Supabase database to Mailofly and send personalized transactional emails instantly whenever a new record is inserted or modified—without writing background workers or managing queue servers.The Problem It Solves
In standard web architectures, sending transactional emails when something happens in your database (like a user registering, subscribing to a plan, or placing an order) typically requires:- Setting up a queue system like BullMQ, Celery, or AWS SQS.
- Running separate worker processes or Docker containers.
- Deploying serverless Edge Functions and managing webhooks.
- Handling connection retries and dead-letter queues.
How Mailofly Helps
With Mailofly’s native Supabase integration:- Zero infrastructure: Dispatches emails straight from PostgreSQL using the native
pg_netextension. - Instant, sub-second delivery: Triggers fire the millisecond your database transaction commits.
- Non-blocking execution: Uses asynchronous HTTP requests so your database queries and application response times are never slowed down.
- Automatic column mapping: All row columns (
name,plan,amount, etc.) are automatically passed into your template’s merge variables. - Built-in HMAC security: Payloads are cryptographically signed using
pgcryptoto prevent tampering.
How It Works
- Application Event: A user interacts with your app, triggering an
INSERTorUPDATEinto your Supabase database table. - PostgreSQL Trigger: An
AFTER INSERT OR UPDATEtrigger installed by Mailofly packages the row data into JSON. - Async HTTP Request: The
pg_netextension sends an asynchronous HTTP POST request to Mailofly’s webhook endpoint. - Signature Verification & Rendering: Mailofly validates the HMAC-SHA256 signature, extracts the recipient’s email address from your designated column, populates template variables (
{{name}},{{plan}}), and delivers the message.
Step-by-Step Tutorial
Step 1: Create Your Supabase Table
If you don’t already have a table, create one in your Supabase SQL Editor. Here is an examplesubscribers table:
Step 2: Prepare Your Email Template in Mailofly
- In the Mailofly dashboard, go to Templates and click Create Template.
- Give your template a name (e.g.
Welcome Subscriber). - Add subject line:
Welcome to our platform, {{name}}! - Write your HTML content using merge tags that correspond to your table’s columns:
- Click Save Template.
Step 3: Connect Supabase in Mailofly
- In Mailofly, navigate to Integrations.
- Select the Supabase card.
- Click Connect with Supabase.
- In the Supabase OAuth authorization window, select your organization and project, then click Authorize.
Step 4: Create the Database Trigger
- In the Supabase integration view in Mailofly, click Add Trigger.
- Configure the trigger parameters:
- Project: Select your connected Supabase project.
- Table: Select
public.subscribers. - Event: Choose
INSERT(to fire on new signups) orUPDATE(for status updates). - Recipient Column: Select
email. - Email Template: Select the
Welcome Subscribertemplate you created in Step 2.
- Click Create Trigger.
Step 5: Test the Integration
Test the trigger by inserting a test record directly in the Supabase SQL Editor:- Check
you@example.com—your personalized welcome email will arrive withAlex RiversandEnterprise Propopulated. - In Mailofly, view the Integrations page to see the trigger’s Emails sent count increment.
- Inspect detailed delivery status, open rates, and click tracking in the Logs tab.
Verifying in PostgreSQL
To inspect the background HTTP request made by Supabase, run this query in your Supabase SQL Editor:status_code of 200 confirms that Mailofly accepted and queued the email for immediate delivery.
Pro Tips & Best Practices
Non-Blocking Reliability:
Because
pg_net executes outside the main PostgreSQL transaction worker, even if an email network timeout occurs, your user’s database insert will never fail or roll back.Next Steps
- Supabase Integration Reference — Technical specifications, cryptographic signing details, and extension requirements.
- Template Variables & Personalization — Learn about formatting dates, fallback values, and conditionals.
- Custom Domains Setup — Connect your own sending domain for maximum inbox deliverability.