Skip to content

Commit

Permalink
Add necessary patchwork to work with 8-bit latin1 email
Browse files Browse the repository at this point in the history
  • Loading branch information
Mortal committed Jan 5, 2015
1 parent 585f75b commit b97e80c
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions emailtunnel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import os
import re
import sys
import logging
import datetime
Expand All @@ -37,6 +38,11 @@
import smtplib


# From smtplib.py
def _fix_eols(data):
return re.sub(r'(?:\r\n|\n|\r(?!\n))', "\r\n", data)


def now_string():
"""Return the current date and time as a string."""
return datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f")
Expand All @@ -61,6 +67,14 @@ def _sanity_check(self, message):
a = message.rstrip('\n')
b = str(self).rstrip('\n')

if '\ufffd' in a:
logging.debug(
'Original message is not sane; contains REPLACEMENT CHARACTER')

if '\ufffd' in b:
logging.debug(
'Parsed message is not sane; contains REPLACEMENT CHARACTER')

if a == b:
return

Expand Down Expand Up @@ -194,10 +208,19 @@ def collect_incoming_data(self, data):
try:
str_data = str(data, 'utf-8')
except UnicodeDecodeError:
logging.error('ResilientSMTPChannel.collect_incoming_data: ' +
'UnicodeDecodeError encountered; decoding with ' +
'errors=replace')
str_data = data.decode('utf-8', 'replace')
try:
str_data = data.decode('latin1')
except UnicodeDecodeError:
logging.error(
'ResilientSMTPChannel.collect_incoming_data: ' +
'UnicodeDecodeError encountered; decoding as ' +
'utf-8, errors=replace')
str_data = data.decode('utf-8', 'replace')
else:
logging.warning(
'ResilientSMTPChannel.collect_incoming_data: ' +
'UnicodeDecodeError encountered; decoding as ' +
'latin1')

# str_data.encode('utf-8').decode('utf-8') will surely not raise
# a UnicodeDecodeError in SMTPChannel.collect_incoming_data.
Expand Down Expand Up @@ -295,7 +318,8 @@ def deliver(self, message, recipients, sender):
logging.info('RCPT TO: %r MAIL FROM: %r Subject: %r'
% (recipients, sender, str(message.subject)))
try:
relay_host.sendmail(sender, recipients, str(message))
str_message = _fix_eols(str(message))
relay_host.sendmail(sender, recipients, str_message.encode('latin1'))
finally:
try:
relay_host.quit()
Expand Down

0 comments on commit b97e80c

Please sign in to comment.