better email error logging and some pretty printing for admin interface
This commit is contained in:
parent
67375bd281
commit
4152034e4a
9 changed files with 45 additions and 26 deletions
|
@ -1,8 +1,8 @@
|
|||
import logging
|
||||
|
||||
import aiosmtplib
|
||||
from asgiref.sync import sync_to_async
|
||||
from channels.layers import get_channel_layer
|
||||
from channels.db import database_sync_to_async
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from mail.models import Email, EventAddress, EmailAttachment
|
||||
|
@ -82,8 +82,7 @@ def make_reply(reply_email, references=None, event=None):
|
|||
return reply
|
||||
|
||||
|
||||
async def send_smtp(message, log):
|
||||
log.info('Sending message to %s' % message['To'])
|
||||
async def send_smtp(message):
|
||||
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
||||
|
||||
|
||||
|
@ -148,9 +147,9 @@ def parse_email_body(raw, log=None):
|
|||
attachments.append(attachment)
|
||||
if 'inline' in cdispo:
|
||||
body = body + f'<img src="cid:{attachment.id}">'
|
||||
log.info("Image", ctype, attachment.id)
|
||||
log.info("Image %s %s", ctype, attachment.id)
|
||||
else:
|
||||
log.info("Attachment", ctype, cdispo)
|
||||
log.info("Attachment %s %s", ctype, cdispo)
|
||||
else:
|
||||
if parsed.get_content_type() == 'text/plain':
|
||||
body = parsed.get_payload()
|
||||
|
@ -161,7 +160,7 @@ def parse_email_body(raw, log=None):
|
|||
soup = BeautifulSoup(body, 'html.parser')
|
||||
body = re.sub(r'([\r\n]+.?)*[\r\n]', r'\n', soup.get_text()).strip('\n')
|
||||
else:
|
||||
log.warning("Unknown content type", parsed.get_content_type())
|
||||
log.warning("Unknown content type %s", parsed.get_content_type())
|
||||
body = "Unknown content type"
|
||||
body = unescape_and_decode_quoted_printable(body)
|
||||
body = unescape_and_decode_base64(body)
|
||||
|
@ -172,6 +171,7 @@ def parse_email_body(raw, log=None):
|
|||
return parsed, body, attachments
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def receive_email(envelope, log=None):
|
||||
parsed, body, attachments = parse_email_body(envelope.content, log)
|
||||
|
||||
|
@ -255,9 +255,10 @@ class LMTPHandler:
|
|||
content = None
|
||||
try:
|
||||
content = envelope.content
|
||||
email, new, reply = await sync_to_async(receive_email)(envelope, log)
|
||||
email, new, reply = await receive_email(envelope, log)
|
||||
log.info(f"Created email {email.id}")
|
||||
systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id)
|
||||
systemevent = await database_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(
|
||||
|
@ -266,14 +267,15 @@ class LMTPHandler:
|
|||
)
|
||||
log.info(f"Sent message to frontend")
|
||||
if new and reply:
|
||||
await send_smtp(reply, log)
|
||||
log.info('Sending message to %s' % reply['To'])
|
||||
await send_smtp(reply)
|
||||
log.info("Sent auto reply")
|
||||
|
||||
return '250 Message accepted for delivery'
|
||||
except Exception as e:
|
||||
import uuid
|
||||
random_filename = 'mail-' + str(uuid.uuid4())
|
||||
from hashlib import sha256
|
||||
random_filename = 'mail-' + sha256(content).hexdigest()
|
||||
with open(random_filename, 'wb') as f:
|
||||
f.write(content)
|
||||
log.error(type(e), e, f"Saved email to {random_filename}")
|
||||
log.error(f"Saved email to {random_filename} because of error %s (%s)", e, type(e))
|
||||
return '451 Internal server error'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue