2022-10-07 17:09:13 -05:00
|
|
|
import Mailgun from "https://deno.land/x/mailgun@v1.1.1/index.ts";
|
|
|
|
import { type Message } from "https://deno.land/x/mailgun@v1.1.1/types.ts";
|
2022-10-11 12:20:25 -05:00
|
|
|
import * as log from "$std/log/mod.ts";
|
2022-10-07 17:09:13 -05:00
|
|
|
|
|
|
|
export type Email = Message;
|
|
|
|
|
|
|
|
const MAILGUN_API_KEY = Deno.env.get("MAILGUN_API_KEY");
|
|
|
|
const MAILGUN_DOMAIN = Deno.env.get("MAILGUN_DOMAIN");
|
|
|
|
|
|
|
|
if (!MAILGUN_API_KEY) {
|
2022-10-11 12:20:25 -05:00
|
|
|
log.error(
|
2022-10-07 17:09:13 -05:00
|
|
|
"MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MAILGUN_DOMAIN) {
|
|
|
|
console.error(
|
|
|
|
"MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mailgun = new Mailgun({
|
|
|
|
key: "YOUR_API_KEY",
|
|
|
|
region: "us",
|
|
|
|
domain: "YOUR_DOMAIN",
|
|
|
|
});
|
|
|
|
|
|
|
|
export async function send(email: Email) {
|
|
|
|
console.debug("Email:", email);
|
|
|
|
if (MAILGUN_API_KEY && MAILGUN_DOMAIN) {
|
|
|
|
return await mailgun.send(email);
|
|
|
|
} else {
|
|
|
|
return new Response(
|
|
|
|
"Email client is not properly configured. Email not sent.",
|
|
|
|
{
|
|
|
|
status: 401,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|