Go to this https://github.com/kunalGhoshOne/imap-smtp/tree/master
330
https://github.com/kunalGhoshOne/imap-smtp/tree/masterโ
A comprehensive Node.js email server that supports SMTP (sending), IMAP (retrieval), and LMTP (local transfer) protocols with modular architecture, mailbox management, and Docker deployment.
The application is organized into modular components with clear separation of concerns:
smtp-nodejs/
โโโ config/ # Configuration management
โ โโโ config.js # Centralized configuration
โ โโโ database.js # Database connection management
โโโ models/ # Database models
โ โโโ Email.js # Email schema and model
โ โโโ Mailbox.js # Mailbox schema and model
โโโ services/ # Business logic services
โ โโโ MultiPortSMTPServer.js # Multi-port SMTP server
โ โโโ IMAPServer.js # IMAP server with modular commands
โ โโโ LMTPServer.js # LMTP server
โ โโโ EmailProcessor.js # Email processing logic
โ โโโ MailSender.js # External email delivery
โ โโโ EmailQueue.js # Queue management
โ โโโ QueueAPI.js # Web dashboard and API
โ โโโ MailboxAPI.js # Mailbox management API
โ โโโ IPSelectionService.js # Dynamic IP selection
โโโ services/imap/commands/ # Modular IMAP command handlers
โ โโโ CapabilityCommand.js
โ โโโ LoginCommand.js
โ โโโ SelectCommand.js
โ โโโ FetchCommand.js
โ โโโ SearchCommand.js
โ โโโ SortCommand.js
โ โโโ UIDCommand.js
โโโ utils/ # Utility modules
โ โโโ logger.js # Centralized logging
โโโ server.js # Main application entry point
โโโ Dockerfile # Docker container definition
โโโ docker-compose.yml # Multi-service deployment
โโโ package.json # Dependencies and scripts
Clone the repository
Install dependencies:
npm install
Copy the environment example and configure:
cp env.example .env
Update the .env file with your configuration
docker-compose up --build
Note: For multi-IP support, the Docker container uses network_mode: host which requires Linux. For other platforms, modify docker-compose.yml to use bridge networking.
Create a .env file with the following options:
# Mailbox API Configuration
MAILBOX_API_PORT=8080
MAILBOX_API_KEY=changeme
# Server Configuration
SMTP_PORT=2525
SMTP_HOST=0.0.0.0
API_PORT=3000
# Multi-Port SMTP Configuration
SMTP_25_PORT=25
SMTP_465_PORT=465
SMTP_587_PORT=587
# LMTP Configuration
LMTP_24_PORT=24
LMTP_PORT=24
LMTP_SSL_PORT=1024
LMTP_SSL_ENABLED=false
LMTP_SSL_KEY=/path/to/lmtp-key.pem
LMTP_SSL_CERT=/path/to/lmtp-cert.pem
LMTP_SSL_CA=/path/to/lmtp-ca.pem
# IMAP Configuration
IMAP_143_PORT=143
IMAP_993_PORT=993
IMAP_PORT=143
IMAP_SSL_PORT=993
IMAP_SSL_ENABLED=false
IMAP_SSL_KEY=/path/to/imap-key.pem
IMAP_SSL_CERT=/path/to/imap-cert.pem
IMAP_SSL_CA=/path/to/imap-ca.pem
# Database Configuration
MONGODB_URL=mongodb://localhost:27017/smtp-server
# Email Configuration
MAX_EMAIL_SIZE=10485760
ALLOWED_DOMAINS=example.com,test.com
# Webhook Configuration
WEBHOOK_ENABLED=false
WEBHOOK_SUCCESS_URL=https://your-webhook-url.com/success
WEBHOOK_FAILURE_URL=https://your-webhook-url.com/failure
WEBHOOK_TIMEOUT=10000
WEBHOOK_RETRIES=3
# IP Selection Configuration
IP_SELECTION_ENABLED=false
IP_SELECTION_API_URL=https://your-ip-api.com/get-ip
IP_SELECTION_TIMEOUT=5000
IP_SELECTION_RETRIES=3
FALLBACK_IP=1.2.3.4
# Logging Configuration
LOG_LEVEL=info
ENABLE_CONSOLE_LOG=true
npm start
Or directly:
node server.js
docker-compose up --build
The Mailbox API runs on port 8080 by default and provides endpoints for managing email accounts:
All endpoints require the API key in the x-api-key header or api_key query parameter.
Create Mailbox
POST /api/mailboxes
Content-Type: application/json
x-api-key: your-api-key
{
"username": "[email protected]",
"password": "securepassword"
}
List Mailboxes
GET /api/mailboxes
x-api-key: your-api-key
Delete Mailbox
DELETE /api/mailboxes/[email protected]
x-api-key: your-api-key
Change Password
POST /api/mailboxes/[email protected]/change-password
Content-Type: application/json
x-api-key: your-api-key
{
"oldPassword": "oldpassword",
"newPassword": "newpassword"
}
HELO/EHLO - Greeting and identificationMAIL FROM: - Specify senderRCPT TO: - Specify recipientsDATA - Begin email data transmissionQUIT - End connectionRSET - Reset current transactionCAPABILITY - List server capabilitiesLOGIN - Authenticate userSELECT - Select mailboxLIST - List mailboxesFETCH - Retrieve message dataSEARCH - Search for messagesSORT - Sort messages by criteriaUID - UID-based operationsNOOP - Keep connection aliveLOGOUT - Close connectionLHLO - Greeting and identificationMAIL - Specify senderRCPT - Specify recipientsDATA - Begin email data transmissionQUIT - End connectionRSET - Reset current transactionNOOP - Keep connection alivenpm test # Test email sending
npm run test:ip # Test IP selection
npm run test:ports # Test multi-port SMTP
npm run test:lmtp # Test LMTP server
telnet localhost 2525
telnet localhost 143
telnet localhost 24
node test-separate-tables.js
Access the real-time queue dashboard at: http://localhost:3000โ
GET /api/queue/stats - Get queue statisticsGET /api/emails - List emails with paginationGET /api/emails/:id - Get specific email detailsPOST /api/emails/:id/retry - Retry failed emailDELETE /api/emails/:id - Delete emailGET /api/successful-emails - List successfully delivered emailsGET /api/successful-emails/:id - Get specific successful email detailsGET /api/bounced-emails - List bounced emailsGET /api/bounced-emails/:id - Get specific bounced email detailsGET /api/incoming-emails - List incoming emailsGET /api/incoming-emails/:id - Get specific incoming email detailsDELETE /api/incoming-emails/:id - Delete incoming emailGET /api/email-stats - Get comprehensive email statisticsGET /api/ip-selection/stats - Get IP selection cache statisticsPOST /api/ip-selection/clear-cache - Clear IP selection cachePOST /api/ip-selection/test - Test IP selection for specific emailGET /api/smtp/stats - Get multi-port SMTP server statisticsGET /api/imap/stats - Get IMAP server statisticsGET /api/lmtp/stats - Get LMTP server statisticsGET /health - Health checkFor applications requiring multiple source IPs (e.g., email sending from different IPs), the Docker container uses network_mode: host. This allows the container to bind to all host network interfaces and send traffic from any available source IP.
# Build and start all services
docker-compose up --build
# Run in background
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
Modify the docker-compose.yml file to change default ports or use bridge networking instead of host networking.
config/)config.jsCentralized configuration management that loads environment variables and provides defaults.
database.jsDatabase connection management with connection pooling and error handling.
models/)Email.jsMongoDB schema for outgoing email storage including:
SuccessfulEmail.jsMongoDB schema for successfully delivered emails including:
BouncedEmail.jsMongoDB schema for bounced/failed emails including:
IncomingEmail.jsMongoDB schema for incoming emails including:
Mailbox.jsMongoDB schema for mailbox management including:
services/)MultiPortSMTPServer.jsMulti-port SMTP server:
IMAPServer.jsIMAP server for email retrieval:
LMTPServer.jsLMTP server for local mail transfer:
EmailProcessor.jsBusiness logic for email processing:
MailSender.jsHandles external email delivery:
EmailQueue.jsQueue management system:
QueueAPI.jsWeb interface and API:
MailboxAPI.jsMailbox management API:
IPSelectionService.jsDynamic IP selection:
services/imap/commands/)CapabilityCommand.jsHandles IMAP CAPABILITY command:
LoginCommand.jsHandles IMAP LOGIN command:
SelectCommand.jsHandles IMAP SELECT command:
FetchCommand.jsHandles IMAP FETCH command:
SearchCommand.jsHandles IMAP SEARCH command:
SortCommand.jsHandles IMAP SORT command:
UIDCommand.jsHandles IMAP UID commands:
utils/)logger.jsCentralized logging system with:
Emails are stored in MongoDB with the following structure:
{
sender: String,
recipients: [String],
subject: String,
text: String,
html: String,
attachments: [{
filename: String,
contentType: String,
content: Buffer
}],
raw: String,
createdAt: Date
}
The application includes comprehensive error handling:
The server handles graceful shutdown on:
SIGTERM (Docker/Kubernetes)SIGINT (Ctrl+C)ISC License
Content type
Image
Digest
sha256:66d69e4d5โฆ
Size
61.2 MB
Last updated
about 1 year ago
docker pull kunalghoshone/imap-smtp