
A Python script that forwards new emails from Gmail to a Telegram chat.
This Google Apps Script creates a powerful integration between your Gmail and Telegram, allowing you to:
/send command, supporting recipients, subject, body, and even attachments (documents, photos, videos, audio).Follow these steps to set up the script in Google Apps Script:
Code.gs file).@BotFather.@BotFather and use the command /newbot.You need the ID of the chat where your bot will send/receive messages.
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates (replace <YOUR_BOT_TOKEN> with your actual bot token). Look for "chat":{"id":... in the JSON response. The number after id: is your chat ID.@YourBotName hi). Then, go to the getUpdates URL as above. The chat ID for a group will be a negative number.In the Google Apps Script editor, locate the Configuration section at the top of the code and update the following:
const TELEGRAM_BOT_TOKEN = 'YOUR_BOT_API_TOKEN'; // Replace with your actual bot token
const TELEGRAM_CHAT_ID = 'YOUR_CHAT_ID'; // Replace with your Telegram Chat IDFor the script to access Gmail, you need to enable the Gmail API.
Ctrl + S (Windows) / Cmd + S (Mac).To make the script run automatically, you need to set up time-driven triggers.
In the Google Apps Script editor, click on the "Triggers" icon (alarm clock icon on the left sidebar).
Click "Add Trigger" (bottom right).
For forwardEmailToTelegram (Email Forwarding):
forwardEmailToTelegramHeadTime-drivenMinute timer (or 5 minutes, 10 minutes, etc., depending on how frequently you want to check for emails)Every 5 minutes (recommended)Save.For processTelegramCommands (Telegram Command Processing):
processTelegramCommandsHeadTime-drivenMinute timerEvery 1 minute (recommended for responsiveness)Save.Authorization: The first time a trigger runs, Google will ask you to authorize the script to access your Gmail and connect to external services. Follow the prompts to grant the necessary permissions.
Once the forwardEmailToTelegram trigger is set up, the script will automatically check for new, unread emails in your Gmail's Primary Inbox and forward them to your specified Telegram chat. It will then mark the emails as read.
You can send emails directly from your Telegram chat using the /send command.
Basic Email:
/send recipient@example.com,, Subject of the email,, Your email body goes here.Email with Attachments:
To send an email with an attachment, first upload the file (document, photo, video, or audio) to the Telegram chat, and in the caption of that file, use the /send command:
/send recipient@example.com,, Subject with attachment,, Email body with attachment(Upload file with this caption)
Sending Multiple Emails:
You can add an optional fourth parameter to send the email multiple times.
/send recipient@example.com,, Subject for multiple,, Body for multiple,, 5(This will send the email 5 times)
TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID are sensitive. Keep them confidential.forwardEmailToTelegram function specifically looks for emails in the category:primary inbox. Emails in other categories (Social, Promotions, Updates, Forums) will not be forwarded.Logger tab) for any errors or debugging information.This project is open-source and available under the MIT License.