Move most email sending to common email function

This commit is contained in:
2023-10-19 16:51:00 -04:00
parent 86109eb770
commit b5a92db954
3 changed files with 37 additions and 31 deletions

View File

@ -1,27 +1,21 @@
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";
export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
const body = new FormData();
body.append("from", "noreply@mail.carcrushers.cc");
body.append("to", appeal.user.email);
body.append("subject", "Appeal Denied");
body.append("template", "appeal_denied");
body.append("v:note", context.data.body.feedback || "No note provided.");
const emailReq = await fetch(
"https://api.mailgun.net/v3/mail.carcrushers.cc/messages",
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Denied",
"appeal_denied",
{
body,
headers: {
authorization: `Basic ${btoa("api:" + context.env.MAILGUN_API_KEY)}`,
},
method: "POST",
note: context.data.body.feedback || "No note provided.",
},
);
if (!emailReq.ok) {
console.log(await emailReq.json());
if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to deny appeal", 500);
}