This Docker Image provides a TelegramBot which can fetch emails via IMAP and forward them or only their subjects to Telegram Chats.
You will need an IMAP Mailserver (account), a Docker host and a TelegramBot token. Create the last using the BotFather.
The IMAP account should have multiple email addresses, so the TelegramBot can handle multiple Telegram Chats as destination for the mails.
- Create the IMAP account.
- Prepare the Docker container.
- Copy the
docker-compose.ymland fill in the details. - Copy the
rules.phpand fill with rules. - Add the
logfolder, if you want to.- set the rights using
docker exec -it --user root telegram_mail chown -R php:php /home/php/telegram/log
- set the rights using
- Copy the
- Start the docker container, e.g.
docker-compose up -d. - Test the setup, send a Mail to the IMAP account covered by a rule.
- Call
curl 172.19.198.8from the host, the container should answerOK. - The Telegram message should arrive.
- Call
- Make sure to notify the container about new emails, e.g.:
- Use a cronjob and call every
xminutes. - Use
incronand monitor the IMAP (Dovecot) folder for the user -- useIN_MOVED_TO. - Use a Postfix Filter to call the script.
- ...
- Use a cronjob and call every
The rules.php which should be mapped into the container at /home/php/telegram/rules.php. You may use the :ro
flag.
RULES::TELEGRAMis an array and defines the email address which receives the emails and the telegram chat, where the mails are send to.mailtothe user-part of the email (@SYSDOMAINwill be appended).taga tag to name the mails in the telegram chatsteltoan array of telegram users (first send a message to the bot and the see the id atdocker exec -it telegram_mail php ./telegram_info.php)
RULES::MAILOVERVIEWis an array and defines the email address which receives the emails and the telegram chat, where reports about the subject of the mails are send to.- see at
RULES::TELEGRAM
- see at
<?php
class RULES {
//Telegram pushes, one array per rule
const TELEGRAM = array(
array(
'mailto' => array( 'test'), // the addresses the mails goes to (only small letters, <part>@SYSDOMAIN)
'tag' => '[TELEGRAM-TEST]', // TAG to be prepended to subject
'telto' => array( '0000000' ) // the telegram chat, to send the mail to
),
// ....
);
//Mailoverview
const MAILOVERVIEW = array(
array(
'mailto' => 'test', // the addresses the mails goes to (only small letters, <part>@SYSDOMAIN)
'tag' => '[Overview-Test]', // TAG to be used as subject
'telto' => array( '0000000' ) // the telegram chat, to send the mail to
),
// ...
);
}
?>
The docker-compose.yml to setup the Docker Image.
TELEGRAM_API_TOKENThe TelegramBot token created by the BotFather.MAIL_SERVERThe IMAP server in PHP notation, e.g.{localhost:993/imap/ssl}INBOXMAIL_USERThe username for the IMAP account.MAIL_PWThe password for the IMAP account.SYSDOMAINThe domain wich will be appended to themailtoparts in the rules.DELETMAILSShould the mails be deleted?true, false(If usingfalsemails will be forwarded forever.)
services:
telebot:
image: kimbtechnologies/telegrambot:latest
container_name: telegram_mail
volumes:
- ./rules.php:/home/php/telegram/rules.php:ro # load the rules from outside, rules.php has to be placed on the host manually.
# - ./log/:/home/php/telegram/log # place the logs outside of the container, make sure php has the right to write the files
restart: always
environment:
- TELEGRAM_API_TOKEN=tbf
- MAIL_SERVER=tbf
- MAIL_USER=tbf
- MAIL_PW=tbf
- SYSDOMAIN=tbf
- DELETMAILS=tbf
networks:
telegram:
ipv4_address: 172.19.198.8
restart: always
networks:
telegram:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.19.198.0/24
The script which runs the bot and should be called on new mails. (It just does a HTTP request to the container, the container will do the rest.)
#!/bin/sh
curl -s 172.19.198.8 > /dev/null