# Buggy code
def viewheaders(request, mail_id):
    """Display message headers."""
    content = ""
    for qm in get_connector().get_mail_content(mail_id):
        content += qm.mail_text
    if type(content) is unicode:
        content = content.encode("utf-8")
    msg = email.message_from_string(content)
    headers = []
    for name, value in msg.items():
        if value:
            result = chardet.detect(value)
            if result["encoding"] is not None:
                value = value.decode(result["encoding"])
        headers += [(name, value)]
    return render(request, "amavis/viewheader.html", {"headers": headers})


# Traceback (DataError)
Traceback (most recent call last):

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/extensions/amavis/lib.py", line 36, in wrapped_f
return f(request, *args, **kwargs)

File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/extensions/amavis/views.py", line 135, in getmailcontent
mail = SQLemail(mail_id, mformat="plain", links="0")

File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/extensions/amavis/sql_email.py", line 23, in __init__
{"name": label, "value": self.get_header(self.msg, f)}

File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/extensions/amavis/sql_email.py", line 44, in msg
mail_text = "".join([qm.mail_text for qm in qmails])

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/models/query.py", line 96, in __iter__
self._fetch_all()

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/srv/modoboa/env/local/lib/python2.7/site-packages/Django-1.6.8-py2.7.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)

DataError: invalid byte sequence for encoding "UTF8": 0xb3

# Fixed code
def viewheaders(request, mail_id):
    """Display message headers."""
    content = get_connector().get_mail_content(mail_id)
    msg = email.message_from_string(content)
    headers = []
    for name, value in msg.items():
        if value:
            result = chardet.detect(value)
            if result["encoding"] is not None:
                value = value.decode(result["encoding"])
        headers += [(name, value)]
    return render(request, "amavis/viewheader.html", {"headers": headers})