modoboa/modoboa-postfix-autoreply

Weird emails can break autoreply

gawel opened this issue · 1 comments

gawel commented

We got this:

Traceback (most recent call last):
  File "/var/www/modoboa/bearboa/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/modoboa_postfix_autoreply/management/commands/autoreply.py", line 169, in handle
    send_autoreply(sender, mbox, armessage, original_msg)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/modoboa_postfix_autoreply/management/commands/autoreply.py", line 78, in send_autoreply
    msg.send()
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 342, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages
    sent = self._send(message)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 121, in _send
    message = email_message.message()
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 307, in message
    msg['Subject'] = self.subject
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 232, in __setitem__
    name, val = forbid_multi_line_headers(name, val, self.encoding)
  File "/var/www/modoboa/vtenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 92, in forbid_multi_line_headers
    raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
django.core.mail.message.BadHeaderError: Header values can't contain newlines (got u"Auto: Votre inscription \xe0 xxx Re: =?UTF-8?Q?Session_Publique_=3A_Un_point_sur_la_notion_de_communa?=\n\t=?UTF-8?Q?ut=C3=A9=2C_10_ans_apr=C3=A8s_le_web_social?=" for header u'Subject')

I don't really understand how a \n is in the original subject but that's a fact. It happens.

I think we should try to decode the header and check if the subject is valid and if not, don't include it in the reply's subject.

Thoughts ?

gawel commented

POC:

import email
from email.header import decode_header
from StringIO import StringIO

content = StringIO('''From: a@a.com
To: a@a.com
Subject: =?UTF-8?Q?Session_Publique_=3A_Un_point_sur_la_notion_de_communa?=\n\t=?UTF-8?Q?ut=C3=A9=2C_10_ans_apr=C3=A8s_le_web_social?=


bla
''')
content.seek(0)


def get_subject(original_msg):
    subject = u''
    for s, charset in decode_header(original_msg['Subject']):
        subject += s.decode(charset)
    return u' '.join(subject.split())


original_msg = email.message_from_file(content)
print(repr(get_subject(original_msg)))

$ python2 test_decode.py
u'Session Publique : Un point sur la notion de communaut\xe9, 10 ans apr\xe8s le web social'