Files
meet-hub/supabase/functions/send-email-v2/index.ts

101 lines
3.3 KiB
TypeScript

import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};
interface EmailRequest {
recipient: string;
api_token: string;
from_name: string;
from_email: string;
subject: string;
content: string;
}
// Send via Mailketing API
async function sendViaMailketing(request: EmailRequest): Promise<{ success: boolean; message: string }> {
const { recipient, api_token, from_name, from_email, subject, content } = request;
// Build form-encoded body (http_build_query format)
const params = new URLSearchParams();
params.append('api_token', api_token);
params.append('from_name', from_name);
params.append('from_email', from_email);
params.append('recipient', recipient);
params.append('subject', subject);
params.append('content', content);
console.log(`Sending email via Mailketing to ${recipient}`);
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);
return {
success: true,
message: result.response || 'Email sent successfully via Mailketing'
};
}
serve(async (req: Request): Promise<Response> => {
if (req.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
try {
const body: EmailRequest = await req.json();
// Validate required fields
if (!body.recipient || !body.api_token || !body.from_name || !body.from_email || !body.subject || !body.content) {
return new Response(
JSON.stringify({ success: false, message: "Missing required fields: recipient, api_token, from_name, from_email, subject, content" }),
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(body.recipient) || !emailRegex.test(body.from_email)) {
return new Response(
JSON.stringify({ success: false, message: "Invalid email format" }),
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
console.log(`Attempting to send email to: ${body.recipient}`);
console.log(`From: ${body.from_name} <${body.from_email}>`);
console.log(`Subject: ${body.subject}`);
const result = await sendViaMailketing(body);
return new Response(
JSON.stringify(result),
{ status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
} catch (error: any) {
console.error("Error sending email:", error);
return new Response(
JSON.stringify({
success: false,
message: error.message || "Failed to send email"
}),
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
});