Ticket #14964: 14964-2.diff

File 14964-2.diff, 1.8 KB (added by Claude Paroz, 13 years ago)

Handle non ASCII filenames v2 + test

  • django/core/mail/message.py

    diff --git a/django/core/mail/message.py b/django/core/mail/message.py
    index 39f7afc..7347391 100644
    a b class EmailMessage(object):  
    311311                mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
    312312        attachment = self._create_mime_attachment(content, mimetype)
    313313        if filename:
     314            try:
     315                filename = filename.encode('ascii')
     316            except UnicodeEncodeError:
     317                filename = ('utf-8', '', filename.encode('utf-8'))
    314318            attachment.add_header('Content-Disposition', 'attachment',
    315319                                  filename=filename)
    316320        return attachment
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index 52e251f..ed44cb9 100644
    a b class MailTests(TestCase):  
    189189        msg = EmailMultiAlternatives(subject, text_content, from_email, [to], headers=headers)
    190190        msg.attach_alternative(html_content, "text/html")
    191191        msg.attach("an attachment.pdf", "%PDF-1.4.%...", mimetype="application/pdf")
     192        # Test also with unicode in file name
     193        msg.attach(u"une pièce jointe.pdf", "%PDF-1.4.%...", mimetype="application/pdf")
    192194        msg_str = msg.message().as_string()
    193195        message = email.message_from_string(msg_str)
    194196        self.assertTrue(message.is_multipart())
    class MailTests(TestCase):  
    197199        payload = message.get_payload()
    198200        self.assertEqual(payload[0].get_content_type(), 'multipart/alternative')
    199201        self.assertEqual(payload[1].get_content_type(), 'application/pdf')
     202        self.assertEqual(payload[2].get_filename(), u'une pièce jointe.pdf')
    200203
    201204    def test_dummy_backend(self):
    202205        """
Back to Top