init relay

This commit is contained in:
Shihaam Abdul Rahman 2024-09-21 01:49:53 +05:00
commit 5c99e21cfe
Signed by: shihaam
GPG Key ID: 6DA2E87EBC227636

43
relay.py Normal file
View File

@ -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()