///
// Sends PocketBase's email through exe.dev's email gateway until you turn on
// SMTP in Settings > Mail settings. The gateway needs no password, but it
// sends plain text, and only to you, your exe.dev team, and people you've
// shared this VM with: enough for superuser password resets and
// alerts to you, not for your app's users. https://exe.dev/docs/send-email
//
// Saved as ~/pocketbase/pb_hooks/exe-email.pb.js. From
// https://exe.dev/deploy/pocketbase
onMailerSend((e) => {
if (e.app.settings().smtp.enabled) {
return e.next() // SMTP is set up: send the usual way
}
const m = e.message
const text = m.text || (m.html || "")
.replace(/\s+/g, " ")
// keep links: Verify becomes "Verify: URL"
.replace(/]*?href="([^"]*)"[^>]*>(.*?)<\/a>/gi, "$2: $1")
.replace(/
]*>/gi, "\n")
.replace(/<\/(p|div|h\d|li|tr)>/gi, "\n\n")
.replace(/<[^>]*>/g, "")
.replace(/ /g, " ").replace(/</g, "<").replace(/>/g, ">")
.replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&")
.replace(/ *\n */g, "\n").replace(/\n{3,}/g, "\n\n").trim()
for (const to of m.to) {
const res = $http.send({
url: "http://169.254.169.254/gateway/email/send",
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ to: to.address, subject: m.subject, body: text }),
timeout: 30,
})
if (res.statusCode !== 200) {
throw new Error("exe.dev email gateway: HTTP " + res.statusCode + " " + JSON.stringify(res.json))
}
e.app.logger().info("Sent email through exe.dev", "to", to.address, "subject", m.subject)
}
})