Opened 9 years ago

Closed 9 years ago

#26216 closed Bug (invalid)

UnicodeDecodeError during _create_attachment

Reported by: thomasysliu Owned by: nobody
Component: Core (Mail) Version: 1.9
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

Description

UnicodeDecodeError raised when the file name of the attachment have non 'ascii' characters.

  • Simple Code

    Code highlighting:

    # -*- coding: utf-8 -*-
    from django.core.mail import send_mail
    from django.core.mail import EmailMessage
    import os
    from django.core.management.base import BaseCommand, CommandError
    
    def test():
            filename = u'\u96fb\u5b50\u5b782.ics'
            email = EmailMessage("subject", "body", to=["xxx@this.is.an.email.addr"])
            email.attach(filename.encode('utf-8'), "TEST")
            email.send()
    
    
    class Command(BaseCommand):
            args = ''
            help = 'Send mail'
            def handle(self, *args, **options):
                    test()
    
    
    
  • Traceback
    /tmp/test_email$ python2.7 manage.py test_sendemail
    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
        utility.execute()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
        output = self.handle(*args, **options)
      File "/private/tmp/test_email/test_email/management/commands/test_sendemail.py", line 18, in handle
        test()
      File "/private/tmp/test_email/test_email/management/commands/test_sendemail.py", line 11, in test
        email.send()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
        return self.get_connection(fail_silently).send_messages([self])
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages
        sent = self._send(message)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 121, in _send
        message = email_message.message()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/message.py", line 256, in message
        msg = self._create_message(msg)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/message.py", line 344, in _create_message
        return self._create_attachments(msg)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/message.py", line 357, in _create_attachments
        msg.attach(self._create_attachment(*attachment))
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/mail/message.py", line 402, in _create_attachment
        filename.encode('ascii')
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
    

Change History (2)

comment:1 by Tim Graham, 9 years ago

Looks like if you omit the .encode('utf-8') before passing the filename to attach(), Django will take care of that for you. Did you try it?

in reply to:  1 comment:2 by thomasysliu, 9 years ago

Resolution: invalid
Status: newclosed

Replying to timgraham:

Looks like if you omit the .encode('utf-8') before passing the filename to attach(), Django will take care of that for you. Did you try it?

Yes. Django will no that for me.

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