diff --git a/supabase/functions/send-smtp-email/index.ts b/supabase/functions/send-smtp-email/index.ts index 55f22e9..2f9787d 100644 --- a/supabase/functions/send-smtp-email/index.ts +++ b/supabase/functions/send-smtp-email/index.ts @@ -87,6 +87,13 @@ Deno.serve(async (req) => { if (settings.port === 465) useTls = true; else if (settings.port === 587 || settings.port === 25) useTls = false; + console.log("SMTP connecting", { + host: settings.host, + port: settings.port, + tls: useTls, + hasAuth: !!settings.username, + }); + const client = new SMTPClient({ connection: { hostname: settings.host, @@ -96,6 +103,8 @@ Deno.serve(async (req) => { ? { username: settings.username, password } : undefined, }, + debug: { log: true, allowUnsecure: false, encodeLB: true, noStartTLS: false }, + pool: { size: 1, timeout: 20_000 }, }); fromAddr = settings.from_name @@ -112,7 +121,10 @@ Deno.serve(async (req) => { // dedupe bccList = Array.from(new Set(bccList)); - await client.send({ + console.log("SMTP sending", { from: fromAddr, to: toList, cc: ccList, bcc: bccList }); + + // Race the send against a timeout so we get a useful error instead of a silent hang + const sendPromise = client.send({ from: fromAddr, to: toList, cc: ccList.length ? ccList : undefined, @@ -122,7 +134,16 @@ Deno.serve(async (req) => { content: body.text ?? "auto", html: body.html, }); - await client.close(); + const timeoutPromise = new Promise((_, reject) => + setTimeout(() => reject(new Error("SMTP send timed out after 25s")), 25_000), + ); + await Promise.race([sendPromise, timeoutPromise]); + console.log("SMTP send OK"); + try { + await client.close(); + } catch (closeErr) { + console.warn("SMTP close error (non-fatal)", closeErr instanceof Error ? closeErr.message : closeErr); + } // Log success await admin.from("email_logs").insert({