SmileyChris / django-mailer-2

mail queuing and management for the Django web framework
MIT License
69 stars 33 forks source link

BadHeaderError when using logging errors via django.utils.log.AdminEmailHandler #22

Open funvit opened 11 years ago

funvit commented 11 years ago

settings.py:

EMAIL_BACKEND = 'django_mailer.smtp_queue.EmailBackend'

in code somewere:

except Exception, e:
    logger.error('discount_cards.views.register -> new_user.save -> %s' % e.message)

raise BadHeaderError: Header values can't contain newlines

truncated traceback:

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/utils/log.py", line 101, in emit
  html_message=html_message)

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/core/mail/__init__.py", line 96, in mail_admins
  mail.send(fail_silently=fail_silently)

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/core/mail/message.py", line 251, in send
  return self.get_connection(fail_silently).send_messages([self])

File "/u01/funvit/nginx/env/src/django-mailer/django_mailer/smtp_queue.py", line 31, in send_messages
  queue_email_message(email_message)

File "/u01/funvit/nginx/env/src/django-mailer/django_mailer/__init__.py", line 122, in queue_email_message
  encoded_message=email_message.message().as_string())

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/core/mail/message.py", line 219, in message
  msg['Subject'] = self.subject

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/core/mail/message.py", line 127, in __setitem__
  name, val = forbid_multi_line_headers(name, val, self.encoding)

File "/u01/funvit/nginx/env/lib/python2.6/site-packages/django/core/mail/message.py", line 87, in forbid_multi_line_headers
  raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))

BadHeaderError: Header values can't contain newlines (got u'[Django] ERROR: discount_cards.views.register -> new_user.save -> value too long for type character varying(30)\n' for header 'Subject')

im currently testing dirty patch via adding at start of _def queue_emailmessage

# file: __init__.py

email_message.subject = email_message.subject.replace(u'\n', u' ')
SmileyChris commented 11 years ago

This sounds like an old Django issue. At least in 1.4, the AdminEmailHandler ensures the subject is devoid of new lines and isn't longer than 989 chars.

funvit commented 11 years ago

yes, Django 1.3