-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathindex.tsx
More file actions
124 lines (109 loc) · 3.39 KB
/
Copy pathindex.tsx
File metadata and controls
124 lines (109 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React from 'react';
import { render } from '@react-email/render';
import { createTransport } from 'nodemailer';
import { Resend } from 'resend';
import type { z } from 'zod';
import { db } from '@openpanel/db';
import { type TemplateKey, type Templates, templates } from './emails';
import { getUnsubscribeUrl } from './unsubscribe';
export * from './unsubscribe';
const FROM = process.env.EMAIL_SENDER ?? 'hello@openpanel.dev';
export type EmailData<T extends TemplateKey> = z.infer<Templates[T]['schema']>;
export type EmailTemplate = keyof Templates;
function createSmtpTransport() {
return createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT ? Number(process.env.SMTP_PORT) : 587,
secure: process.env.SMTP_SECURE === 'true',
auth:
process.env.SMTP_USER && process.env.SMTP_PASS
? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS }
: undefined,
connectionTimeout: 10_000,
greetingTimeout: 10_000,
socketTimeout: 30_000,
});
}
export async function sendEmail<T extends TemplateKey>(
templateKey: T,
options: {
to: string;
data: z.infer<Templates[T]['schema']>;
},
) {
const { to, data } = options;
const template = templates[templateKey];
const props = template.schema.safeParse(data);
if (!props.success) {
console.error('Failed to parse data', props.error);
return null;
}
if ('category' in template && template.category) {
const unsubscribed = await db.emailUnsubscribe.findUnique({
where: {
email_category: {
email: to,
category: template.category,
},
},
});
if (unsubscribed) {
console.log(
`Skipping email to ${to} - unsubscribed from ${template.category}`,
);
return null;
}
}
const headers: Record<string, string> = {};
if ('category' in template && template.category) {
const unsubscribeUrl = getUnsubscribeUrl(to, template.category);
(props.data as any).unsubscribeUrl = unsubscribeUrl;
headers['List-Unsubscribe'] = `<${unsubscribeUrl}>`;
headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
}
const subject = template.subject(props.data as any);
if (process.env.SMTP_HOST) {
try {
const html = await render(
<template.Component {...(props.data as any)} />,
);
const transport = createSmtpTransport();
const res = await transport.sendMail({
from: FROM,
to,
subject,
html,
headers,
});
return res;
} catch (error) {
console.error('Failed to send email via SMTP', error);
return null;
}
}
if (!process.env.RESEND_API_KEY) {
console.log('No SMTP_HOST or RESEND_API_KEY found, here is the data');
console.log('Template:', template);
console.log('Subject: ', subject);
console.log('To: ', to);
console.log('Data: ', JSON.stringify(data, null, 2));
return null;
}
const resend = new Resend(process.env.RESEND_API_KEY);
try {
const res = await resend.emails.send({
from: FROM,
to,
subject,
react: <template.Component {...(props.data as any)} />,
headers: Object.keys(headers).length > 0 ? headers : undefined,
});
if (res.error) {
throw new Error(res.error.message);
}
return res;
} catch (error) {
console.error('Failed to send email via Resend', error);
return null;
}
}