Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ CONCURRENCY="10"
NEXT_PUBLIC_DASHBOARD_URL="http://localhost:3000"
NEXT_PUBLIC_API_URL="http://localhost:3333"
WORKER_PORT=9999
API_PORT=3333
API_PORT=3333

# EMAIL (pick one)
# Option A - Resend
# RESEND_API_KEY=""
# EMAIL_SENDER="hello@openpanel.dev"
# Option B - SMTP
# SMTP_HOST=""
# SMTP_PORT="587"
# SMTP_SECURE="false"
# SMTP_USER=""
# SMTP_PASS=""
# EMAIL_SENDER="hello@openpanel.dev"
3 changes: 3 additions & 0 deletions packages/email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"dependencies": {
"@openpanel/db": "workspace:*",
"@react-email/components": "^0.5.6",
"@react-email/render": "^2.0.7",
"@types/nodemailer": "^8.0.0",
"nodemailer": "^8.0.5",
"react": "catalog:",
"react-dom": "catalog:",
"resend": "^4.0.1",
Expand Down
63 changes: 51 additions & 12 deletions packages/email/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import { render } from '@react-email/render';
import { createTransport } from 'nodemailer';
import { Resend } from 'resend';
import type { z } from 'zod';

Expand All @@ -13,6 +15,21 @@ 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,
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

export async function sendEmail<T extends TemplateKey>(
templateKey: T,
options: {
Expand Down Expand Up @@ -47,30 +64,52 @@ export async function sendEmail<T extends TemplateKey>(
}
}

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 RESEND_API_KEY found, here is the data');
console.log('No SMTP_HOST or RESEND_API_KEY found, here is the data');
console.log('Template:', template);
console.log('Subject: ', template.subject(props.data as any));
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);

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';
}

try {
const res = await resend.emails.send({
from: FROM,
to,
subject: template.subject(props.data as any),
subject,
react: <template.Component {...(props.data as any)} />,
headers: Object.keys(headers).length > 0 ? headers : undefined,
});
Expand All @@ -79,7 +118,7 @@ export async function sendEmail<T extends TemplateKey>(
}
return res;
} catch (error) {
console.error('Failed to send email', error);
console.error('Failed to send email via Resend', error);
return null;
}
}
Loading