Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lms/djangoapps/bulk_email/tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"""
Unit tests for sending course email
"""
from email import charset as Charset
from mock import patch

from django.conf import settings
from django.core import mail
from django.core.urlresolvers import reverse
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings

from courseware.tests.tests import TEST_DATA_MONGO_MODULESTORE
Expand Down Expand Up @@ -279,3 +281,20 @@ def test_chunked_queries_send_numerous_emails(self, email_mock):
[s.email for s in self.students] +
[s.email for s in added_users if s not in optouts])
self.assertItemsEqual(outbox_contents, should_send_contents)


class TestEmailEncoding(TestCase):
"""
Test Content-Transfer encoding for emails.
"""

def test_email_encoding(self):
"""
Verify that the Content-Transfer encoding for utf-8 messages is base64.

Django (version <= 1.6) globally overrides python's default Content-Transfer-Encoding for UTF-8 messages from
base64 to 7bit. We set it to base64 again otherwise mail servers add newlines and exclamation marks when a line
is longer than 989 characters.
"""
__, body_enc, __ = Charset.CHARSETS['utf-8']
self.assertEqual(body_enc, Charset.BASE64)
13 changes: 13 additions & 0 deletions lms/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def run():
"""
autostartup()

reset_email_content_transfer_encoding()

if settings.FEATURES.get('USE_CUSTOM_THEME', False):
enable_theme()

Expand All @@ -30,6 +32,17 @@ def run():
enable_third_party_auth()


def reset_email_content_transfer_encoding():
"""
Django (version <= 1.6) globally overrides python's default Content-Transfer-Encoding for UTF-8 messages from base64
to 7bit. We set it to base64 again otherwise mail servers add newlines and exclamation marks when a line is longer
than 989 characters.
"""
from email import charset as Charset
header_enc, body_enc, output_charset = Charset.CHARSETS['utf-8']
Charset.add_charset('utf-8', header_enc, Charset.BASE64, output_charset)


def enable_theme():
"""
Enable the settings for a custom theme, whose files should be stored
Expand Down