Skip to content

Commit

Permalink
Add cloudflare worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Jul 25, 2024
1 parent e5484da commit d12ea05
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/lib/admin_mailer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Optional dependency, only used if platform suppport node.js (not Cloudflare Workers).
let nodemailer: typeof import("nodemailer") | undefined
try {
nodemailer = await import("nodemailer")
Expand All @@ -21,6 +22,20 @@ export const sendAdminEmail = async ({
return
}

if (nodemailer) {
return sendAdminEmailNodemailer({ subject, body })
} else {
return sendAdminEmailCloudflareWorkers({ subject, body })
}
}

const sendAdminEmailNodemailer = async ({
subject,
body,
}: {
subject: string
body: string
}) => {
if (!nodemailer) {
console.log(
"This environment does not support sending emails. Nodemailer requires node.js and doesn't work in environments like Cloudflare Workers.",
Expand Down Expand Up @@ -72,3 +87,42 @@ export const sendAdminEmail = async ({
console.log("Failed to send admin email, error:", e)
}
}

// https://blog.cloudflare.com/sending-email-from-workers-with-mailchannels/
const sendAdminEmailCloudflareWorkers = async ({
subject,
body,
}: {
subject: string
body: string
}) => {
const send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
personalizations: [
{
to: [{ email: env.PRIVATE_ADMIN_EMAIL }],
},
],
from: {
email: env.PRIVATE_ADMIN_EMAIL,
},
subject: "ADMIN_MAIL: " + subject,
content: [
{
type: "text/plain",
value: body,
},
],
}),
})

const response = await fetch(send_request)
if (!response.ok) {
console.log("Error sending admin email with MailChannels API", response)
return
}
}

0 comments on commit d12ea05

Please sign in to comment.