From 9f8ee0d7d26141e0b527c698c91afe30acfc676d Mon Sep 17 00:00:00 2001 From: dwindown Date: Sat, 3 Jan 2026 09:06:27 +0700 Subject: [PATCH] Add Mailketing API support to send-notification function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The send-notification function was using SMTP by default and failing with ConnectionRefused errors. Added Mailketing API integration to match the email provider used by other functions (send-auth-otp, send-email-v2). Changes: - Added sendViaMailketing() function with form-encoded body - Added "mailketing" case to provider switch (now the default) - Changed default provider from "smtp" to "mailketing" - Reads API token from mailketing_api_token or api_token settings This ensures order_created emails are sent successfully via Mailketing just like other email notifications in the system. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- supabase/functions/send-notification/index.ts | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/supabase/functions/send-notification/index.ts b/supabase/functions/send-notification/index.ts index ef217ed..0bbca82 100644 --- a/supabase/functions/send-notification/index.ts +++ b/supabase/functions/send-notification/index.ts @@ -33,6 +33,36 @@ interface EmailPayload { from_email: string; } +// Send via Mailketing API +async function sendViaMailketing(payload: EmailPayload, apiToken: string): Promise { + const params = new URLSearchParams(); + params.append('api_token', apiToken); + params.append('from_name', payload.from_name); + params.append('from_email', payload.from_email); + params.append('recipient', payload.to); + params.append('subject', payload.subject); + params.append('content', payload.html); + + console.log(`Sending email via Mailketing to ${payload.to}`); + + const response = await fetch('https://api.mailketing.co.id/api/v1/send', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: params.toString(), + }); + + if (!response.ok) { + const errorText = await response.text(); + console.error('Mailketing API error:', response.status, errorText); + throw new Error(`Mailketing API error: ${response.status} ${errorText}`); + } + + const result = await response.json(); + console.log('Mailketing API response:', result); +} + // Send via SMTP async function sendViaSMTP(payload: EmailPayload, config: SMTPConfig): Promise { const boundary = "----=_Part_" + Math.random().toString(36).substr(2, 9); @@ -284,10 +314,16 @@ serve(async (req: Request): Promise => { }; // Determine provider and send - const provider = settings.integration_email_provider || "smtp"; + const provider = settings.integration_email_provider || "mailketing"; console.log(`Sending email via ${provider} to ${recipient_email}`); switch (provider) { + case "mailketing": + const mailketingToken = settings.mailketing_api_token || settings.api_token; + if (!mailketingToken) throw new Error("Mailketing API token not configured"); + await sendViaMailketing(emailPayload, mailketingToken); + break; + case "smtp": await sendViaSMTP(emailPayload, { host: settings.smtp_host,