Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of missing 'content-transfer-encoding' header #137

Merged
merged 1 commit into from
May 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def get_connection(self):
def process_incoming_message(self, message):
"""Process a message incoming to this mailbox."""
msg = self._process_message(message)
if msg is None:
return None
msg.outgoing = False
msg.save()

Expand All @@ -235,6 +237,8 @@ def process_incoming_message(self, message):
def record_outgoing_message(self, message):
"""Record an outgoing message associated with this mailbox."""
msg = self._process_message(message)
if msg is None:
return None
msg.outgoing = True
msg.save()
return msg
Expand Down Expand Up @@ -363,7 +367,14 @@ def _process_message(self, message):
)
msg.save()
message = self._get_dehydrated_message(message, msg)
msg.set_body(message.as_string())
try:
body = message.as_string()
except KeyError as exc:
# email.message.replace_header may raise 'KeyError' if the header
# 'content-transfer-encoding' is missing
logger.warning("Failed to parse message: %s", exc,)
return None
msg.set_body(body)
if message['in-reply-to']:
try:
msg.in_reply_to = Message.objects.filter(
Expand All @@ -382,7 +393,8 @@ def get_new_mail(self, condition=None):
return new_mail
for message in connection.get_message(condition):
msg = self.process_incoming_message(message)
new_mail.append(msg)
if not msg is None:
new_mail.append(msg)
self.last_polling = now()
if django.VERSION >= (1, 5): # Django 1.5 introduces update_fields
self.save(update_fields=['last_polling'])
Expand Down