mail2telegram README

The project's README from github.com/tbxark/mail2telegram. As one markdown file: README.md.

mail2telegram


English | 中文

Receive email in Telegram: instant push notifications plus a Mini App inbox.

Deploy to Cloudflare

mail2telegram is a Telegram bot for receiving email, running entirely on Cloudflare Workers. It combines instant push notifications with a Telegram Mini App: every incoming email is pushed to your chat with quick action buttons, while the full history, attachments and every setting live in the Mini App. Mail arrives through Cloudflare Email Routing, which forwards every message to the worker, and AI summaries run on Workers AI or any OpenAI-compatible provider.

Telegram Mini App: push notification, inbox, message reader and iPad split view

How it works

Email ──▶ Telegram push with quick action buttons
      └─▶ Mini App inbox (history, attachments, settings)

All behavior is configured in the Mini App and stored in D1 — the worker itself only needs a few variables for its Telegram identity and optional API keys.

Guides

Telegram Mini App

Open the Mini App from the bot with /start. The first /start from a chat also binds the webhook and points the bot menu button at the worker, so later opens can use the menu button directly. Everything below is managed inside the Mini App:

The Mini App is protected by Telegram initData validation against TELEGRAM_TOKEN, restricted to the IDs in TELEGRAM_ID. In a plain browser the same URL asks for WEB_PASSWORD; when no password is configured, the Mini App is the only way in and the project landing page is shown instead.

On phones the app uses a native tab bar and navigation stack. On desktop clients (macOS, Telegram Desktop) and wide viewports it switches to an iPad-style split view with a sidebar, list and reading pane.

Usage

The push message structure is unchanged:

[Subject]

-----------
From : [sender]
To   : [recipient]

(Preview)(Summary)(Open)
  1. Preview shows the plain text body directly in the chat, limited to 4096 characters.
  2. Summary appears when a summary backend is enabled in Settings (Workers AI, or an OpenAI-compatible provider).
  3. Open launches the Mini App straight to this message's detail page. Telegram only allows Mini App buttons in private chats, so group notifications omit it.

Reply to any pushed message in Telegram to answer the sender through Resend.

CPU budget: parsing a message costs roughly 2ms of CPU per MB, so keep Max Size under ~2MB on the Workers free plan (10ms CPU per request). The paid plan's 30s budget has no such constraint. See Attachments.

Address lists

Rules are managed in the Mini App. A rule matches either an exact address (case-insensitive) or a regular expression. The white list takes precedence over the block list, so an allow rule can override a broad block rule for the same address.

Attachments

Attachments are stored in R2 and listed in the reader, where they can be downloaded. If no BUCKET binding is configured, mail is still stored without attachment contents.

Attachments need the whole message, so Max Size in Mail Handling has to be at least as large as the mail you want to receive. Base64 encoding adds about a third to every attachment, so budget the limit around the largest file you expect. Mail over the limit has its body truncated, and because attachments follow the body in the message, a truncated mail stores no attachments at all — the alternative would be handing you a corrupt download. The oversize policy Headers avoids the problem differently by skipping the body altogether.

Retention

Mail older than the retention setting is not part of the notification cache, but the full history remains until you delete it from the Mini App.

Development

The repo is a pnpm workspace with three packages and the deployable worker config at the root:

Path Package Contents
packages/server @mail2telegram/server Cloudflare Worker: email handlers, Telegram bot, Mini App API, D1 migrations and the Worker-runtime tests.
packages/web @mail2telegram/web Telegram Mini App (Vite + React), built to packages/web/dist/client.
packages/shared @mail2telegram/shared Types-only HTTP contract consumed by both sides; owns every type that crosses the wire.
pnpm install
pnpm dev            # Vite dev server on :5173 + wrangler dev on :8787
pnpm build          # typecheck every package and build the Mini App into packages/web/dist/client
pnpm typecheck      # tsc --noEmit for each package
pnpm test           # pure-logic tests (tsx) + Worker-runtime tests (vitest, Miniflare)
pnpm test:unit      # mergeSettings / testAddress / parseEmail only
pnpm test:pool      # D1, inbound-email and cleanup tests in the Workers runtime
pnpm lint           # oxlint
pnpm lint:fix       # oxlint --fix
pnpm format         # oxfmt
pnpm format:check   # oxfmt --check (used by CI)
pnpm screenshots    # regenerate the README images in docs/assets from mock data

