Opened 4 years ago

Closed 4 years ago

#20371 closed Uncategorized (invalid)

mail_admins should support strings as arguments

Reported by: anonymous Owned by: nobody
Component: Core (Mail) Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


As the topic says, mail_admins should support str-types as arguments, not only unicode data. The following example triggers an exception which is somewhat critical when used in combination with logging and the AdminEmailHandler:

from django.core.mail import mail_admins
mail.mail_admins(subject, "test")

>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

Let me explain what the problem is here: A lot of people rely on the AdminEmailHandler which uses mail_admins. Now whenever an exception is raised it is a pretty common use-case to log the exception, like this:

    bar = my_model.do_something()
except Exception, e:
    logger.critical("do_something failed: %s" %e)
    return False
    return bar == 0

When the exception message contains unicode data, it will be converted to str. This will cause mail_admins to fail if non-ascii characaters were present.

Change History (3)

comment:1 Changed 4 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

You're only getting a unicode decode error here because you've put unicode content into a byte string.

If you use:

subject = u'Über"

i.e., you correctly identify the string as unicode, you won't get an error. mail_admin() handles unicode content just fine. What you've described here is a simple case of Garbage In, Garbage Out.

comment:2 in reply to:  1 Changed 4 years ago by anonymous

Resolution: invalid
Status: closednew

Sorry, maybe the title of this bug is a bit misleading, it should probably be "AdminEmailHandler should not raise exception if non-unicode data is logged".

The problem here is that mail_admins is called by the logging handler (django.utils.log.AdminEmailHandler), and logging handlers should not raise exceptions (they should log them), even if garbage is passed to them.

Replying to russellm:

You're only getting a unicode decode error here because you've put unicode content into a byte string.

No, subject is a valid UTF-8 string:

In [1]: subject="Über"
In [2]: print subject
------> print(subject)
In [3]: type(subject)
Out[3]: <type 'str'>
In [4]: u_subject=subject.decode("utf-8")
In [5]: type(u_subject)
Out[5]: <type 'unicode'>
In [6]: print u_subject
------> print(u_subject)

BTW, passing u_subject to mail_admins does work!

This could be fixed in two places:

  • mail_admins
  • AdminEmailHandler (probably the right place to fix this, sorry for initial fuzziness about mail_admins)

comment:3 Changed 4 years ago by code22

Resolution: invalid
Status: newclosed

This would lead to catching all exceptions that you get in the code and that is unacceptable.

Note: See TracTickets for help on using tickets.
Back to Top