MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / sendEmail

Function sendEmail

packages/email/src/index.tsx:33–124  ·  view source on GitHub ↗
(
  templateKey: T,
  options: {
    to: string;
    data: z.infer<Templates[T]['schema']>;
  },
)

Source from the content-addressed store, hash-verified

31}
32
33export async function sendEmail<T extends TemplateKey>(
34 templateKey: T,
35 options: {
36 to: string;
37 data: z.infer<Templates[T]['schema']>;
38 },
39) {
40 const { to, data } = options;
41 const template = templates[templateKey];
42 const props = template.schema.safeParse(data);
43
44 if (!props.success) {
45 console.error('Failed to parse data', props.error);
46 return null;
47 }
48
49 if ('category' in template && template.category) {
50 const unsubscribed = await db.emailUnsubscribe.findUnique({
51 where: {
52 email_category: {
53 email: to,
54 category: template.category,
55 },
56 },
57 });
58
59 if (unsubscribed) {
60 console.log(
61 `Skipping email to ${to} - unsubscribed from ${template.category}`,
62 );
63 return null;
64 }
65 }
66
67 const headers: Record<string, string> = {};
68 if ('category' in template && template.category) {
69 const unsubscribeUrl = getUnsubscribeUrl(to, template.category);
70 (props.data as any).unsubscribeUrl = unsubscribeUrl;
71 headers['List-Unsubscribe'] = `<${unsubscribeUrl}>`;
72 headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
73 }
74
75 const subject = template.subject(props.data as any);
76
77 if (process.env.SMTP_HOST) {
78 try {
79 const html = await render(
80 <template.Component {...(props.data as any)} />,
81 );
82 const transport = createSmtpTransport();
83 const res = await transport.sendMail({
84 from: FROM,
85 to,
86 subject,
87 html,
88 headers,
89 });
90 return res;

Callers 5

auth.tsFile · 0.90
organization.tsFile · 0.90
weeklyDigestCronJobFunction · 0.90
onboardingJobFunction · 0.90

Calls 7

getUnsubscribeUrlFunction · 0.90
createSmtpTransportFunction · 0.85
renderFunction · 0.50
errorMethod · 0.45
logMethod · 0.45
stringifyMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected