Email Automation System
Relevant source files
The following files were used as context for generating this wiki page:
The Email Automation System is a centralized communications hub designed to handle all transactional and notification-based messaging for the Monarch Property Management platform. It leverages Supabase Edge Functions as the compute layer and Resend as the delivery provider to ensure high deliverability and secure communication between the platform and its users (Admins, Vendors, and Tenants).
The system supports complex workflows including vendor invitations, maintenance notifications, payment reminders, booking confirmations, and legal compliance alerts. By utilizing standardized branding and shared configurations, the system ensures a consistent professional appearance across all automated touchpoints.
System Architecture
The architecture follows a serverless pattern where the frontend or database triggers Edge Functions, which then process the request, validate user preferences, and interact with the Resend API.
The diagram above illustrates the flow from client triggers through specialized Edge Functions to the final delivery via Resend.
Sources: supabase/functions/send-email/index.ts:77-105, supabase/functions/_shared/emailConfig.ts:1-15
Core Components
1. Edge Function Handlers
Each primary email flow is managed by a specific Supabase Edge Function. These functions handle Zod-based input validation, authentication checks, and rate limiting.
- General Notifier (
send-email): A versatile function that supports multiple templates (vendor-invite, maintenance, test, welcome, project_assignment). Sources: supabase/functions/send-email/index.ts:20-40 - Invoice Delivery (
send-invoice): Handles financial communications, including line-item formatting and payment URL generation. Sources: supabase/functions/send-invoice/index.ts:38-60 - User Onboarding (
send-welcome-email): A specialized function for new user registration. Sources: supabase/functions/send-welcome-email/index.ts:15-30
2. Branding and Styling (emailHeader.ts)
Standardized styling is applied to all emails to maintain brand identity. The BRAND_COLORS object defines a palette including Gold (#D4AF37) and Charcoal (#2C2C2C).
| Element | Description |
|---|---|
LOGO_URL | Hosted Monarch logo used in headers. |
emailStyles | Global CSS including responsive wrappers and button styling. |
generateEmailFooter | Standard footer with privacy links and copyright. |
wrapEmailContent | Helper to wrap body content in standard header/footer. |
Sources: supabase/functions/_shared/emailHeader.ts:3-30, supabase/functions/_shared/emailHeader.ts:175-190
Data Models and Configuration
Email Configuration (emailConfig.ts)
The system uses a centralized configuration to manage sender identities and anti-spam measures.
Database Templates
Critical email content is stored and versioned in the email_templates table via SQL migrations. This allows for dynamic variable injection (e.g., {{name}}, {{rfq_title}}).
| Template Name | Purpose | Key Variables |
|---|---|---|
welcome_email | New account greeting | name, dashboard_url |
bid_confirmation | Vendor bid receipt | vendor_name, rfq_title, bid_amount |
rfq_invitation | Invite vendor to bid | rfq_title, budget_min, budget_max |
contract_award | Notification of win | contract_title, contract_value |
compliance_expiry | Security warning | document_name, expiry_date |
Sources: supabase/migrations/20251228080428_f95dcabc-4c93-43af-a1d9-2553a871c206.sql:3-150
Security and Compliance
The system implements multiple layers of protection to ensure deliverability and user privacy:
- Anti-Spam Headers: The
getAntiSpamHeadersutility generatesList-UnsubscribeandX-Entity-Ref-IDheaders required by major providers like Gmail and Yahoo. Sources: supabase/functions/_shared/emailConfig.ts:51-70 - User Preferences: Before sending, the
send-emailfunction queriesuser_notification_settingsto verify if the recipient has disabled specific notification types (e.g.,payment_alerts,project_updates). Sources: supabase/functions/send-email/index.ts:133-165 - Rate Limiting: Implementation of per-user and per-IP rate limits (e.g., 5 emails per minute) to prevent abuse and protect API quotas. Sources: supabase/functions/send-email/index.ts:55-75, supabase/functions/send-welcome-email/index.ts:10-14
- Audit Logging: Every email sent is logged in the
sent_emailstable, and errors trigger entries insecurity_auditlogs for administrative review. Sources: supabase/functions/send-email/index.ts:175-185, supabase/functions/send-email/index.ts:233-255
Sequence of a Triggered Email
Summary
The Email Automation System provides a robust, branded, and secure framework for all Monarch Property Management communications. By combining centralized configuration in emailConfig.ts with flexible templates in the database and strict preference checking in the Edge Functions, the system balances high-volume automated messaging with individual user privacy and platform security. The use of Resend and standardized anti-spam headers ensures that critical notifications like contract awards and payment reminders reach users' primary inboxes reliably.