Add Mailketing API support to send-notification function

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 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-01-03 09:06:27 +07:00
parent 1fbaf4d360
commit 9f8ee0d7d2

View File

@@ -33,6 +33,36 @@ interface EmailPayload {
from_email: string;
}
// Send via Mailketing API
async function sendViaMailketing(payload: EmailPayload, apiToken: string): Promise<void> {
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<void> {
const boundary = "----=_Part_" + Math.random().toString(36).substr(2, 9);
@@ -284,10 +314,16 @@ serve(async (req: Request): Promise<Response> => {
};
// 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,