This guide explains how to send emails to logged-in users using Supabase Edge Functions and Resend.
- Supabase Project: You already have this.
- Resend Account: Sign up at resend.com (Free tier allows 3000 emails/mo).
- Supabase CLI: You need this installed on your machine to deploy functions.
If you haven't already:
brew install supabase/tap/supabasesupabase loginFollow the browser instructions to log in.
Inside your project root (brAIn folder):
supabase initYou need to give your Supabase function access to your Resend API Key.
- Get your API Key from Resend Dashboard.
- Run this command (replace
YOUR_RESEND_KEY):
supabase secrets set RESEND_API_KEY=re_123456789I have created the function code for you in supabase/functions/send-email/index.ts.
To deploy it to the cloud:
supabase functions deploy send-emailYou can trigger this function directly from your app (e.g., when a user clicks a button).
import { supabase } from '../lib/supabase';
const sendWelcomeEmail = async () => {
const { data, error } = await supabase.functions.invoke('send-email', {
body: {
subject: 'Welcome to brAIn!',
html: '<h1>Welcome Cadet!</h1><p>Ready to learn?</p>'
},
});
if (error) console.error('Error:', error);
else console.log('Email sent:', data);
};You can set up a Database Webhook in the Supabase Dashboard to automatically call this function whenever a new user is created in the auth.users table.