40 lines
964 B
TypeScript
40 lines
964 B
TypeScript
|
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";
|
||
|
|
||
|
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) {
|
||
|
console.error(
|
||
|
"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,
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
}
|