wrangler dev serves the built packages/web/dist/client, so run pnpm build (or keep pnpm build:web running) at least once before starting it; pnpm dev runs both processes but does not build the assets.

Local D1 migrations:

pnpm db:migrate:local

The Mini App normally requires a valid Telegram initData signature. For local development, put WEB_PASSWORD=dev (plus your bot values) in the gitignored .dev.vars, run wrangler dev and open http://localhost:5173, then sign in with that password — exactly how a browser session works in production. The ?debug flag still mocks the Telegram UI (theme, viewport, platform) for previewing, and ?platform=ios previews the phone layout in a desktop browser. Never set a real WEB_PASSWORD in anything but the deployed worker's variables.

License

mail2telegram is released under the MIT license. See LICENSE for details.

mail2telegram (tbxark/mail2telegram)

Searching Database…

这是什么?mail2telegram 是一个接收你电子邮件的 Telegram 机器人,完全运行在 Cloudflare Workers 上,把聊天中的即时推送通知与用于查看历史、附件和设置的 Telegram Mini App 收件箱结合在一起。
这是一个独立服务器,还是完全运行在 Cloudflare 的无服务器平台上?它是完全无服务器的:机器人、Mini App API 和存储都运行在 Cloudflare Workers 上,数据用 D1、附件用 R2,因此没有需要管理的服务器。
怎么部署 mail2telegram?点击「Deploy to Cloudflare」按钮,它会克隆仓库、创建 D1 和 R2 资源、要求你填写 Telegram 机器人的相关值并完成部署;之后你需要绑定机器人并设置 Cloudflare Email Routing。文档还提供了通过 CLI、GitHub Actions 或 Workers Builds 的手动部署路径。
开始搭建之前我需要准备什么?一个带有 Workers、D1 和 R2 的 Cloudflare 账号、一个 Telegram 机器人 token,以及配置为把邮件转发给 worker 的 Cloudflare Email Routing;AI 摘要还需要 Workers AI 或兼容 OpenAI 的 API 密钥。
能接收的邮件有大小限制吗?有——解析每 MB 大约消耗 2ms CPU,因此在 Workers 免费套餐(10ms CPU 预算)下你需要把 Max Size 保持在约 2MB 以下;付费套餐的 30 秒预算则没有这个限制。超限邮件的正文会被截断,附件则会完全丢失。
Mini App 在手机和桌面上的外观一样吗?不一样——在手机上它使用原生标签栏和导航栈,而在桌面版 Telegram 客户端和宽视口下,它会切换为 iPad 风格的分栏视图,带侧边栏、列表和阅读窗格。
谁能打开 Mini App 查看我的邮件?Mini App 会用你的机器人 token 校验 Telegram 的 initData,只允许 TELEGRAMID 中列出的聊天 ID 访问;从普通浏览器打开同一 URL 则需要 WEBPASSWORD,如果未设置,显示的会是项目落地页而不是收件箱。
我可以直接从 Telegram 回复邮件吗?可以。在 Telegram 中回复任何推送的消息,mail2telegram 就会通过 Resend 把你的回复发给原始发件人。
旧邮件会怎么样?超过保留期限的邮件会从通知缓存中移除,但完整历史仍保留在 Mini App 中,直到你自己删除。
我可以筛选哪些发件人能联系到我吗?可以。Mini App 允许你设置白名单和黑名单,可按精确地址或正则匹配,同一地址的白名单规则优先于黑名单规则。
Creator
tbxark
Size
3.6 MB
Stars
904
Age
3y
Commits · 90 Days