Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add send-email-smtp edge function example

thorwebdev035e6c13c557
diff --git a/examples/edge-functions/README.md b/examples/edge-functions/README.md
index e0db8539038..6a49a8cfe31 100644
--- a/examples/edge-functions/README.md
+++ b/examples/edge-functions/README.md
@@ -10,6 +10,7 @@ The function examples are located in [`./supabase/functions`](./supabase/functio
- [`browser-with-cors`](./supabase/functions/browser-with-cors/index.ts): Handle CORS headers for function invocations from browser environments.
- [`select-from-table-with-auth-rls`](./supabase/functions/select-from-table-with-auth-rls/index.ts): Retrieve data from an authenticated user via RLS.
+- [`send-email-smtp`](./supabase/functions/send-email-smtp/index.ts): Send an email using SMTP credentials.
## Develop locally
diff --git a/examples/edge-functions/app/src/functionsList.js b/examples/edge-functions/app/src/functionsList.js
index aad5982b277..a3f5ba13780 100644
--- a/examples/edge-functions/app/src/functionsList.js
+++ b/examples/edge-functions/app/src/functionsList.js
@@ -1,5 +1,6 @@
export const functionsList = [
'local: Whatever function is currently served by the CLI',
'browser-with-cors',
- 'select-from-table-with-auth-rls',
+ 'select-from-table-with-auth-rls',
+ 'send-email-smtp',
]
diff --git a/examples/edge-functions/supabase/.env.local.example b/examples/edge-functions/supabase/.env.local.example
new file mode 100644
index 00000000000..d99d5930180
--- /dev/null
+++ b/examples/edge-functions/supabase/.env.local.example
@@ -0,0 +1,5 @@
+SMTP_HOSTNAME="your.hostname.com"
+SMTP_PORT="2587"
+SMTP_USERNAME="your_username"
+SMTP_PASSWORD="your_password"
+SMTP_FROM="[email protected]"
\ No newline at end of file
diff --git a/examples/edge-functions/supabase/functions/send-email-smtp/README.md b/examples/edge-functions/supabase/functions/send-email-smtp/README.md
new file mode 100644
index 00000000000..02e6d5a6b08
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/send-email-smtp/README.md
@@ -0,0 +1,16 @@
+# send-email-smtp
+
+## Deploy
+
+```
+supabase link --project-ref your-project-ref
+supabase secrets set SMTP_HOSTNAME="your.hostname.com" SMTP_PORT="2587" SMTP_USERNAME="your_username" SMTP_PASSWORD="your_password" SMTP_FROM="[email protected]"
+supabase functions deploy send-email-smtp
+```
+
+Note: `SMTP_PORT` must be a port other than `25`, `465`, and `587` as Deno Deploy does not support outgoing connections to ports. AWS SES (port 2587) is recommended.
+
+## Test locally
+
+- `cp ./supabase/.env.local.example ./supabase/.env.local`
+- `supabase functions serve send-email-smtp --env-file ./supabase/.env.local`
\ No newline at end of file
diff --git a/examples/edge-functions/supabase/functions/send-email-smtp/index.ts b/examples/edge-functions/supabase/functions/send-email-smtp/index.ts
new file mode 100644
index 00000000000..5fa1615629c
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/send-email-smtp/index.ts
@@ -0,0 +1,45 @@
+// Follow this setup guide to integrate the Deno language server with your editor:
+// https://deno.land/manual/getting_started/setup_your_environment
+// This enables autocomplete, go to definition, etc.
+
+import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+import { SmtpClient } from 'https://deno.land/x/[email protected]/mod.ts'
+
+const smtp = new SmtpClient()
+
+serve(async (req) => {
+ await smtp.connect({
+ hostname: Deno.env.get('SMTP_HOSTNAME')!,
+ port: Number(Deno.env.get('SMTP_PORT')!),
+ username: Deno.env.get('SMTP_USERNAME')!,
+ password: Deno.env.get('SMTP_PASSWORD')!,
+ })
+
+ try {
+ await smtp.send({
+ from: Deno.env.get('SMTP_FROM')!,
+ subject: `Hello from Supabase Edge Functions`,
+ content: `Hello Functions \o/`,
+ })
+ } catch (error: any) {
+ return new Response(error.message, { status: 500 })
+ }
+
+ await smtp.close()
+
+ return new Response(
+ JSON.stringify({
+ done: true,
+ }),
+ {
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+})
+
+// To invoke:
+// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \
+// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \
+// --header 'Content-Type: application/json' \
+// --data '{"name":"Functions"}'