From 8fac7c9711fb93df38e67853b28c9f4c13cd202d Mon Sep 17 00:00:00 2001 From: Patrick Craston Date: Fri, 1 Mar 2013 16:35:26 +0000 Subject: [PATCH] Only store text/plain and text/html in body field When parsing multipart message it was storing the whole message as string in database which could lead to database errors. Only want to store the actual message text --- django_mailbox/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index fa06590..b6b3bd0 100755 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -162,7 +162,14 @@ class Mailbox(models.Model): msg.message_id = message['message-id'][0:255] msg.from_header = message['from'] msg.to_header = message['to'] - msg.body = message.as_string() + # if message is multipart want to only store the actual message body in database (not attachments) + if message.is_multipart(): + for part in message.walk(): + if part.get_content_type() == 'text/plain' or part.get_content_type() == 'text/html': + msg.body = part.get_payload() + # else store the whole message in database + else: + msg.body = message.as_string() if message['in-reply-to']: try: msg.in_reply_to = Message.objects.filter(message_id=message['in-reply-to'])[0]