Fix email unconfirmed login flow with OTP resend and update email API field names

This commit is contained in:
dwindown
2026-01-02 19:33:51 +07:00
parent 8f46c5cfd9
commit eee6339074
13 changed files with 668 additions and 65 deletions

View File

@@ -6,33 +6,35 @@ const corsHeaders = {
};
interface EmailRequest {
to: string;
recipient: string;
api_token: string;
from_name: string;
from_email: string;
subject: string;
html_body: string;
content: string;
}
// Send via Mailketing API
async function sendViaMailketing(request: EmailRequest): Promise<{ success: boolean; message: string }> {
const { to, api_token, from_name, from_email, subject, html_body } = request;
const { recipient, api_token, from_name, from_email, subject, content } = request;
const formData = new FormData();
formData.append('to', to);
formData.append('from_name', from_name);
formData.append('from_email', from_email);
formData.append('subject', subject);
formData.append('html_body', html_body);
// 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 ${to}`);
console.log(`Sending email via Mailketing to ${recipient}`);
const response = await fetch('https://api.mailketing.co/v1/send', {
const response = await fetch('https://api.mailketing.co.id/api/v1/send', {
method: 'POST',
headers: {
'Authorization': `Bearer ${api_token}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formData,
body: params.toString(),
});
if (!response.ok) {
@@ -46,7 +48,7 @@ async function sendViaMailketing(request: EmailRequest): Promise<{ success: bool
return {
success: true,
message: result.message || 'Email sent successfully via Mailketing'
message: result.response || 'Email sent successfully via Mailketing'
};
}
@@ -59,23 +61,23 @@ serve(async (req: Request): Promise<Response> => {
const body: EmailRequest = await req.json();
// Validate required fields
if (!body.to || !body.api_token || !body.from_name || !body.from_email || !body.subject || !body.html_body) {
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: to, api_token, from_name, from_email, subject, html_body" }),
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.to) || !emailRegex.test(body.from_email)) {
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.to}`);
console.log(`Attempting to send email to: ${body.recipient}`);
console.log(`From: ${body.from_name} <${body.from_email}>`);
console.log(`Subject: ${body.subject}`);