import os import asyncio from aiosmtpd.controller import Controller from email.parser import BytesParser from email.policy import default import telegram import re import httpx # Configuration TELEGRAM_API_KEY = os.getenv('TELEGRAM_API_KEY') DEFAULT_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') # Fallback chat ID NTFY_TOKEN = os.getenv('NTFY_TOKEN') NTFY_URL = os.getenv('NTFY_URL') # SMTP configuration SMTP_HOST = '0.0.0.0' # Listen on all interfaces SMTP_PORT = 2525 # Alternative 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() full_message = f"Subject: {subject}\n\n{body}" # Extract destination from the recipient email address destination, service = self.extract_destination(envelope.rcpt_tos[0]) if service == 'telegram': await self.send_telegram(destination, full_message) elif service == 'ntfy': await self.send_ntfy(destination, subject, body) else: print(f"Invalid service: {service}. Message not sent.") return '250 Message accepted for delivery' def extract_destination(self, email): match = re.match(r'(.+)@(telegram|ntfy)$', email) if match: return match.group(1), match.group(2) print(f"Invalid email format: {email}. Using default Telegram chat ID.") return DEFAULT_CHAT_ID, 'telegram' async def send_telegram(self, chat_id, message): bot = telegram.Bot(TELEGRAM_API_KEY) try: await bot.send_message(chat_id=chat_id, text=message) print(f"Message sent to Telegram chat ID: {chat_id}") except telegram.error.BadRequest as e: print(f"Failed to send message to Telegram chat ID: {chat_id}. Error: {str(e)}") print("Attempting to send to default Telegram chat ID.") await bot.send_message(chat_id=DEFAULT_CHAT_ID, text=message) async def send_ntfy(self, topic, subject, body): headers = { "Authorization": f"Bearer {NTFY_TOKEN}", "Title": subject, "Content-Type": "text/plain", } async with httpx.AsyncClient() as client: try: response = await client.post(f"{NTFY_URL}/{topic}", content=body, headers=headers) response.raise_for_status() print(f"Message sent to ntfy topic: {topic}") except httpx.HTTPStatusError as e: print(f"Failed to send message to ntfy topic: {topic}. Error: {str(e)}") 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()