Skip to main content

A drop-in sendmail replacement that forwards system emails to Telegram.

Project description

telegram-sendmail

A drop-in sendmail replacement that forwards system emails to Telegram.

telegram-sendmail intercepts emails that system daemons — cron, logwatch, fail2ban, and others — send through the standard sendmail interface and forwards them to a Telegram chat as formatted messages. Any application that calls /usr/sbin/sendmail works without modification.

Two delivery modes:

  • Pipe mode (default) — reads a raw RFC 2822 email from stdin, the standard interface used by cron and the majority of Unix system daemons.
  • SMTP server mode (-bs) — runs a minimal SMTP state machine on stdin/stdout for applications that speak SMTP directly to sendmail.

Features

  • Parses plain-text and HTML MIME emails; sanitises HTML through a two-pass pipeline before conversion to Telegram-compatible markup
  • Detects MIME attachments without forwarding binary content
  • Archives every raw email to a local spool file before Telegram delivery; no message is lost if the network is unavailable
  • Smart message truncation at word boundaries with a configurable length limit
  • Structured logging to syslog (LOG_MAIL facility) with an optional --console mode for interactive debugging
  • Full sendmail-compatible flag support: -f, -r, -s, -bs, -t, -i, -oi, plus silent acceptance of positional recipient arguments
  • Configurable HTTP retry strategy with exponential backoff
  • Suppresses known-noisy messages via case-insensitive glob patterns on Subject and From headers
  • EX_TEMPFAIL (exit code 75) on HTTP 429/5xx so MTA-aware daemons re-queue and retry automatically

Requirements

Installation

Pre-built binaries for x86_64 and aarch64 with no runtime dependencies are attached to every GitHub Release. Installing from PyPI requires Python 3.10 or later:

# Recommended: install with pipx (automatic isolation)
pipx install telegram-sendmail

# Alternative: install into the system Python
pip install telegram-sendmail

Configuration

Create /etc/telegram-sendmail.ini (system-wide) or ~/.telegram-sendmail.ini (per-user):

[telegram]
token   = 123456789:AAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
chat_id = -1001234567890

[options]
; All keys are optional — out-of-range values fall back to defaults.
;
; spool_dir             = /var/mail
; message_max_length    = 3800      (100–4096)
; smtp_timeout          = 30        (5–300)
; telegram_timeout      = 10        (5–60)
; max_retries           = 3         (0–10)
; backoff_factor        = 0.5       (0.0–10.0)
; disable_notification  = false

[filters]
; suppress_subject =                (case-insensitive globs)
; suppress_sender  =                (case-insensitive globs)

The config file contains the bot token. Permissions must be 0600; a WARNING is emitted to syslog on group- or world-readable files.

sudo chmod 600 /etc/telegram-sendmail.ini

A fully-annotated template covering every available key is available at telegram-sendmail.ini.example.

Registering as the system sendmail

telegram-sendmail does not register itself as /usr/sbin/sendmail automatically to avoid silently overwriting an existing MTA.

Simple symlink (no existing MTA):

TGSM_BIN=$(which telegram-sendmail)
sudo ln -sf "$TGSM_BIN" /usr/sbin/sendmail
sudo ln -sf "$TGSM_BIN" /usr/bin/sendmail

update-alternatives (Debian/Ubuntu, existing MTA present):

TGSM_BIN=$(which telegram-sendmail)
sudo update-alternatives --install /usr/sbin/sendmail sendmail "$TGSM_BIN" 50
sudo update-alternatives --config sendmail

Exit Codes

Code Name Meaning
0 EX_OK Message delivered successfully to Telegram.
1 EX_ERROR Operational failure: parse error, API error, or unexpected exception.
75 EX_TEMPFAIL Transient failure (HTTP 429/5xx). Daemons will re-queue and retry.
78 EX_CONFIG Configuration error: file missing, unreadable, or missing required key.

For comprehensive setup instructions, configuration file templates, usage examples, and troubleshooting guides, refer to the full README on the telegram-sendmail GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_sendmail-1.2.0.tar.gz (135.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_sendmail-1.2.0-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file telegram_sendmail-1.2.0.tar.gz.

File metadata

  • Download URL: telegram_sendmail-1.2.0.tar.gz
  • Upload date:
  • Size: 135.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for telegram_sendmail-1.2.0.tar.gz
Algorithm Hash digest
SHA256 880dd51a3250117d919c3d5f8db7244af9b3e81573d9ed05116d538702e16aa7
MD5 1d4ed415fd00af9ac75ca6b4659ad109
BLAKE2b-256 2f2a9ca58650b3d6ad63d4684181d481022c362ceb45763525754055b5cccf5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for telegram_sendmail-1.2.0.tar.gz:

Publisher: pypi.yml on theodiv/telegram-sendmail

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file telegram_sendmail-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_sendmail-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b13e476b4550cdada3880c0eb7304f93cc59a0d1a914ad653f29b3b0174215e3
MD5 edc3fe2dd5f08715fdb0dcde5db91d5b
BLAKE2b-256 13daaa6d9984a9efa458cc362be9018b2869b53b4ee279f57997acb4bb800a8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for telegram_sendmail-1.2.0-py3-none-any.whl:

Publisher: pypi.yml on theodiv/telegram-sendmail

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page