diff --git a/README.md b/README.md index d2d2e50..52da119 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ A production-ready Minecraft server administration panel built with Next.js, Bun | Auth | [Better Auth v1.5](https://better-auth.com) | | UI | [shadcn/ui v4](https://ui.shadcn.com) + [Base UI](https://base-ui.com) + Tailwind CSS v4 | | Real-time | [Socket.io](https://socket.io) | -| Email | [Resend](https://resend.com) | +| Email | [Nodemailer](https://nodemailer.com) (any SMTP server) | --- @@ -219,12 +219,59 @@ bun run db:studio | `NEXT_PUBLIC_BETTER_AUTH_URL` | No | *(inferred from browser)* | Browser-side auth URL. Only needed if the public URL differs from what the browser can infer (e.g. behind a proxy). | | `BETTER_AUTH_TRUSTED_ORIGINS` | No | `http://localhost:3000` | Comma-separated list of allowed origins for CORS and CSRF protection. Add your public domain in production. | -### Email +### Email (SMTP) + +CubeAdmin sends email via any standard SMTP server — Gmail, Mailgun, Postfix, Brevo, Amazon SES, or your own self-hosted relay. Email is only used for team invitations and magic-link sign-in. The app starts without email configured; invitations will fail gracefully with a logged error. | Variable | Required | Default | Description | |---|---|---|---| -| `RESEND_API_KEY` | No* | — | API key for [Resend](https://resend.com). Required for team invitation emails to work. The app starts without it, but invitations will fail silently. | -| `EMAIL_FROM` | No | `CubeAdmin ` | The sender address used for outgoing emails. Must be a verified address in your Resend account. | +| `SMTP_HOST` | No* | — | SMTP server hostname (e.g. `smtp.gmail.com`, `smtp.mailgun.org`, `localhost`). Required for any email to be sent. | +| `SMTP_PORT` | No | `587` | SMTP server port. Common values: `587` (STARTTLS), `465` (TLS/SSL), `25` (plain, relay), `1025` (local dev). | +| `SMTP_SECURE` | No | `false` | Set to `true` to use implicit TLS (port 465). Leave `false` for STARTTLS (port 587) or plain. | +| `SMTP_USER` | No | — | SMTP authentication username. Leave unset for unauthenticated relay (e.g. local Postfix). | +| `SMTP_PASS` | No | — | SMTP authentication password or app-specific password. | +| `EMAIL_FROM` | No | `CubeAdmin ` | The `From` address on outgoing emails. Must be authorised by your SMTP provider to avoid spam filtering. | + +#### Common SMTP configurations + +**Gmail** (app password required — enable 2FA first): +```env +SMTP_HOST=smtp.gmail.com +SMTP_PORT=587 +SMTP_SECURE=false +SMTP_USER=you@gmail.com +SMTP_PASS=your-app-password +EMAIL_FROM=CubeAdmin +``` + +**Mailgun**: +```env +SMTP_HOST=smtp.mailgun.org +SMTP_PORT=587 +SMTP_SECURE=false +SMTP_USER=postmaster@mg.yourdomain.com +SMTP_PASS=your-mailgun-smtp-password +EMAIL_FROM=CubeAdmin +``` + +**Amazon SES**: +```env +SMTP_HOST=email-smtp.us-east-1.amazonaws.com +SMTP_PORT=587 +SMTP_SECURE=false +SMTP_USER=your-ses-access-key-id +SMTP_PASS=your-ses-smtp-secret +EMAIL_FROM=CubeAdmin +``` + +**Local dev with Mailpit** (catches all emails, no real sending): +```env +SMTP_HOST=localhost +SMTP_PORT=1025 +SMTP_SECURE=false +# no SMTP_USER / SMTP_PASS needed +``` +Run Mailpit with `docker run -p 1025:1025 -p 8025:8025 axllent/mailpit` and view emails at [http://localhost:8025](http://localhost:8025). ### Minecraft Server @@ -303,7 +350,7 @@ Roles are assigned when inviting team members. The initial superadmin can promot │ ├── auth/ # Better Auth config + client │ ├── backup/ # Backup manager │ ├── db/ # Drizzle schema + migrations -│ ├── email/ # Resend email client +│ ├── email/ # Nodemailer SMTP client + email templates │ ├── minecraft/ # Process manager, RCON, version fetcher │ ├── security/ # Rate limiting │ └── socket/ # Socket.io server setup diff --git a/bun.lock b/bun.lock index 04445e8..997adc1 100644 --- a/bun.lock +++ b/bun.lock @@ -9,6 +9,7 @@ "@monaco-editor/react": "^4.7.0", "@tanstack/react-query": "^5.90.21", "@tanstack/react-table": "^8.21.3", + "@types/nodemailer": "^7.0.11", "@xterm/addon-fit": "^0.11.0", "@xterm/addon-search": "^0.16.0", "@xterm/addon-web-links": "^0.12.0", @@ -27,12 +28,12 @@ "next-safe-action": "^8.1.8", "next-themes": "^0.4.6", "node-cron": "^4.2.1", + "nodemailer": "^8.0.1", "rcon-client": "^4.2.5", "react": "19.2.3", "react-dom": "19.2.3", "react-dropzone": "^15.0.0", "recharts": "^3.8.0", - "resend": "^6.9.3", "semver": "^7.7.4", "shadcn": "^4.0.2", "socket.io": "^4.8.3", @@ -506,8 +507,6 @@ "@socket.io/component-emitter": ["@socket.io/component-emitter@3.1.2", "", {}, "sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA=="], - "@stablelib/base64": ["@stablelib/base64@1.0.1", "", {}, "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ=="], - "@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="], "@standard-schema/utils": ["@standard-schema/utils@0.3.0", "", {}, "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g=="], @@ -590,6 +589,8 @@ "@types/node-cron": ["@types/node-cron@3.0.11", "", {}, "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg=="], + "@types/nodemailer": ["@types/nodemailer@7.0.11", "", { "dependencies": { "@types/node": "*" } }, "sha512-E+U4RzR2dKrx+u3N4DlsmLaDC6mMZOM/TPROxA0UAPiTgI0y4CEFBmZE+coGWTjakDriRsXG368lNk1u9Q0a2g=="], + "@types/react": ["@types/react@19.2.14", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w=="], "@types/react-dom": ["@types/react-dom@19.2.3", "", { "peerDependencies": { "@types/react": "^19.2.0" } }, "sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ=="], @@ -1100,8 +1101,6 @@ "fast-levenshtein": ["fast-levenshtein@2.0.6", "", {}, "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw=="], - "fast-sha256": ["fast-sha256@1.3.0", "", {}, "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ=="], - "fast-uri": ["fast-uri@3.1.0", "", {}, "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA=="], "fastq": ["fastq@1.20.1", "", { "dependencies": { "reusify": "^1.0.4" } }, "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw=="], @@ -1520,6 +1519,8 @@ "node-releases": ["node-releases@2.0.36", "", {}, "sha512-TdC8FSgHz8Mwtw9g5L4gR/Sh9XhSP/0DEkQxfEFXOpiul5IiHgHan2VhYYb6agDSfp4KuvltmGApc8HMgUrIkA=="], + "nodemailer": ["nodemailer@8.0.1", "", {}, "sha512-5kcldIXmaEjZcHR6F28IKGSgpmZHaF1IXLWFTG+Xh3S+Cce4MiakLtWY+PlBU69fLbRa8HlaGIrC/QolUpHkhg=="], + "normalize-path": ["normalize-path@3.0.0", "", {}, "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA=="], "npm-run-path": ["npm-run-path@6.0.0", "", { "dependencies": { "path-key": "^4.0.0", "unicorn-magic": "^0.3.0" } }, "sha512-9qny7Z9DsQU8Ou39ERsPU4OZQlSTP47ShQzuKZ6PRXpYLtIFgl/DEBYEXKlvcEa+9tHVcK8CF81Y2V72qaZhWA=="], @@ -1608,8 +1609,6 @@ "possible-typed-array-names": ["possible-typed-array-names@1.1.0", "", {}, "sha512-/+5VFTchJDoVj3bhoqi6UeymcD00DAwb1nJwamzPvHEszJ4FpF6SNNbUbOS8yI56qHzdV8eK0qEfOSiodkTdxg=="], - "postal-mime": ["postal-mime@2.7.3", "", {}, "sha512-MjhXadAJaWgYzevi46+3kLak8y6gbg0ku14O1gO/LNOuay8dO+1PtcSGvAdgDR0DoIsSaiIA8y/Ddw6MnrO0Tw=="], - "postcss": ["postcss@8.5.8", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-OW/rX8O/jXnm82Ey1k44pObPtdblfiuWnrd8X7GJ7emImCOstunGbXUpp7HdBrFQX6rJzn3sPT397Wp5aCwCHg=="], "postcss-selector-parser": ["postcss-selector-parser@7.1.1", "", { "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" } }, "sha512-orRsuYpJVw8LdAwqqLykBj9ecS5/cRHlI5+nvTo8LcCKmzDmqVORXtOIYEEQuL9D4BxtA1lm5isAqzQZCoQ6Eg=="], @@ -1706,8 +1705,6 @@ "reselect": ["reselect@5.1.1", "", {}, "sha512-K/BG6eIky/SBpzfHZv/dd+9JBFiS4SWV7FIujVyJRux6e45+73RaUHXLmIR1f7WOMaQ0U1km6qwklRQxpJJY0w=="], - "resend": ["resend@6.9.3", "", { "dependencies": { "postal-mime": "2.7.3", "svix": "1.84.1" }, "peerDependencies": { "@react-email/render": "*" }, "optionalPeers": ["@react-email/render"] }, "sha512-GRXjH9XZBJA+daH7bBVDuTShr22iWCxXA8P7t495G4dM/RC+d+3gHBK/6bz9K6Vpcq11zRQKmD+B+jECwQlyGQ=="], - "resolve": ["resolve@1.22.11", "", { "dependencies": { "is-core-module": "^2.16.1", "path-parse": "^1.0.7", "supports-preserve-symlinks-flag": "^1.0.0" }, "bin": { "resolve": "bin/resolve" } }, "sha512-RfqAvLnMl313r7c9oclB1HhUEAezcpLjz95wFH4LVuhk9JF/r22qmVP9AMmOU4vMX7Q8pN8jwNg/CSpdFnMjTQ=="], "resolve-from": ["resolve-from@4.0.0", "", {}, "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g=="], @@ -1808,8 +1805,6 @@ "stable-hash": ["stable-hash@0.0.5", "", {}, "sha512-+L3ccpzibovGXFK+Ap/f8LOS0ahMrHTf3xu7mMLSpEGU0EO9ucaysSylKo9eRDFNhWve/y275iPmIZ4z39a9iA=="], - "standardwebhooks": ["standardwebhooks@1.0.0", "", { "dependencies": { "@stablelib/base64": "^1.0.0", "fast-sha256": "^1.3.0" } }, "sha512-BbHGOQK9olHPMvQNHWul6MYlrRTAOKn03rOe4A8O3CLWhNf4YHBqq2HJKKC+sfqpxiBY52pNeesD6jIiLDz8jg=="], - "state-local": ["state-local@1.0.7", "", {}, "sha512-HTEHMNieakEnoe33shBYcZ7NX83ACUjCu8c40iOGEZsngj9zRnkqS9j1pqQPXwobB0ZcVTk27REb7COQ0UR59w=="], "statuses": ["statuses@2.0.2", "", {}, "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw=="], @@ -1860,8 +1855,6 @@ "supports-preserve-symlinks-flag": ["supports-preserve-symlinks-flag@1.0.0", "", {}, "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w=="], - "svix": ["svix@1.84.1", "", { "dependencies": { "standardwebhooks": "1.0.0", "uuid": "^10.0.0" } }, "sha512-K8DPPSZaW/XqXiz1kEyzSHYgmGLnhB43nQCMeKjWGCUpLIpAMMM8kx3rVVOSm6Bo6EHyK1RQLPT4R06skM/MlQ=="], - "tabbable": ["tabbable@6.4.0", "", {}, "sha512-05PUHKSNE8ou2dwIxTngl4EzcnsCDZGJ/iCLtDflR/SHB/ny14rXc+qU5P4mG9JkusiV7EivzY9Mhm55AzAvCg=="], "tagged-tag": ["tagged-tag@1.0.0", "", {}, "sha512-yEFYrVhod+hdNyx7g5Bnkkb0G6si8HJurOoOEgC8B/O0uXLHlaey/65KRv6cuWBNhBgHKAROVpc7QyYqE5gFng=="], @@ -1956,8 +1949,6 @@ "util-deprecate": ["util-deprecate@1.0.2", "", {}, "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="], - "uuid": ["uuid@10.0.0", "", { "bin": { "uuid": "dist/bin/uuid" } }, "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ=="], - "valibot": ["valibot@1.2.0", "", { "peerDependencies": { "typescript": ">=5" }, "optionalPeers": ["typescript"] }, "sha512-mm1rxUsmOxzrwnX5arGS+U4T25RdvpPjPN4yR0u9pUBov9+zGVtO84tif1eY4r6zWxVxu3KzIyknJy3rxfRZZg=="], "validate-npm-package-name": ["validate-npm-package-name@7.0.2", "", {}, "sha512-hVDIBwsRruT73PbK7uP5ebUt+ezEtCmzZz3F59BSr2F6OVFnJ/6h8liuvdLrQ88Xmnk6/+xGGuq+pG9WwTuy3A=="], diff --git a/lib/email/index.ts b/lib/email/index.ts index 341a094..3831315 100644 --- a/lib/email/index.ts +++ b/lib/email/index.ts @@ -1,13 +1,24 @@ -import { Resend } from "resend"; +import nodemailer from "nodemailer"; import { render } from "@react-email/render"; import { InvitationEmail } from "./templates/invitation"; -function getResend(): Resend { - const key = process.env.RESEND_API_KEY; - if (!key) throw new Error("RESEND_API_KEY is not configured"); - return new Resend(key); +function createTransport() { + const host = process.env.SMTP_HOST; + if (!host) throw new Error("SMTP_HOST is not configured"); + + return nodemailer.createTransport({ + host, + port: parseInt(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, + }); } +const FROM = () => process.env.EMAIL_FROM ?? "CubeAdmin "; + export async function sendMagicLinkEmail({ email, url, @@ -17,14 +28,13 @@ export async function sendMagicLinkEmail({ url: string; token: string; }): Promise { - const { error } = await getResend().emails.send({ - from: process.env.EMAIL_FROM ?? "CubeAdmin ", + await createTransport().sendMail({ + from: FROM(), to: email, subject: "Your CubeAdmin sign-in link", html: `

Click the link below to sign in to CubeAdmin. This link expires in 1 hour.

${url}

`, + text: `Sign in to CubeAdmin: ${url}\n\nThis link expires in 1 hour.`, }); - - if (error) throw new Error(`Failed to send magic link email: ${error.message}`); } export async function sendInvitationEmail({ @@ -38,16 +48,12 @@ export async function sendInvitationEmail({ inviteUrl: string; role: string; }): Promise { - const html = await render( - InvitationEmail({ invitedByName, inviteUrl, role }), - ); - - const { error } = await getResend().emails.send({ - from: process.env.EMAIL_FROM ?? "CubeAdmin ", + const html = await render(InvitationEmail({ invitedByName, inviteUrl, role })); + await createTransport().sendMail({ + from: FROM(), to, subject: `You've been invited to CubeAdmin`, html, + text: `${invitedByName} invited you to CubeAdmin as ${role}.\n\nAccept your invitation: ${inviteUrl}\n\nThis link expires in 48 hours.`, }); - - if (error) throw new Error(`Failed to send email: ${error.message}`); } diff --git a/package.json b/package.json index 17a3841..01f1b93 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@monaco-editor/react": "^4.7.0", "@tanstack/react-query": "^5.90.21", "@tanstack/react-table": "^8.21.3", + "@types/nodemailer": "^7.0.11", "@xterm/addon-fit": "^0.11.0", "@xterm/addon-search": "^0.16.0", "@xterm/addon-web-links": "^0.12.0", @@ -34,12 +35,12 @@ "next-safe-action": "^8.1.8", "next-themes": "^0.4.6", "node-cron": "^4.2.1", + "nodemailer": "^8.0.1", "rcon-client": "^4.2.5", "react": "19.2.3", "react-dom": "19.2.3", "react-dropzone": "^15.0.0", "recharts": "^3.8.0", - "resend": "^6.9.3", "semver": "^7.7.4", "shadcn": "^4.0.2", "socket.io": "^4.8.3",