README
¶
SMTP to Telegram
smtp_to_telegram is a small program which listens for SMTP and sends
all incoming Email messages to Telegram. It also supports bidirectional
communication: you can reply to a forwarded Telegram message to send an
email back to the original sender.
Say you have a software which can send Email notifications via SMTP.
You may use smtp_to_telegram as an SMTP server so
the notification mail would be sent to the chosen Telegram chats.
Getting started
- Create a new Telegram bot: https://core.telegram.org/bots#creating-a-new-bot.
- Open that bot account in the Telegram account which should receive
the messages, press
/start. - Retrieve a chat id with
curl https://api.telegram.org/bot<BOT_TOKEN>/getUpdates. If you don't see chat id, try writing one more message to the bot. - Repeat steps 2 and 3 for each Telegram account which should receive the messages.
- Create
env_filefromenv_file.exampleand fill it with your data. - Start a docker container:
docker compose up
You may use localhost:25 as the target SMTP address.
No TLS or authentication is required.
The Telegram message format is:
From: {from}
To: {to}
CC: {cc}
Reply-To: {reply_to}
Subject: {subject}
Message-ID: {message_id}
{body}
{attachments_details}
{from} is the address from the From: header, or the SMTP envelope sender
if the header is missing. The CC, Reply-To and Message-ID lines are only
shown when present. Custom message templates are no longer supported (breaking
change in v2).
Reply to Email
When an email is forwarded to Telegram, the bot uses Telegram's ForceReply feature to prompt you to reply. If you reply to that message in Telegram, the bot will send your reply as an email back to the original sender.
How it works
- An incoming email is forwarded to your Telegram chat.
- The bot sends the message with ForceReply enabled, prompting you to reply.
- You write a reply in Telegram.
- The bot sends your reply as an email to the original sender's address.
If the forwarded message has a
Message-IDline, the reply setsIn-Reply-To/Referencesso mail clients show it in the same thread.
Configuration
To enable the reply feature, configure outbound SMTP via environment variables:
| Variable | Description | Default |
|---|---|---|
ST_SMTP_OUT_HOST |
Outbound SMTP server hostname (required to enable replies) | — |
ST_SMTP_OUT_PORT |
Outbound SMTP server port | 587 |
ST_SMTP_OUT_USERNAME |
SMTP authentication username | — |
ST_SMTP_OUT_PASSWORD |
SMTP authentication password | — |
Or via the YAML config file (see Configuration File):
smtp_out:
host: smtp.example.com
port: 587
username: user@example.com
password: secret
Limitations
- If the original email had multiple
To:addresses, the first address is used as the sender address for the reply. - Replies are more likely to land in spam if the sending domain isn't authenticated. Set up DKIM, SPF (via a custom MAIL FROM domain) and DMARC for it with your SMTP provider.
Development
Install pre-commit hooks to run formatting, linting, and tests before each commit:
pre-commit install
Configuration File
You can define filter rules and outbound SMTP settings in a YAML configuration file. Rules match against email fields (from, to, subject, body, html) using regex patterns and reject emails that match.
Setup
Configure the config file path using the environment variable:
ST_CONFIG_FILE=/path/to/config.yaml
Or use the command line flag:
--config-file /path/to/config.yaml
Example Configuration
smtp_out:
host: smtp.example.com
port: 587
username: user@example.com
password: secret
filter_rules:
# Simple rule: single condition
- name: block-adnxs-tracking
conditions:
- field: body
pattern: 'adnxs\.com'
# AND logic: ALL conditions must match (default)
- name: block-dating-spam
match: all
conditions:
- field: from
pattern: '@ecinetworks\.com$'
- field: subject
pattern: 'get(ting)? to know'
# OR logic: ANY condition matches
- name: block-spam-domains
match: any
conditions:
- field: body
pattern: 'cdnex\.online'
- field: body
pattern: 'spam-tracker\.net'
- field: body
pattern: 'click-now\.xyz'
# Match URLs in HTML-only emails
- name: block-html-tracking
conditions:
- field: body_or_html
pattern: 'https?://[^\s]+\.(xyz|top|click)'
Rule Structure
| Field | Description |
|---|---|
name |
Rule identifier (used in logs and in the spambox) |
match |
all (default) - all conditions must match; any - at least one condition must match |
conditions |
List of conditions to evaluate |
Available Fields
| Field | Description |
|---|---|
from |
Sender email address; matches if the pattern matches either the From: header address or the SMTP envelope sender |
to |
Recipient email address |
subject |
Email subject line |
body |
Plain text body |
html |
HTML body |
body_or_html |
Matches if pattern found in either body OR html (recommended for URL matching) |
How It Works
- Rules are loaded and regex patterns are compiled at startup (invalid patterns or field names cause startup failure)
- All patterns are case-insensitive
- After an email is parsed, each rule is evaluated in order
- First matching rule rejects the email with a 554 SMTP error
- If no rules match, the email is forwarded to Telegram
Spambox
Rejected emails are lost by default: the sender gets a 554 error and the log has only the rule name, the sender and the subject. To keep a copy for reviewing and tuning the rules, set a spambox file:
ST_SPAMBOX_FILE=/spambox/spambox.jsonl
ST_SPAMBOX_MAX_SIZE=10m
Or use the --spambox-file and --spambox-max-size command line flags.
Each rejected email is appended as one line of JSON (JSON Lines):
| Key | Description |
|---|---|
time |
When the email was rejected (UTC, RFC 3339) |
rule |
Name of the rule that rejected it |
from, to, subject |
The values the filter fields of the same names were matched against |
envelope_from |
SMTP envelope sender, only if it differs from from |
text |
The message as it would have been posted to Telegram: the header lines, then the plain text body (truncated if long). This is what the body field matches |
html |
HTML body, if any, limited to 64 KiB (html_truncated is then true) |
Attachments are not saved. Some ways to read the file with jq:
# The last 5 rejected emails, as text
tail -n 5 spambox.jsonl | jq -r '"=== \(.time) \(.rule)\n\(.text)\n"'
# How many emails each rule rejected
jq -r .rule spambox.jsonl | sort | uniq -c | sort -rn
# Everything rejected by one rule
jq 'select(.rule == "block-spam")' spambox.jsonl
- When the file would grow past
ST_SPAMBOX_MAX_SIZE(default10m), it is renamed to<file>.1, replacing the previous.1. So at most twice that size is kept. - To rotate the file with your own tooling instead (e.g. logrotate), set
ST_SPAMBOX_MAX_SIZE=0. The file is reopened for every email, so it can be renamed or removed at any time; no signal orcopytruncateis needed. - The file must be writable at startup, otherwise the program exits. A failed write later on is only logged; the email is rejected either way.
- With Docker, mount a directory rather than the file itself (rotation renames
the file), and make it writable for the container user (
daemon, uid 2, unless you set another one):
services:
smtp_to_telegram:
environment:
ST_SPAMBOX_FILE: /spambox/spambox.jsonl
volumes:
- ./spambox:/spambox
Changelog
See CHANGELOG.md.
Credits
Forked from KostyaEsmukov/smtp_to_telegram.
Documentation
¶
There is no documentation for this package.