parse
ben/matrix-smtp-webhook/pipeline/head There was a failure building this commit Details

main
parent 94002ed432
commit 2d0baee2a3

@ -1,5 +1,6 @@
import os
import asyncio
from email.parser import Parser
from email.policy import default
from aiosmtpd.controller import Controller
import requests
@ -32,19 +33,23 @@ class WebhookHandler:
return '250 OK'
async def handle_DATA(self, server, session, envelope):
c = envelope.content.decode('utf8', errors='replace').splitlines()
content = [f" {a.strip()}" for a in c]
p = Parser(policy=default).parsestr(
envelope.content.decode('utf8', errors='replace'))
content = p.get_body(preferencelist="plain").get_content()
body = "\n".join([f" {a.strip()}" for a in content.splitlines()])
msg_from = envelope.mail_from
msg_to = ", ".join(envelope.rcpt_tos)
logger.success(f"from: {msg_from}, to: {msg_to}")
msg = "\n\n".join([
headers = "<br>".join([
f"**From**: `{msg_from}`",
f"**To**: `{msg_to}`",
"\n".join(content)
f"**Subject**: `{p.get('subject', '')}`"
])
msg = "\n\n".join([headers, body])
matrix_msg(msg)
logger.success(f"from: {msg_from}, to: {msg_to}")
print('End of message')
return '250 Message accepted for delivery'

Loading…
Cancel
Save