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
  • 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

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.1.0.tar.gz (132.3 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.1.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: telegram_sendmail-1.1.0.tar.gz
  • Upload date:
  • Size: 132.3 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.1.0.tar.gz
Algorithm Hash digest
SHA256 7b1257c2e972701c387aa36f1d60d2aa122740825747591b5ffd8a75b4b8bf9b
MD5 94ee81ca1adaff5768d4e695f61b6381
BLAKE2b-256 1dce97047add291adc19562dfd0feada824a81768779daece0555e5db8c0a864

See more details on using hashes here.

Provenance

The following attestation bundles were made for telegram_sendmail-1.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_sendmail-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5856e6f8248502c66539e81b83f9514245ced981d3b902064e13c475d65ecc0
MD5 cf98af4d126d2971526d906e249f49ff
BLAKE2b-256 6aede5ab3d00d12d4d0f42fc9457f0b89e0ce3674c48b77598813feaa10d3ac1

See more details on using hashes here.

Provenance

The following attestation bundles were made for telegram_sendmail-1.1.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