home-lang/mail
A performant & secure SMTP server implementation, designed for self-hosting email infrastructure.
A performant and secure SMTP server implementation written in Zig, designed for self-hosting email infrastructure.
Multi-Tenancy Support: Complete tenant isolation and resource management
Cluster Mode for High Availability: Distributed coordination and failover
--port, --host - Server binding configuration--log-level - Adjust logging verbosity (debug|info|warn|error|critical)--max-connections - Connection limit override--enable-tls/--disable-tls - TLS toggle--enable-auth/--disable-auth - Authentication toggleSMTP_HOST, SMTP_PORT, SMTP_HOSTNAMESMTP_MAX_CONNECTIONS, SMTP_MAX_RECIPIENTSSMTP_MAX_MESSAGE_SIZESMTP_ENABLE_TLS, SMTP_ENABLE_AUTHSMTP_TLS_CERT, SMTP_TLS_KEYzig build
# Run with defaults (0.0.0.0:2525)
zig build run
# Or run the compiled binary
./zig-out/bin/smtp-server
# Show help
./zig-out/bin/smtp-server --help
# Show version
./zig-out/bin/smtp-server --version
# Run on custom port with debug logging
./zig-out/bin/smtp-server --port 587 --log-level debug
# Run with custom configuration
./zig-out/bin/smtp-server --host 127.0.0.1 --port 2525 --max-connections 200
# Run with IPv6
./zig-out/bin/smtp-server --host "::1" --port 2525
# Bind to all IPv6 addresses
./zig-out/bin/smtp-server --host "::" --port 2525
# Using environment variables
export SMTP_PORT=2525
export SMTP_MAX_CONNECTIONS=500
export SMTP_HOSTNAME="mail.example.com"
./zig-out/bin/smtp-server
# IPv6 via environment
export SMTP_HOST="::"
./zig-out/bin/smtp-server
# Enable webhook notifications
export SMTP_WEBHOOK_URL="http://localhost:8080/webhook"
./zig-out/bin/smtp-server
The server starts on 0.0.0.0:2525 by default (non-privileged port for development).
IPv6 Support: The server fully supports IPv6. Use ::1 for localhost or :: to bind to all IPv6 addresses.
Webhook Notifications: Set SMTP_WEBHOOK_URL to receive HTTP POST notifications with JSON payload containing sender, recipients, size, and timestamp when mail is received.
See EXAMPLES.md for more usage examples including Docker, systemd, and production deployments.
Configuration is managed in src/config.zig. Key settings include:
# Run Zig unit tests
zig build test
The project includes comprehensive unit tests for:
# Run the automated SMTP integration tests
./test-smtp.sh
# Test against custom host/port
SMTP_HOST=localhost SMTP_PORT=2525 ./test-smtp.sh
The integration test suite includes 20 comprehensive tests:
# Using telnet
telnet localhost 2525
# Example session:
EHLO client.example.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Test Message
From: [email protected]
To: [email protected]
This is a test message.
.
QUIT
# Using swaks (Swiss Army Knife for SMTP)
swaks --to [email protected] \
--from [email protected] \
--server localhost:2525 \
--body "Test message"
# Test with authentication
swaks --to [email protected] \
--from [email protected] \
--server localhost:2525 \
--auth PLAIN \
--auth-user test \
--auth-password test
# Test rate limiting (send multiple messages)
for i in {1..105}; do
swaks --to [email protected] \
--from [email protected] \
--server localhost:2525 \
--body "Message $i" \
--hide-all
done
See EXAMPLES.md for more testing examples and integration guides.
.
├── build.zig # Build configuration
├── test-smtp.sh # Automated test suite
├── EXAMPLES.md # Comprehensive usage examples
├── TLS.md # TLS/STARTTLS setup guide
├── TODO.md # Development roadmap
├── src/
│ ├── main.zig # Entry point with CLI and signal handling
│ ├── smtp.zig # SMTP server with connection management
│ ├── protocol.zig # SMTP protocol handler (RFC 5321)
│ ├── config.zig # Configuration with env var support
│ ├── args.zig # Command-line argument parser
│ ├── auth.zig # Authentication mechanisms
│ ├── security.zig # Rate limiting, validation, security
│ ├── logger.zig # Multi-level structured logging
│ ├── errors.zig # Custom error types and handling
│ ├── webhook.zig # Webhook notifications
│ └── tls.zig # TLS certificate management
└── mail/
└── new/ # Incoming messages (maildir format)
TLS/SSL: Deploy behind a reverse proxy (nginx, HAProxy) for TLS termination:
# See TLS.md for complete setup guide
# Example with nginx on port 465 (SMTPS)
# Server runs on port 2525, nginx handles TLS
The server includes TLS configuration support but requires a reverse proxy for the cryptographic handshake. See TLS.md for detailed setup instructions including:
Authentication: The current implementation accepts all credentials. Implement proper credential verification in src/auth.zig:
pub fn verifyCredentials(username: []const u8, password: []const u8) bool {
// Add your authentication logic here
// Check against database, LDAP, etc.
}
Rate Limiting: Adjust rate limits based on your needs in config.zig
Firewall: Use firewall rules to restrict access:
# Example using ufw
sudo ufw allow from trusted.ip.address to any port 25
Run as Non-Root: After binding to port 25, drop privileges:
# Use a process supervisor like systemd with User= directive
Logging: Monitor logs for suspicious activity
SPF/DKIM/DMARC: Implement email authentication when sending:
To run on the standard SMTP port (25), you'll need elevated privileges:
# Option 1: Run as root (not recommended)
sudo zig build run
# Option 2: Grant capability (Linux)
sudo setcap 'cap_net_bind_service=+ep' zig-out/bin/smtp-server
./zig-out/bin/smtp-server
# Option 3: Use iptables redirect
sudo iptables -t nat -A PREROUTING -p tcp --dport 25 -j REDIRECT --to-port 2525
# Run Zig unit tests
zig build test
# Run integration tests (requires server running on port 2525)
./test-smtp.sh
This project follows Zig's standard formatting:
zig fmt src/
This project uses zig-bump for version management and automated releases.
The easiest way - an interactive script that guides you through the release:
./scripts/release.sh
# or
make release
This provides a beautiful interactive menu with:
If you prefer direct commands:
# Native Zig build (cross-platform)
zig build bump-patch # Bug fixes (0.0.1 -> 0.0.2)
zig build bump-minor # New features (0.0.1 -> 0.1.0)
zig build bump-major # Breaking changes (0.0.1 -> 1.0.0)
zig build bump # Interactive selection
zig build bump-patch-dry # Preview changes
# Or using Makefile shortcuts
make release-patch # Same as zig build bump-patch
make release-minor # Same as zig build bump-minor
make release-major # Same as zig build bump-major
When you bump the version, it will:
build.zig.zonSee docs/RELEASE_PROCESS.md for detailed release documentation.
Full TLS/STARTTLS implementation
Database-backed authentication
DKIM signing support
SPF validation
Greylisting
Spam filtering integration
Webhook notifications for incoming mail
REST API for message retrieval
Web-based admin interface
IPv6 support
SMTP relay configuration
Bounce handling
Built with Zig's performance-first philosophy:
Typical performance on modern hardware:
Contributions are welcome! Please ensure:
zig fmt)zig build test)MIT License - See LICENSE file for details
For issues, questions, or contributions, please open an issue on the repository.
Version: v0.26.0 - Enterprise Ready 🚀
All core features plus enterprise capabilities are complete! See PROJECT_STATUS.md for:
This SMTP server is production-ready with comprehensive features, security, and documentation. For specific deployment scenarios, refer to the deployment guides in the docs/ directory.