2023-11-27 00:38:43 +00:00
|
|
|
import logging
|
|
|
|
import aiosmtplib
|
2024-01-07 20:18:42 +00:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from channels.layers import get_channel_layer
|
2023-11-27 00:38:43 +00:00
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
from mail.models import Email, EventAddress
|
|
|
|
from notify_sessions.models import SystemEvent
|
|
|
|
from tickets.models import IssueThread, StateChange
|
2023-11-27 00:38:43 +00:00
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
|
|
|
|
def collect_references(issue_thread):
|
|
|
|
mails = issue_thread.emails.order_by('timestamp')
|
|
|
|
references = []
|
|
|
|
for mail in mails:
|
|
|
|
if mail.reference:
|
|
|
|
references.append(mail.reference)
|
|
|
|
return references
|
|
|
|
|
|
|
|
|
|
|
|
def make_reply(reply_email, references=None, event=None, issue_thread=None):
|
2023-11-27 00:38:43 +00:00
|
|
|
from email.message import EmailMessage
|
|
|
|
from core.settings import MAIL_DOMAIN
|
2024-01-07 20:18:42 +00:00
|
|
|
event = event or "noreply"
|
2023-11-27 00:38:43 +00:00
|
|
|
reply = EmailMessage()
|
2024-01-07 20:18:42 +00:00
|
|
|
reply["From"] = reply_email.sender
|
|
|
|
reply["To"] = reply_email.recipient
|
|
|
|
reply["Subject"] = reply_email.subject
|
|
|
|
reply["Reply-To"] = f"{event}+{issue_thread}@{MAIL_DOMAIN}"
|
|
|
|
if reply_email.in_reply_to:
|
|
|
|
reply["In-Reply-To"] = reply_email.in_reply_to
|
|
|
|
if reply_email.reference:
|
|
|
|
reply["Message-ID"] = reply_email.reference
|
|
|
|
else:
|
|
|
|
reply["Message-ID"] = reply_email.id + "@" + MAIL_DOMAIN
|
|
|
|
reply_email.reference = reply["Message-ID"]
|
|
|
|
reply_email.save()
|
|
|
|
if references:
|
|
|
|
reply["References"] = " ".join(references)
|
|
|
|
|
|
|
|
reply.set_content(reply_email.body)
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
return reply
|
|
|
|
|
|
|
|
|
|
|
|
async def send_smtp(message, log):
|
|
|
|
log.info('Sending message to %s' % message['To'])
|
|
|
|
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
|
|
|
|
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
def find_active_issue_thread(in_reply_to, subject=None):
|
|
|
|
reply_to = Email.objects.filter(reference=in_reply_to)
|
|
|
|
if reply_to.exists():
|
|
|
|
return reply_to.first().issue_thread, False
|
|
|
|
else:
|
|
|
|
issue = IssueThread.objects.create(name=subject)
|
|
|
|
return issue, True
|
|
|
|
|
|
|
|
|
2023-11-27 00:38:43 +00:00
|
|
|
class LMTPHandler:
|
|
|
|
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
|
|
|
|
from core.settings import MAIL_DOMAIN
|
|
|
|
if not address.endswith('@' + MAIL_DOMAIN):
|
|
|
|
return '550 not relaying to that domain'
|
|
|
|
envelope.rcpt_tos.append(address)
|
|
|
|
return '250 OK'
|
|
|
|
|
|
|
|
async def handle_DATA(self, server, session, envelope):
|
|
|
|
import email
|
|
|
|
log = logging.getLogger('mail.log')
|
2024-01-07 20:18:42 +00:00
|
|
|
log.setLevel(logging.DEBUG)
|
2023-11-27 00:38:43 +00:00
|
|
|
log.info('Message from %s' % envelope.mail_from)
|
|
|
|
log.info('Message for %s' % envelope.rcpt_tos)
|
|
|
|
log.info('Message data:\n')
|
|
|
|
|
|
|
|
try:
|
|
|
|
parsed = email.message_from_bytes(envelope.content)
|
|
|
|
body = ""
|
|
|
|
if parsed.is_multipart():
|
|
|
|
for part in parsed.walk():
|
|
|
|
ctype = part.get_content_type()
|
|
|
|
cdispo = str(part.get('Content-Disposition'))
|
|
|
|
|
|
|
|
# skip any text/plain (txt) attachments
|
|
|
|
if ctype == 'text/plain' and 'attachment' not in cdispo:
|
|
|
|
body = part.get_payload(decode=True)
|
|
|
|
else:
|
|
|
|
log.info("Attachment", ctype, cdispo)
|
|
|
|
else:
|
|
|
|
body = parsed.get_payload(decode=True)
|
|
|
|
log.info(body)
|
|
|
|
|
|
|
|
header_from = parsed.get('From')
|
|
|
|
header_to = parsed.get('To')
|
2024-01-07 20:18:42 +00:00
|
|
|
header_in_reply_to = parsed.get('In-Reply-To')
|
|
|
|
header_message_id = parsed.get('Message-ID')
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
if header_from != envelope.mail_from:
|
|
|
|
log.warning("Header from does not match envelope from")
|
2024-01-07 20:18:42 +00:00
|
|
|
log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
if header_to != envelope.rcpt_tos[0]:
|
|
|
|
log.warning("Header to does not match envelope to")
|
2024-01-07 20:18:42 +00:00
|
|
|
log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
|
|
|
|
|
|
|
|
recipient = envelope.rcpt_tos[0]
|
|
|
|
sender = envelope.mail_from
|
|
|
|
subject = parsed.get('Subject')
|
|
|
|
target_event = None
|
|
|
|
try:
|
|
|
|
address_map = await sync_to_async(EventAddress.objects.get)(address=recipient)
|
|
|
|
if address_map.event:
|
|
|
|
target_event = address_map.event
|
|
|
|
except EventAddress.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
active_issue_thread, new = await sync_to_async(find_active_issue_thread)(header_in_reply_to, subject)
|
|
|
|
|
|
|
|
email = await sync_to_async(Email.objects.create)(sender=sender,
|
|
|
|
recipient=recipient,
|
|
|
|
body=body.decode('utf-8'),
|
|
|
|
subject=subject,
|
|
|
|
reference=header_message_id,
|
|
|
|
in_reply_to=header_in_reply_to,
|
|
|
|
raw=envelope.content.decode('utf-8'),
|
|
|
|
event=target_event,
|
|
|
|
issue_thread=active_issue_thread)
|
|
|
|
log.info(f"Created email {email.id}")
|
|
|
|
systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id)
|
|
|
|
log.info(f"Created system event {systemevent.id}")
|
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
await channel_layer.group_send(
|
|
|
|
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
|
|
|
|
"message": "email received"}
|
|
|
|
)
|
|
|
|
log.info(f"Sent message to frontend")
|
|
|
|
if new:
|
|
|
|
await sync_to_async(StateChange.objects.create)(issue_thread=active_issue_thread, state='new')
|
|
|
|
|
|
|
|
references = await sync_to_async(collect_references)(active_issue_thread)
|
|
|
|
|
|
|
|
reply_email = await sync_to_async(Email.objects.create)(sender=recipient, # "noreply@" + MAIL_DOMAIN,
|
|
|
|
recipient=sender,
|
|
|
|
body="Thank you for your message.",
|
|
|
|
subject="Message received",
|
|
|
|
in_reply_to=header_message_id,
|
|
|
|
event=target_event,
|
|
|
|
issue_thread=active_issue_thread)
|
|
|
|
await send_smtp(make_reply(reply_email, references), log)
|
|
|
|
log.info("Sent auto reply")
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
return '250 Message accepted for delivery'
|
|
|
|
except Exception as e:
|
|
|
|
log.error(e)
|
|
|
|
return '550 Message rejected'
|