window.pageBoot = async (me) => {
const [acc, tpl] = await Promise.all([GET("/test-send/accounts"), GET("/test-send/templates")]);
const accSel = $("#account"), tplSel = $("#template");
(acc.accounts || []).forEach(a => accSel.append(el("option", {value:a.id}, `${a.email} (${(a.sending_method||"api").toUpperCase()})`)));
(tpl.templates || []).forEach(t => tplSel.append(el("option", {value:t.id}, t.name)));
if (!(acc.accounts||[]).length)
$("#result").innerHTML = `
No authorized Gmail accounts available. Connect and authorize an account first (Gmail Accounts page).
`;
if (!(tpl.templates||[]).length)
tplSel.append(el("option", {value:""}, "— none —"));
$("#srcMode").addEventListener("change", () => {
const tplMode = $("#srcMode").value === "template";
$("#tplWrap").style.display = tplMode ? "" : "none";
$("#rawWrap").style.display = tplMode ? "none" : "";
});
$("#form").addEventListener("submit", async (e) => {
e.preventDefault();
const btn = $("#sendBtn"); btn.disabled = true; btn.textContent = "Sending via Gmail…";
$("#result").innerHTML = `Sending — this performs a real send through the Gmail API/SMTP of the selected account…
`;
const payload = {
account_id: +$("#account").value,
to_email: $("#to").value.trim(),
subject: $("#subject").value.trim(),
include_pdf: $("#incPdf").checked,
use_random_variables: $("#useVars").checked,
};
if ($("#srcMode").value === "template") {
if (!$("#template").value) { toast("Select a template or switch to raw HTML","err"); btn.disabled=false; btn.textContent="Send Test Email"; return; }
payload.template_id = +$("#template").value;
} else {
payload.body_html = $("#bodyHtml").value;
}
try {
const d = await POST("/test-send/send", payload);
const ok = d.ok;
const kv = [
["Result", ok ? "ACCEPTED by Gmail" : (d.stage || "FAILED")],
["Detail", d.detail || ""],
["Message ID", d.message_id || "—"],
["Thread ID", d.thread_id || "—"],
["Method", d.method ? d.method.toUpperCase() : "—"],
["Provider response", d.provider_response || "—"],
["Elapsed", d.elapsed_ms != null ? d.elapsed_ms + " ms" : "—"],
];
const rows = kv.map(([k,v]) => v ? `${esc(k)}
${esc(v)}
` : "").join("");
$("#result").innerHTML = `
${ok ? "✓ Gmail accepted the message" : "✕ Gmail did not accept the message"}
${rows}
${ok ? "" : `
The system never reports fake success — this is the provider's real response.
`}
`;
toast(ok ? "Test email accepted by Gmail" : "Test send failed (see details)", ok ? "ok" : "err", 6000);
} catch (err) {
$("#result").innerHTML = ``;
toast("Test send error: " + err.message, "err", 7000);
} finally {
btn.disabled = false; btn.textContent = "Send Test Email";
}
});
};
===HTML buttons===
31:
33:
35:
37:
43:
49:
50:
52: