import { API_URL } from "https://esm.town/v/std/API_URL";
import { parseSendGridEmail } from "https://esm.town/v/stevekrouse/parseSendGridEmail?v=8";
import { parseSendGridEmails } from "https://esm.town/v/stevekrouse/parseSendGridEmails?v=10";
export const email = async (data: {
to?: (IAddress | string)[] | IAddress | string;
from?: IAddress | string;
cc?: (IAddress | string)[] | IAddress | string;
bcc?: (IAddress | string)[] | IAddress | string;
subject?: string;
replyTo?: (IAddress | string)[] | IAddress | string;
html?: string;
text?: string;
attachments?: AttachmentData[];
headers?: Record<string, string>;
}) => {
let result = await fetch(
`${API_URL}/v1/email`,
{
method: "POST",
body: JSON.stringify({
to: parseSendGridEmails(data.to),
from: data.from && parseSendGridEmail(data.from),
cc: parseSendGridEmails(data.cc),
bcc: parseSendGridEmails(data.bcc),
replyToList: parseSendGridEmails(data.replyTo),
subject: data.subject,
html: data.html,
text: data.text,
attachments: data.attachments,
headers: data.headers,
}),
headers: {
authorization: "Bearer " + Deno.env.get("valtown"),
},
},
).then(r => r.json());
if (result?.message !== "Email accepted to be sent") {
let message = result?.message ?? result?.error ?? JSON.stringify(result);
throw new Error("Val Town Email Error: " + message);
}
return result;
};
export interface IAddress {
email: string;
name?: string;