-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add SMTP support as alternative to Resend #1775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julianwitzel
wants to merge
1
commit into
CapSoftware:main
Choose a base branch
from
julianwitzel:feat/smtp-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,33 @@ | ||
| import { buildEnv, serverEnv } from "@cap/env"; | ||
| import { render } from "@react-email/render"; | ||
| import nodemailer from "nodemailer"; | ||
| import type { JSXElementConstructor, ReactElement } from "react"; | ||
| import { Resend } from "resend"; | ||
|
|
||
| export const resend = () => | ||
| serverEnv().RESEND_API_KEY ? new Resend(serverEnv().RESEND_API_KEY) : null; | ||
|
|
||
| let _smtpTransport: ReturnType<typeof nodemailer.createTransport> | null | undefined; | ||
|
|
||
| export const smtp = () => { | ||
| if (_smtpTransport !== undefined) return _smtpTransport; | ||
| const env = serverEnv(); | ||
| if (!env.SMTP_HOST) { | ||
| _smtpTransport = null; | ||
| return null; | ||
| } | ||
| _smtpTransport = nodemailer.createTransport({ | ||
| host: env.SMTP_HOST, | ||
| port: env.SMTP_PORT, | ||
| secure: env.SMTP_SECURE, | ||
| auth: | ||
| env.SMTP_USER && env.SMTP_PASS | ||
| ? { user: env.SMTP_USER, pass: env.SMTP_PASS } | ||
| : undefined, | ||
| }); | ||
| return _smtpTransport; | ||
| }; | ||
|
|
||
| export const sendEmail = async ({ | ||
| email, | ||
| subject, | ||
|
|
@@ -26,23 +49,51 @@ export const sendEmail = async ({ | |
| replyTo?: string; | ||
| fromOverride?: string; | ||
| }) => { | ||
| const r = resend(); | ||
| if (!r) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| if (marketing && !buildEnv.NEXT_PUBLIC_IS_CAP) return; | ||
| let from; | ||
|
|
||
| let from: string; | ||
| if (fromOverride) from = fromOverride; | ||
| else if (marketing) from = "Richie from Cap <[email protected]>"; | ||
| else if (buildEnv.NEXT_PUBLIC_IS_CAP) | ||
| from = "Cap Auth <[email protected]>"; | ||
| else from = `auth@${serverEnv().RESEND_FROM_DOMAIN}`; | ||
| else if (serverEnv().SMTP_FROM) from = serverEnv().SMTP_FROM!; | ||
| else if (serverEnv().RESEND_FROM_DOMAIN) | ||
| from = `auth@${serverEnv().RESEND_FROM_DOMAIN}`; | ||
| else { | ||
| // No from-address configured. Most SMTP servers reject "noreply@localhost" | ||
| // as invalid sender, which would silently drop the email. Fail loudly instead. | ||
| throw new Error( | ||
| "No email from-address configured. Set SMTP_FROM (when using SMTP) or RESEND_FROM_DOMAIN (when using Resend).", | ||
| ); | ||
| } | ||
|
|
||
| // Resend has a sandbox sink at [email protected] for test mode; | ||
| // over SMTP we just use the real recipient | ||
| const to = test && !smtp() ? "[email protected]" : email; | ||
|
|
||
| // Try SMTP first if configured (preferred for self-hosted) | ||
| const transport = smtp(); | ||
| if (transport) { | ||
| const html = await render(react); | ||
| return transport.sendMail({ | ||
| from, | ||
| to, | ||
| subject, | ||
| html, | ||
| cc: test ? undefined : cc, | ||
| replyTo, | ||
| }) as any; | ||
| } | ||
|
|
||
| // Fall back to Resend if configured | ||
| const r = resend(); | ||
| if (!r) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| return r.emails.send({ | ||
| from, | ||
| to: test ? "[email protected]" : email, | ||
| to, | ||
| subject, | ||
| react, | ||
| scheduledAt, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,15 @@ function createServerEnv() { | |
| RESEND_API_KEY: z.string().optional(), | ||
| RESEND_FROM_DOMAIN: z.string().optional(), | ||
|
|
||
| // SMTP configuration (alternative to Resend) | ||
| // If SMTP_HOST is set, emails will be sent via SMTP instead of Resend | ||
| SMTP_HOST: z.string().optional().describe("SMTP server hostname"), | ||
| SMTP_PORT: z.coerce.number().optional().default(587).describe("SMTP server port"), | ||
| SMTP_USER: z.string().optional().describe("SMTP username"), | ||
| SMTP_PASS: z.string().optional().describe("SMTP password"), | ||
| SMTP_FROM: z.string().optional().describe("Default From address (e.g. [email protected])"), | ||
| SMTP_SECURE: boolString(false).describe("Use TLS/SSL (true for port 465, false for 587/STARTTLS)"), | ||
|
|
||
| /// S3 configuration | ||
| // Though they are prefixed with `CAP_AWS`, these don't have to be | ||
| // for AWS, and can instead be for any S3-compatible service | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sendEmail()callsmtp()callsnodemailer.createTransport()afresh for every email, discarding the internal connection pool each time. Following the memoisation pattern already used forserverEnv()would keep a single pooled transporter alive for the process lifetime and avoid repeated setup overhead.Prompt To Fix With AI