commit 5c99e21cfe9cbc8034401e2f1bf079e4ebabe800 Author: Shihaam Abdul Rahman Date: Sat Sep 21 01:49:53 2024 +0500 init relay diff --git a/relay.py b/relay.py new file mode 100644 index 0000000..dc44e88 --- /dev/null +++ b/relay.py @@ -0,0 +1,43 @@ +import os +import asyncio +from aiosmtpd.controller import Controller +from email.parser import BytesParser +from email.policy import default +import telegram + +# Telegram configuration +TELEGRAM_API_KEY = os.getenv('TELEGRAM_API_KEY') +TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') + +# SMTP configuration +SMTP_HOST = '0.0.0.0' # Listen on all interfaces +SMTP_PORT = 2525 # Standard SMTP port + +class SMTPHandler: + async def handle_RCPT(self, server, session, envelope, address, rcpt_options): + envelope.rcpt_tos.append(address) + return '250 OK' + + async def handle_DATA(self, server, session, envelope): + parser = BytesParser(policy=default) + message = parser.parsebytes(envelope.content) + + subject = message.get('subject', 'No subject') + body = message.get_body(preferencelist=('plain', 'html')).get_content() + + telegram_message = f"Subject: {subject}\n\n{body}" + + bot = telegram.Bot(TELEGRAM_API_KEY) + await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=telegram_message) + + return '250 Message accepted for delivery' + +async def start_smtp_server(): + controller = Controller(SMTPHandler(), hostname=SMTP_HOST, port=SMTP_PORT) + controller.start() + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.create_task(start_smtp_server()) + print(f"SMTP server started on {SMTP_HOST}:{SMTP_PORT}") + loop.run_forever()