# Buggy code
def mark_messages(request, selection, mtype, recipient_db=None):
    """Mark a selection of messages as spam.

    :param str selection: message unique identifier
    :param str mtype: type of marking (spam or ham)
    """
    if not manual_learning_enabled(request.user):
        return render_to_json_response({"status": "ok"})
    if recipient_db is None:
        recipient_db = "user" if request.user.group == "SimpleUsers" else "global"
    selection = check_mail_id(request, selection)
    connector = get_connector()
    saclient = SpamassassinClient(request.user, recipient_db)
    for item in selection:
        rcpt, mail_id = item.split()
        content = "".join(
            [msg.mail_text for msg in connector.get_mail_content(mail_id)]
        )
        result = (
            saclient.learn_spam(rcpt, content)
            if mtype == "spam"
            else saclient.learn_ham(rcpt, content)
        )
        if not result:
            break
        connector.set_msgrcpt_status(rcpt, mail_id, mtype[0].upper())
    if saclient.error is None:
        saclient.done()
        message = ungettext(
            "%(count)d message processed successfully",
            "%(count)d messages processed successfully",
            len(selection),
        ) % {"count": len(selection)}
    else:
        message = saclient.error
    status = 400 if saclient.error else 200
    return render_to_json_response({"message": message, "reload": True}, status=status)


# 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 mark_messages(request, selection, mtype, recipient_db=None):
    """Mark a selection of messages as spam.

    :param str selection: message unique identifier
    :param str mtype: type of marking (spam or ham)
    """
    if not manual_learning_enabled(request.user):
        return render_to_json_response({"status": "ok"})
    if recipient_db is None:
        recipient_db = "user" if request.user.group == "SimpleUsers" else "global"
    selection = check_mail_id(request, selection)
    connector = get_connector()
    saclient = SpamassassinClient(request.user, recipient_db)
    for item in selection:
        rcpt, mail_id = item.split()
        content = connector.get_mail_content(mail_id)
        result = (
            saclient.learn_spam(rcpt, content)
            if mtype == "spam"
            else saclient.learn_ham(rcpt, content)
        )
        if not result:
            break
        connector.set_msgrcpt_status(rcpt, mail_id, mtype[0].upper())
    if saclient.error is None:
        saclient.done()
        message = ungettext(
            "%(count)d message processed successfully",
            "%(count)d messages processed successfully",
            len(selection),
        ) % {"count": len(selection)}
    else:
        message = saclient.error
    status = 400 if saclient.error else 200
    return render_to_json_response({"message": message, "reload": True}, status=status)