Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

setup mailhog

Kiran K07f4ba64a6c7
diff --git a/apps/web/.env.example b/apps/web/.env.example
index a83a428b55..6b1f002b56 100644
--- a/apps/web/.env.example
+++ b/apps/web/.env.example
@@ -43,6 +43,13 @@ NEXTAUTH_URL=http://localhost:8888 # (only needed for localhost)
# Get your Resend API Key here: https://resend.com/api-keys
RESEND_API_KEY=
+# SMTP configuration (Recommended for local development)
+SMTP_HOST=localhost
+SMTP_PORT=1025
+SMTP_SECURE_ENABLED=0
+SMTP_USER=smtpUser
+SMTP_PASSWORD=smtpPassword
+
###############################
###### OPTIONAL ENV VARS ######
###############################
diff --git a/apps/web/docker-compose.yml b/apps/web/docker-compose.yml
index 79c187d9ee..1aeca3974d 100644
--- a/apps/web/docker-compose.yml
+++ b/apps/web/docker-compose.yml
@@ -1,5 +1,6 @@
# This is meant for local development only. Do not use this in production.
version: "3.8"
+
services:
ps-mysql:
image: mysql:8.0
@@ -17,6 +18,7 @@ services:
- 3306:3306
volumes:
- ps-mysql:/var/lib/mysql
+
planetscale-proxy:
image: ghcr.io/mattrobenolt/ps-http-sim:latest
command:
@@ -32,5 +34,12 @@ services:
- 3900:3900
links:
- ps-mysql
+
+ mailhog:
+ image: "mailhog/mailhog:latest"
+ ports:
+ - "1025:1025"
+ - "8025:8025"
+
volumes:
- ps-mysql:
\ No newline at end of file
+ ps-mysql:
diff --git a/apps/web/emails/index.ts b/apps/web/emails/index.ts
index d7a90acd88..59d223ff8f 100644
--- a/apps/web/emails/index.ts
+++ b/apps/web/emails/index.ts
@@ -1,6 +1,51 @@
import { resend } from "@/lib/resend";
+import { render } from "@react-email/components";
+import nodemailer from "nodemailer";
+import { ReactElement } from "react";
import { CreateEmailOptions } from "resend";
+// Send email using SMTP (Recommended for local development)
+const sendEmailViaSMTP = async ({
+ email,
+ subject,
+ text,
+ react,
+}: Omit<CreateEmailOptions, "to" | "from"> & {
+ email: string;
+}) => {
+ const smtpConfigured = Boolean(
+ process.env.SMTP_HOST && process.env.SMTP_PORT,
+ );
+
+ if (!smtpConfigured) {
+ console.info("SMTP is not configured. Skipping sending email.");
+ return;
+ }
+
+ const transporter = nodemailer.createTransport({
+ host: process.env.SMTP_HOST,
+ port: process.env.SMTP_PORT,
+ auth: {
+ user: process.env.SMTP_USER,
+ pass: process.env.SMTP_PASSWORD,
+ },
+ secure: false,
+ tls: {
+ rejectUnauthorized: false,
+ },
+ });
+
+ const info = await transporter.sendMail({
+ to: email,
+ subject,
+ text,
+ html: render(react as ReactElement),
+ });
+
+ console.log("Email sent: %s", info.messageId);
+};
+
export const sendEmail = async ({
email,
subject,
@@ -17,12 +62,20 @@ export const sendEmail = async ({
replyToFromEmail?: boolean;
marketing?: boolean;
}) => {
- if (process.env.NODE_ENV === "development" && !resend) {
+ if (process.env.NODE_ENV === "development") {
+ return await sendEmailViaSMTP({
+ email,
+ subject,
+ text,
+ react,
+ });
+ }
+
+ if (!resend) {
console.info(
"RESEND_API_KEY is not set in the .env. Skipping sending email.",
);
-
- return Promise.resolve();
+ return;
}
return resend?.emails.send({