DEV Community

Pavithran M
Pavithran M

Posted on

📬 Automate Email Attachments to Telegram with Python + Render.com

Ever wished you could automatically forward email attachments to your Telegram chat or group? Whether it's invoices, reports, or alerts—this Python script does exactly that. And the best part? You can deploy it as a scheduled cron job on Render.com with zero hassle.

Let’s walk through the features, setup, and deployment steps to get this automation running in minutes.


🚀 What This App Does

This Python application:

  • ✅ Connects securely to Yahoo Mail via IMAP
  • 📂 Downloads the latest attachment from a specific folder (B2b)
  • 📤 Sends the attachment to a Telegram chat or group using the Telegram Bot API
  • 🧾 Logs each step for easy debugging
  • 🕒 Runs automatically on a schedule using Render.com’s cron job feature

Perfect for automating workflows that rely on email-based file delivery.


🛠️ Tech Stack

Here’s what powers the app:

  • Python 3
  • imaplib – for accessing email via IMAP
  • email – for parsing MIME messages
  • requests – for sending files to Telegram
  • dotenv – for managing environment variables locally

📦 Getting Started

1. Clone the Repository

git clone https://github.com/your-username/email-to-telegram.git
cd email-to-telegram
Enter fullscreen mode Exit fullscreen mode

2. Create a .env File (for local testing)

EMAIL_USER=your_yahoo_email@example.com
EMAIL_PASS=your_yahoo_app_password
TELEGRAM_TOKEN=your_telegram_bot_token
TELEGRAM_CHAT_ID=your_chat_id_or_group_id
Enter fullscreen mode Exit fullscreen mode

⚠️ Use a Yahoo App Password instead of your regular password for IMAP access.


🧑‍💻 Local Testing

Run the script manually to verify everything works:

python script.py
Enter fullscreen mode Exit fullscreen mode

You should see logs showing:

  • Connection to Yahoo Mail
  • Attachment download
  • Telegram API response

☁️ Deploying on Render.com

Render makes it super easy to automate this with a cron job.

Step 1: Create a New Cron Job

  • Go to Render Cron Jobs
  • Choose Python as the runtime
  • Set your schedule using cron syntax (e.g., 0 * * * * for hourly)

Step 2: Upload Your Code

  • Upload your script (script.py)
  • Include a requirements.txt with:
  requests
  python-dotenv
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Environment Variables

In the Environment section of the cron job setup:

  • EMAIL_USER
  • EMAIL_PASS
  • TELEGRAM_TOKEN
  • TELEGRAM_CHAT_ID

You don’t need the .env file on Render—just set the variables directly.

Step 4: Confirm and Deploy

  • Save and deploy the cron job
  • Check logs after the first scheduled run to confirm success

🧪 Troubleshooting Tips

  • No logs? Add print() statements to trace execution.
  • No attachment sent? Double-check the folder name (B2b) and email structure.
  • Telegram error? Make sure the bot isn’t blocked and has permission to send messages.

🧾 Final Thoughts

This setup is ideal for automating repetitive email-based workflows and keeping your Telegram feed updated with important files. You can easily extend it to support multiple folders, file types, or even filter by subject lines.

If you found this helpful, drop a ❤️ or share your own automation ideas in the comments!


Take a look at me!

Top comments (0)