A modern, type-safe email service built with Next.js, React Email, and Nodemailer. This FOSS (Free and Open Source Software) project provides a robust API for sending transactional emails with beautiful, customizable templates.
- π¨ Beautiful Email Templates: Pre-built, responsive email templates using React Email components
- π Type-Safe: Full TypeScript support with Zod schema validation
- π Template Registry: Centralized template management system
- β‘ Fast & Modern: Built on Next.js 15 with React 19
- π Secure: Built-in security with server identity verification
- π― Flexible: Easy to customize and extend with new templates
- π¦ Production Ready: Includes error handling, validation, and logging
Before you begin, ensure you have the following installed:
- Node.js: Version 18.x or higher
- npm, yarn, or pnpm: Package manager (pnpm recommended)
- SMTP Server: Access to an SMTP server (e.g., Gmail, SendGrid, Sendinblue)
git clone https://github.com/kanakkholwal/mail-system.git
cd mail-systemnpm install
# or
yarn install
# or
pnpm installCopy the example environment file and configure your settings:
cp .env.example .envEdit .env with your SMTP credentials:
# SMTP Configuration
MAIL_EMAIL=your-email@example.com
MAIL_PASSWORD=your-smtp-password
SMTP_HOST=smtp-relay.sendinblue.com
# Security
SERVER_IDENTITY=your-secure-random-key
# Optional
NODE_ENV=developmentnpm run devThe server will start at http://localhost:3000
| Variable | Required | Default | Description |
|---|---|---|---|
MAIL_EMAIL |
β | noreply@nexonauts.com |
SMTP email address |
MAIL_PASSWORD |
β | - | SMTP password/app password |
SMTP_HOST |
β | smtp-relay.sendinblue.com |
SMTP server host |
SERVER_IDENTITY |
β | - | Security key for API authentication |
NODE_ENV |
β | development |
Environment mode |
The system supports any SMTP provider. Common configurations:
Gmail:
SMTP_HOST=smtp.gmail.com
MAIL_EMAIL=your-email@gmail.com
MAIL_PASSWORD=your-app-passwordSendGrid:
SMTP_HOST=smtp.sendgrid.net
MAIL_EMAIL=apikey
MAIL_PASSWORD=your-sendgrid-api-keySendinblue (Brevo):
SMTP_HOST=smtp-relay.sendinblue.com
MAIL_EMAIL=your-email@example.com
MAIL_PASSWORD=your-smtp-keyEndpoint: POST /api/send
Request Body:
{
"template_key": "welcome_verify",
"targets": ["user@example.com"],
"subject": "Welcome to Our Platform!",
"payload": {
"name": "John Doe",
"email": "user@example.com",
"verificationUrl": "https://example.com/verify?token=abc123"
}
}Response (Success):
{
"data": ["user@example.com"]
}Response (Error):
{
"error": "Template Validation Failed",
"message": "Invalid payload for template"
}curl -X POST http://localhost:3000/api/send \
-H "Content-Type: application/json" \
-d '{
"template_key": "welcome_verify",
"targets": ["user@example.com"],
"subject": "Welcome!",
"payload": {
"name": "John Doe",
"email": "user@example.com",
"verificationUrl": "https://example.com/verify?token=abc123"
}
}'const response = await fetch('http://localhost:3000/api/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
template_key: 'welcome_verify',
targets: ['user@example.com'],
subject: 'Welcome to Our Platform!',
payload: {
name: 'John Doe',
email: 'user@example.com',
verificationUrl: 'https://example.com/verify?token=abc123'
}
})
});
const result = await response.json();
console.log(result);Purpose: Welcome new users and verify their email address.
Required Payload:
{
name: string; // User's name
email: string; // User's email address
verificationUrl: string; // URL for email verification
branding?: { // Optional branding override
name?: string;
logoUrl?: string;
websiteUrl?: string;
primaryColor?: string;
}
}Purpose: Send password reset instructions to users.
Required Payload:
{
name: string; // User's name
email: string; // User's email address
resetUrl: string; // URL for password reset
branding?: { // Optional branding override
name?: string;
logoUrl?: string;
websiteUrl?: string;
primaryColor?: string;
}
}Create a new directory in emails/templates/:
mkdir emails/templates/my-templateCreate index.tsx:
import { Button } from "@emails/kit/components/button";
import { Layout } from "@emails/kit/components/layout";
import { H1, BodyText } from "@emails/kit/components/typography";
import { Section } from "@react-email/components";
import { MyTemplateProps } from "./schema";
export const MyTemplate = ({ name, actionUrl }: MyTemplateProps) => {
return (
<Layout previewText="Your custom email">
<Section className="my-6">
<H1>Hello {name}!</H1>
<BodyText>This is your custom email template.</BodyText>
<Button href={actionUrl}>Take Action</Button>
</Section>
</Layout>
);
};
export default MyTemplate;Create schema.ts:
import { z } from "zod";
export const myTemplateSchema = z.object({
name: z.string(),
actionUrl: z.string().url(),
});
export type MyTemplateProps = z.infer<typeof myTemplateSchema>;Update emails/registry.ts:
import MyTemplate from "./templates/my-template";
import { myTemplateSchema } from "./templates/my-template/schema";
export const registry = {
// ... existing templates
"my_template": {
component: MyTemplate,
schema: myTemplateSchema,
},
} as const;curl -X POST http://localhost:3000/api/send \
-H "Content-Type: application/json" \
-d '{
"template_key": "my_template",
"targets": ["user@example.com"],
"subject": "Custom Email",
"payload": {
"name": "John",
"actionUrl": "https://example.com/action"
}
}'mail-system/
βββ app/
β βββ api/
β β βββ send/
β β βββ route.ts # Email sending API endpoint
β βββ layout.tsx # Root layout
βββ emails/
β βββ kit/ # Reusable email components
β β βββ components/ # Button, Header, Footer, etc.
β β βββ tailwind.ts # Tailwind configuration
β β βββ tokens.ts # Design tokens
β β βββ types.ts # TypeScript types
β βββ templates/ # Email templates
β β βββ welcome-verify/
β β βββ reset-password/
β βββ helper.ts # Email sending helper functions
β βββ registry.ts # Template registry
β βββ renderer.tsx # Template renderer
β βββ schema.ts # Zod schemas
βββ .env.example # Example environment variables
βββ next.config.ts # Next.js configuration
βββ package.json # Dependencies and scripts
βββ project.config.ts # Project configuration
βββ tsconfig.json # TypeScript configuration
βββ README.md # This file
# Start development server
npm run dev
# Build for production
npm run build
# Start production server
npm run start
# Lint code
npm run lint
# Format code
npm run formatThis project uses:
- TypeScript for type safety
- Zod for runtime validation
- Prettier for code formatting
- ESLint for code linting
- Create a new branch:
git checkout -b feature/your-feature - Make your changes
- Run linting:
npm run lint - Format code:
npm run format - Test your changes thoroughly
- Commit with clear messages
- Push and create a Pull Request
Contributions are welcome! Here's how you can help:
- Check if the bug has already been reported
- Create a new issue with a clear title and description
- Include steps to reproduce
- Add relevant logs or screenshots
- Open an issue describing the enhancement
- Explain why this enhancement would be useful
- Provide examples if possible
- Fork the repository
- Create your feature branch
- Make your changes following our coding standards
- Add or update tests if needed
- Ensure all tests pass
- Update documentation as needed
- Submit a pull request
- Be respectful and inclusive
- Provide constructive feedback
- Focus on what's best for the community
- Show empathy towards other contributors
Issue: "SMTP Authentication Failed"
- Verify your SMTP credentials are correct
- For Gmail, ensure you're using an App Password, not your regular password
- Check if 2FA is enabled on your email account
Issue: "Template not found in registry"
- Verify the template_key matches exactly with registry keys
- Check the spelling and case sensitivity
- Ensure the template is properly registered in
emails/registry.ts
Issue: "Invalid payload for template"
- Check that all required fields are provided
- Verify field types match the schema
- Review the Zod validation error messages in the response
Issue: "Port 3000 already in use"
- Kill the process using port 3000:
lsof -ti:3000 | xargs kill -9 - Or use a different port:
PORT=3001 npm run dev
- π Check the documentation
- π¬ Open an issue on GitHub
- π§ Contact the maintainers
This project is licensed under the MIT License - see below for details:
MIT License
Copyright (c) 2024 Nexonauts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- Built with Next.js
- Email templates powered by React Email
- Email delivery via Nodemailer
- Type safety with TypeScript and Zod
If you find this project helpful, please consider:
- β Starring the repository
- π Reporting bugs
- π‘ Suggesting new features
- π€ Contributing to the codebase
- π’ Sharing with others
Made with β€οΈ by the Nexonauts team