smtp_to_telegram

command module
v0.0.0-...-26c5241 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 30 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

SMTP to Telegram

Docker Hub License

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

  1. Create a new Telegram bot: https://core.telegram.org/bots#creating-a-new-bot.
  2. Open that bot account in the Telegram account which should receive the messages, press /start.
  3. 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.
  4. Repeat steps 2 and 3 for each Telegram account which should receive the messages.
  5. Create env_file from env_file.example and fill it with your data.
  6. 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
  1. An incoming email is forwarded to your Telegram chat.
  2. The bot sends the message with ForceReply enabled, prompting you to reply.
  3. You write a reply in Telegram.
  4. The bot sends your reply as an email to the original sender's address. If the forwarded message has a Message-ID line, the reply sets In-Reply-To/References so 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
  1. Rules are loaded and regex patterns are compiled at startup (invalid patterns or field names cause startup failure)
  2. All patterns are case-insensitive
  3. After an email is parsed, each rule is evaluated in order
  4. First matching rule rejects the email with a 554 SMTP error
  5. 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 (default 10m), 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 or copytruncate is 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

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址