1
0
mirror of https://github.com/django/django.git synced 2025-10-26 15:16:09 +00:00

[1.7.x] Fixed #23063 -- Convert \n and \r to \r\n when using the SMTP backend as per RFC.

Backport of 8d789449c7 from master.
This commit is contained in:
Florian Apolloner
2014-10-09 14:18:31 +02:00
parent d2a2af67f7
commit bc13a08f89
4 changed files with 45 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ import smtpd
import sys
import tempfile
import threading
from smtplib import SMTPException
from smtplib import SMTPException, SMTP
from ssl import SSLError
from django.core import mail
@@ -984,3 +984,37 @@ class SMTPBackendTests(BaseEmailBackendTests, SimpleTestCase):
self.assertEqual(myemailbackend.timeout, 42)
self.assertEqual(myemailbackend.connection.timeout, 42)
myemailbackend.close()
def test_email_msg_uses_crlf(self):
"""#23063 -- Test that RFC-compliant messages are sent over SMTP."""
send = SMTP.send
try:
smtp_messages = []
def mock_send(self, s):
smtp_messages.append(s)
return send(self, s)
SMTP.send = mock_send
email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'])
mail.get_connection().send_messages([email])
# Find the actual message
msg = None
for i, m in enumerate(smtp_messages):
if m[:4] == 'data':
msg = smtp_messages[i+1]
break
self.assertTrue(msg)
if PY3:
msg = msg.decode('utf-8')
# Ensure that the message only contains CRLF and not combinations of CRLF, LF, and CR.
msg = msg.replace('\r\n', '')
self.assertNotIn('\r', msg)
self.assertNotIn('\n', msg)
finally:
SMTP.send = send