Source: https://cli.nylas.com/guides/email-to-telegram-notifications

# Send Email to Telegram (CLI)

Telegram is where many on-call engineers and small teams already get pinged, and a bot can post to a chat with a single HTTPS request. This guide pipes your inbox to a Telegram chat from the terminal: pull messages as JSON with the Nylas CLI, build the message text with jq, and call the Telegram Bot API sendMessage method with curl. No bot framework, no SMTP forwarding, no paid automation.

Written by [Caleb Geene](https://cli.nylas.com/authors/caleb-geene) Director, Site Reliability Engineering

Reviewed by [Qasim Muhammad](https://cli.nylas.com/authors/qasim-muhammad)

Updated June 9, 2026

> **TL;DR:** Pull messages with `nylas email search --json`, shape each one into a message string with `jq`, and POST it to the Telegram Bot API `sendMessage` method with `curl`. There is one piece you have to get right before any of it works — the chat ID — and the last section shows the two-second trick to find it.

Command references used in this guide: [`nylas email search`](https://cli.nylas.com/docs/commands/email-search), [`nylas email list`](https://cli.nylas.com/docs/commands/email-list), and [`nylas email read`](https://cli.nylas.com/docs/commands/email-read).

## How do I send an email to Telegram?

You send an email to Telegram by pulling the message as JSON, building a text string from it, and POSTing that string to the Telegram Bot API `sendMessage` method. The Nylas CLI returns structured messages with `nylas email search --json`, and a single `curl` call delivers each one to a chat. The whole pipeline is three commands and runs in under a second per message.

Two pieces of setup come first. Create a bot with [BotFather](https://core.telegram.org/bots/features#botfather) to get a token of the form `123456:ABC-DEF...`, then find the numeric chat ID of the chat you want to post to. The `sendMessage` method takes both as parameters, documented in the [Bot API reference](https://core.telegram.org/bots/api#sendmessage). Install the CLI first if you have not already.

```bash
# macOS / Linux — see /guides/getting-started for other methods
brew install nylas/nylas-cli/nylas

# Pull the messages you want to forward to Telegram
nylas email search "subject:incident" --unread --json --limit 50 > items.json
```

## How do I build the Telegram message with jq?

You build the Telegram message by mapping each email's subject, sender, and snippet into a single text string with `jq`. The `--json` output is an array of message objects, so iterate with `jq -c '.[]'` and read fields like `.subject` and `.from[0].email`. A null subject would otherwise print the word “null”, so fall back to a placeholder on every field.

Telegram's `sendMessage` caps a single message at 4,096 UTF-8 characters, so truncate long bodies before sending. The `parse_mode` parameter accepts `MarkdownV2` or `HTML`; if you pass Markdown, escape the reserved characters or the request returns a 400. The example below keeps it plain text to avoid that escaping work entirely.

```bash
jq -r '.[] | "\(.subject // "(no subject)")\n\(.from[0].email // "unknown sender")\n\(.snippet // "")"' items.json | head -c 4096 > body.txt

cat body.txt
```

## How do I call the Telegram Bot API sendMessage method?

You call `sendMessage` with an HTTPS POST to `https://api.telegram.org/bot<TOKEN>/sendMessage`, passing `chat_id` and `text` as form fields. Telegram returns JSON with `"ok": true` on success. The token lives in the URL path, not a header, which is why you keep it in an environment variable rather than echoing it. A successful call delivers in well under one second.

Loop over the messages and send one Telegram message per email so each lands as its own chat entry. Using `--data-urlencode` in `curl` encodes the subject and body safely, so spaces, ampersands, and newlines survive transport intact. Telegram allows roughly 30 messages per second to different chats and about 1 message per second to the same chat before it returns a 429, so add a short sleep when forwarding bursts.

```bash
TELEGRAM_TOKEN="123456:ABC-DEF-your-token"
CHAT_ID="-1001234567890"

jq -c '.[]' items.json | while read -r msg; do
  subject=$(echo "$msg" | jq -r '.subject // "(no subject)"')
  sender=$(echo "$msg"  | jq -r '.from[0].email // "unknown sender"')
  text="New email: $subject"$'\n'"From: $sender"

  curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \
    --data-urlencode "chat_id=$CHAT_ID" \
    --data-urlencode "text=$text"
  sleep 1
done
```

## How do I run the forwarder on a schedule?

You run the forwarder on a schedule with cron, scoping each run to recent mail so you never re-send the same message. Pair `nylas email search` with `--unread` and a tight query, or call `nylas email list --unread --json` and mark each forwarded message read so the next run skips it. A cron entry firing every five minutes keeps a Telegram chat current with about 288 polls a day.

For near-instant delivery, drive the same `sendMessage` call from a Nylas webhook on the `message.created` trigger instead of polling. When a new email arrives, the webhook fires and the forwarder posts it within seconds. The Telegram token and the mailbox grant are independent credentials, so you can rotate the bot token without touching the CLI auth. Store both outside the script.

```bash
# Forward unread incident mail to Telegram every 5 minutes
*/5 * * * * /usr/local/bin/email-to-telegram.sh >> /var/log/email-telegram.log 2>&1

# Register a webhook for real-time delivery instead of polling
nylas webhook create --url "https://example.com/telegram-hook" --triggers message.created
```

## Why use the Bot API instead of email forwarding?

The Telegram Bot API gives you a programmable, filtered feed that plain email forwarding cannot. A forwarding rule sends every matching message verbatim; a bot lets you reshape the text, drop noise with `jq`, route different senders to different chats, and add buttons or links. Telegram reports more than 1 billion monthly active users as of 2025, and bots reach any of them through one HTTPS endpoint.

Pulling the mail through the CLI also means one integration works across providers. Whether the mailbox is Gmail, governed by the [Gmail API](https://developers.google.com/workspace/gmail/api/guides), or Microsoft 365, governed by the [Microsoft Graph mail API](https://learn.microsoft.com/en-us/graph/api/resources/mail-api-overview), the JSON shape stays identical. Email address formats follow [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322), so the `.from[0].email` field is consistent regardless of backend.

## Next steps

- [Send and Parse Email with One API](https://cli.nylas.com/guides/send-and-parse-email-one-api) — Send with nylas email send and parse replies with nylas email…
- [Send Email Alerts to Microsoft Teams](https://cli.nylas.com/guides/email-to-teams-notifications) — Forward matching email into a Teams channel after the connector…
- [Email to Slack alerts](https://cli.nylas.com/guides/email-to-slack-notifications) — the same pattern into a Slack channel
- [Email to Discord notifications](https://cli.nylas.com/guides/email-to-discord-notifications) — post to a Discord channel webhook
- [Email to Mattermost notifications](https://cli.nylas.com/guides/email-to-mattermost-notifications) — the self-hosted chat variant
- [Email to Jira issues](https://cli.nylas.com/guides/email-to-jira-issues) — turn mail into tracked tickets
- [Debug email delivery](https://cli.nylas.com/guides/debug-email-delivery-cli) — trace why a message did or did not arrive
- [Full command reference](https://cli.nylas.com/docs/commands) — every flag and subcommand documented
- [Telegram Bot API reference](https://core.telegram.org/bots/api) — every method and parameter
