From 665ec600a40ffc3c9baea30be9c8765939512748 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sun, 15 Jan 2012 02:33:31 +0000 Subject: [PATCH] Made email attachment handling code accept non-ASCII filenames. Thanks to Anton Chaporgin for the report and to Claude Paroz for the patch. Fixes #14964. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17375 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/mail/message.py | 4 ++++ docs/ref/unicode.txt | 4 +++- tests/regressiontests/mail/tests.py | 13 +++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 39f7afc61b..7347391f0b 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -311,6 +311,10 @@ class EmailMessage(object): mimetype = DEFAULT_ATTACHMENT_MIME_TYPE attachment = self._create_mime_attachment(content, mimetype) if filename: + try: + filename = filename.encode('ascii') + except UnicodeEncodeError: + filename = ('utf-8', '', filename.encode('utf-8')) attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment diff --git a/docs/ref/unicode.txt b/docs/ref/unicode.txt index 08f738c879..1286dcfdd0 100644 --- a/docs/ref/unicode.txt +++ b/docs/ref/unicode.txt @@ -322,7 +322,9 @@ can be non-ASCII:: sender = u'Arnbjörg Ráðormsdóttir ' recipients = ['Fred