Skip to main content

Supabase Database Triggers

Mailofly connects directly to your Supabase PostgreSQL database to automate transactional email delivery based on live application events. Instead of writing serverless background workers, message queues, or cron polling jobs, you can configure database triggers in Mailofly that dispatch personalized emails immediately when rows are inserted or updated.

Why Use Database Triggers?

  • Zero backend infrastructure: No need to configure BullMQ, Celery, AWS SQS, or separate microservices just to send emails.
  • Instant delivery: Emails fire the millisecond a database transaction commits (e.g. new signup, purchase, or status change).
  • Automatic column mapping: Any column in your database row automatically populates matching template variables ({{name}}, {{plan}}, {{amount}}).
  • Non-blocking asynchronous execution: Uses native pg_net non-blocking HTTP calls so your database transactions and user responses are never delayed.
  • Cryptographic security: Every request is signed with HMAC-SHA256 to ensure authenticity.

How It Works

  1. OAuth Connection: Connect your Supabase project with one click. Mailofly securely stores credentials to inspect table schemas and manage trigger functions.
  2. Trigger Provisioning: When you configure a trigger, Mailofly installs a lightweight PostgreSQL function and trigger on your chosen table.
  3. Event Firing: When your app executes an INSERT or UPDATE, the trigger formats the new record into a JSON payload, signs it with a shared secret, and posts it via net.http_post().
  4. Template Dispatch: Mailofly verifies the HMAC signature, extracts the recipient’s email address from your chosen column, populates your template with the row values, and delivers the message.
  5. Analytics & Tracking: Delivery status and sent count are automatically updated on your Mailofly dashboard.

Quick Setup

1. Connect Supabase

  1. In your Mailofly dashboard, go to Integrations and click Supabase.
  2. Click Connect with Supabase.
  3. Authorize Mailofly in the Supabase OAuth screen and select your organization and project.

2. Prepare an Email Template

In Templates, create or choose a template with merge tags that match your database columns:

3. Configure the Database Trigger

  1. In the Supabase integration view, click Add Trigger.
  2. Fill out the trigger configuration:
    • Project: Choose your connected Supabase project.
    • Table: Select the table you want to monitor (e.g., public.subscribers or public.orders).
    • Event: Choose INSERT (for new records like signups) or UPDATE (for status changes).
    • Recipient Column: Select the column holding the recipient’s email address (e.g., email).
    • Email Template: Select the template to send.
  3. Click Create Trigger.
Mailofly immediately provisions the function and trigger inside your Supabase project.

Dynamic Column Mapping

Every column in the triggering database row is made available as a template variable:
Template variable names are case-sensitive and match your PostgreSQL column names exactly. If your column is first_name, use {{first_name}} in your template.

Testing Your Trigger

You can test your trigger by inserting a record directly in your Supabase SQL Editor:
Within seconds:
  1. Check your email inbox to confirm receipt of the formatted template.
  2. In Mailofly, view the Emails sent count increment on your trigger row.
  3. Inspect detailed delivery metrics and click/open events in Logs.

Technical Specifications


Troubleshooting

  1. Check your Mailofly Logs to see if the webhook arrived or failed verification.
  2. Confirm your domain is Verified in Mailofly.
  3. In Supabase SQL Editor, verify that pg_net is working:
    Look for status 200 from app.mailofly.com.
Ensure the pgcrypto extension is installed in the extensions schema. Mailofly’s trigger generator sets search_path = public, extensions automatically. You can run:
Ensure the column names in your PostgreSQL table match the merge tag keys in your template verbatim. For example, if your table column is user_name, your template tag must be {{user_name}}.