Skip to content

Commit

Permalink
Fix handling of missing 'content-transfer-encoding' header
Browse files Browse the repository at this point in the history
Closes: coddingtonbear#136

Python's email module raises a KeyError if the header
'content-transfer-encoding' is missing:
  https://bugs.python.org/issue27321
  • Loading branch information
sumpfralle authored and ad-m committed Jul 9, 2017
1 parent 9df9f0a commit 91ceb43
Showing 1 changed file with 14 additions and 2 deletions.
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

0 comments on commit 91ceb43

Please sign in to comment.