Compare commits

...

2 commits

Author SHA1 Message Date
jedi 4a712240fc stash 2024-05-04 03:08:00 +02:00
jedi 6d3f9f2a4c stash 2024-05-04 03:03:21 +02:00
2 changed files with 27 additions and 18 deletions

View file

@ -202,17 +202,17 @@ def receive_email(envelope, log=None):
header_in_reply_to = parsed.get('In-Reply-To')
header_message_id = parsed.get('Message-ID')
# if header_from != envelope.mail_from:
# log.warning("Header from does not match envelope from")
# log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
#
# if header_to != envelope.rcpt_tos[0]:
# log.warning("Header to does not match envelope to")
# log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
# if header_from != envelope.mail_from:
# log.warning("Header from does not match envelope from")
# log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
#
# if header_to != envelope.rcpt_tos[0]:
# log.warning("Header to does not match envelope to")
# log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
# handle undelivered mail header_from : 'Mail Delivery System <MAILER-DAEMON@...'
if match(r'^[a-zA-Z ]*<?MAILER-DAEMON@', header_from) and envelope.mail_from.strip("<>") == "":
if match(r'^([a-zA-Z ]*<)?MAILER-DAEMON@', header_from) and envelope.mail_from.strip("<>") == "":
log.warning("Ignoring mailer daemon")
raise SpecialMailException("Ignoring mailer daemon")
@ -288,22 +288,26 @@ class LMTPHandler:
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
"message": "email received"})
log.info(f"Sent message to frontend")
if thread:
await channel_layer.group_send(
'general', {"type": "generic.event", "name": "user_notification", "event_id": systemevent.id,
"ticket_id": thread.id, "new": new})
if new and reply:
log.info('Sending message to %s' % reply['To'])
await send_smtp(reply)
log.info("Sent auto reply")
if thread:
await channel_layer.group_send(
'general', {"type": "generic.event", "name": "user_notification", "event_id": systemevent.id,
"ticket_id": thread.id, "new": new})
else:
print("No thread found")
return '250 Message accepted for delivery'
except SpecialMailException as e:
import uuid
random_filename = 'special-' + str(uuid.uuid4())
with open(random_filename, 'wb') as f:
f.write(content)
log.warning(f"Special mail exception: {e}")
log.warning(f"Special mail exception: {e} saved to {random_filename}")
return '250 Message accepted for delivery'
except Exception as e:
from hashlib import sha256

View file

@ -1,3 +1,5 @@
import asyncio
from aiohttp.client import ClientSession
from channels.layers import get_channel_layer
from channels.db import database_sync_to_async
@ -22,8 +24,8 @@ async def telegram_notify(message, chat_id):
return await http_get(url)
async def email_notify(message, email):
mail = make_notification(message, email)
async def email_notify(message, title, email):
mail = make_notification(message, email, title)
await send_smtp(mail)
@ -66,15 +68,18 @@ class NotificationDispatcher:
async def dispatch(self, ticket, event_id, new):
message = await render_notification_new_ticket_async(
ticket) if new else await render_notification_reply_ticket_async(ticket)
title = f"{ticket.name} [#{ticket.short_uuid()}]"
print("Dispatching message:", message, "with event_id:", event_id)
targets = await self.get_notification_targets()
# await telegram_notify(message, TELEGRAM_GROUP_CHAT_ID)
jobs = []
# jobs.append(telegram_notify(message, TELEGRAM_GROUP_CHAT_ID))
for target in targets:
if target.channel_type == 'telegram':
print("Sending telegram notification to:", target.channel_target)
await telegram_notify(message, target.channel_target)
jobs.append(telegram_notify(message, target.channel_target))
elif target.channel_type == 'email':
print("Sending email notification to:", target.channel_target)
await email_notify(message, target.channel_target)
jobs.append(email_notify(message, title, target.channel_target))
else:
print("Unknown channel type:", target.channel_type)
await asyncio.gather(*jobs)