SMTP-2-Telegram is a lightweight C++ service that listens for incoming emails via small SMTP server and forwards their contents to a specified Telegram chat using a bot. This is useful for receiving notifications, alerts, or any email-based messages directly in Telegram. This is a project of mine as I work for a company with an old ems system that can't communicate with newer smtp servers and we lost the ability to get emails. This is a great little work around and figured I'd share.
- Receives emails via SMTP
- Forwards email subject and body to Telegram chat
- Modern object-oriented C++17 architecture
- Thread-safe logging with automatic rotation
- Retry logic for reliable message delivery
- Proper MIME and quoted-printable email decoding
- Graceful shutdown (Ctrl+C handling)
- Configurable via environment variables
- Installs as a systemd service via .deb package
- Automatically creates
.envfile if not present - Stores all files (binary, config, logs) in
~/smtp2telegram/ - Secure file permissions (0600 for .env)
- Lightweight and easy to deploy
- ✅ Complete refactoring to object-oriented architecture
- ✅ Thread-safe logging with automatic rotation
- ✅ Retry logic for reliable Telegram delivery
- ✅ Proper MIME/quoted-printable/Base64 decoding
- ✅ HTML email support (strips tags)
- ✅ Multipart email parsing
- ✅ Graceful shutdown on Ctrl+C
- ✅ Secure .env file permissions (0600)
- ✅ Input validation (ports, chat IDs)
- ✅ Connection testing on startup
- ✅ All bugs from v1.x fixed
- C++17 compatible compiler (g++ 7.0+)
- Make (for building, if not using .deb)
- Boost.Asio library
- libcurl with SSL support
- Telegram Bot Token
- Telegram Chat ID
-
Download and install the .deb package:
sudo dpkg -i smtp2telegram_VERSION_ARCH.deb
-
The systemd service will be enabled automatically.
-
See configuration below.
-
Required dependencies:
sudo apt install g++ libboost-all-dev libcurl4-openssl-dev
-
Clone the repository:
git clone https://github.com/flintman/smtp2telegram.git cd smtp2telegram -
Build the project deb:
make deb
The project uses a modern object-oriented design with clear separation of concerns:
- Config - Configuration loading and validation
- Logger - Thread-safe logging with rotation
- TelegramClient - Telegram API client with retry logic
- EmailParser - MIME parsing and email decoding
- SMTPServer - SMTP protocol handling
On first run, the service will create a .env file in ~/smtp2telegram/ if it does not exist by asking
you the questions for each variable.
Set the following environment variables in ~/smtp2telegram/.env manually if you like:
| Variable | Description |
|---|---|
API_KEY |
Token for your Telegram bot |
CHAT_ID |
Chat ID to send messages to |
SMTP_HOSTNAME |
Host/IP to listen for SMTP (default: 0.0.0.0) |
SMTP_PORT |
Port to listen for SMTP (default: 1025) |
LOG_KEEP_DAYS |
Days to keep logs (default: 3) |
Example ~/smtp2telegram/.env file:
API_KEY=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
CHAT_ID=-1001234567890
SMTP_HOSTNAME=0.0.0.0
SMTP_PORT=1025
LOG_KEEP_DAYS=3First Run (*** TO CREATE THE .env FILE follow directions***):
smtp2telegramThe application will:
- Test the Telegram connection on startup
- Log all activities to
~/smtp2telegram/smtp_server.log - Automatically rotate old logs based on
LOG_KEEP_DAYS - Retry failed Telegram sends up to 3 times
- Handle Ctrl+C gracefully for clean shutdown
Start the service (if not already running and have .env file):
sudo systemctl start smtp2telegramStop the service gracefully:
sudo systemctl stop smtp2telegram
# or press Ctrl+C if running in foregroundSend an email to the configured SMTP server. The service will forward the email's subject and body to the specified Telegram chat.
Logs are saved to ~/smtp2telegram/smtp_server.log.
# Ensure you have C++17 support
g++ --version # Need 7.0 or higher
# Install dependencies
sudo apt install g++ libboost-all-dev libcurl4-openssl-dev make# Check logs
tail -f ~/smtp2telegram/smtp_server.log
# Test Telegram connection
curl "https://api.telegram.org/bot<YOUR_API_KEY>/getMe"
# Verify .env file
cat ~/smtp2telegram/.env- Check if port is already in use:
netstat -tuln | grep <YOUR_PORT> - Verify .env file exists and has correct permissions
- Check systemd logs:
journalctl -u smtp2telegram -f
MIT License
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
For questions or support, open an issue on GitHub.