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 @@ -15,4 +15,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 - set one or both; SMTP takes priority over Resend if SMTP_HOST is set
# Option A - Resend
# RESEND_API_KEY=""
# EMAIL_SENDER="hello@openpanel.dev"
# Option B - SMTP (used first when SMTP_HOST is present, even if RESEND_API_KEY is also set)
# SMTP_HOST=""
# SMTP_PORT="587"
# SMTP_SECURE="false"
# SMTP_USER=""
# SMTP_PASS=""
# EMAIL_SENDER="hello@openpanel.dev"
76 changes: 73 additions & 3 deletions apps/public/content/docs/self-hosting/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,85 @@ Get your API key from [resend.com](https://resend.com). Make sure to verify your
**Required**: No
**Default**: `hello@openpanel.dev`

Email address used as the sender for transactional emails.
Email address used as the sender for transactional emails. Applies to both Resend and SMTP transports.

**Example**:
```bash
EMAIL_SENDER=noreply@yourdomain.com
```

### SMTP_HOST

**Type**: `string`
**Required**: No
**Default**: None

SMTP server hostname. When set, OpenPanel uses SMTP to send emails instead of Resend. Takes priority over `RESEND_API_KEY` if both are configured.

**Example**:
```bash
SMTP_HOST=smtp.example.com
```

### SMTP_PORT

**Type**: `number`
**Required**: No
**Default**: `587`

SMTP server port.

**Example**:
```bash
SMTP_PORT=587
```

### SMTP_SECURE

**Type**: `boolean`
**Required**: No
**Default**: `false`

Use TLS for the SMTP connection. Set to `true` when using port `465`.

**Example**:
```bash
SMTP_SECURE=true
```

### SMTP_USER

**Type**: `string`
**Required**: No
**Default**: None

SMTP authentication username. Leave unset if your server does not require authentication.

**Example**:
```bash
SMTP_USER=smtp-user@example.com
```
Comment thread
coderabbitai[bot] marked this conversation as resolved.

### SMTP_PASS

**Type**: `string`
**Required**: No
**Default**: None

SMTP authentication password.

**Example**:
```bash
SMTP_PASS=your-smtp-password
```

<Callout>
The sender email must be verified in your Resend account.
Set `SMTP_HOST` to enable SMTP. If both `SMTP_HOST` and `RESEND_API_KEY` are present, SMTP takes priority. If neither is set, emails are logged to the console (useful for development).
In case of using Resend, the sender email must be verified in your Resend account.
</Callout>
Comment thread
coderabbitai[bot] marked this conversation as resolved.

<Callout type="warn">
If `SMTP_HOST` is set and an SMTP send attempt fails, the system will **not** automatically fall back to `RESEND_API_KEY` — the email will be silently dropped. To use Resend instead, unset `SMTP_HOST`.
</Callout>

## OAuth & Integrations
Expand Down Expand Up @@ -834,7 +904,7 @@ For a basic self-hosted installation, these variables are required:

### Optional but Recommended

- `RESEND_API_KEY` - For email features
- `RESEND_API_KEY` or `SMTP_HOST` - For email features (pick one)
- `EMAIL_SENDER` - Email sender address
- `OPENAI_API_KEY` and/or `ANTHROPIC_API_KEY` - For the in-app AI chat assistant

Expand Down
28 changes: 27 additions & 1 deletion apps/public/content/docs/self-hosting/self-hosting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const op = new OpenPanel({

### E-mail

Some of OpenPanel's features require e-mail. We use Resend as our transactional e-mail provider. To enable email features, create an account on Resend and set the `RESEND_API_KEY` environment variable.
Some of OpenPanel's features require e-mail. We use Resend as our transactional e-mail provider by default, but you can also use your own SMTP server. To enable email features, set the relevant environment variables described below.

<Callout>This is nothing that is required for the basic setup, but it is required for some features.</Callout>

Expand All @@ -112,6 +112,32 @@ Features that require e-mail:
- Invitations
- more will be added over time

#### Option A — Resend

Create an account on [resend.com](https://resend.com), verify your sender domain, and set the `RESEND_API_KEY` environment variable.

```bash title=".env"
RESEND_API_KEY=re_xxxxxxxxxxxxx
EMAIL_SENDER=noreply@yourdomain.com
```

#### Option B — SMTP

If you prefer to use your own SMTP server, set `SMTP_HOST` and OpenPanel will use it instead of Resend.

```bash title=".env"
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_SECURE=false
SMTP_USER=smtp-user@example.com
SMTP_PASS=your-smtp-password
EMAIL_SENDER=noreply@yourdomain.com
```

<Callout type="info">
`SMTP_HOST` takes priority over `RESEND_API_KEY` if both are set. If neither is configured, emails are logged to the console instead of being sent.
</Callout>
Comment thread
coderabbitai[bot] marked this conversation as resolved.

For email configuration details, see the [Environment Variables documentation](/docs/self-hosting/environment-variables#email).

### AI integration
Expand Down
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