Email line length is limited to 998 chars by the RFC 5322 Section 2.1.1. A message body containing longer lines is converted to quoted-printable or base64 (whichever is shorter), to avoid having to insert newlines in a way that alters the intended text.
(self)
| 1312 | self.assertIn(b"Content-Transfer-Encoding: quoted-printable", s) |
| 1313 | |
| 1314 | def test_long_lines(self): |
| 1315 | """ |
| 1316 | Email line length is limited to 998 chars by the RFC 5322 Section |
| 1317 | 2.1.1. A message body containing longer lines is converted to |
| 1318 | quoted-printable or base64 (whichever is shorter), to avoid having to |
| 1319 | insert newlines in a way that alters the intended text. |
| 1320 | """ |
| 1321 | cases = [ |
| 1322 | # (body, expected_cte) |
| 1323 | ("В южных морях " * 60, "base64"), |
| 1324 | ("I de sørlige hav " * 58, "quoted-printable"), |
| 1325 | ] |
| 1326 | for body, expected_cte in cases: |
| 1327 | mail.outbox = [] |
| 1328 | with self.subTest(body=f"{body[:10]}…", expected_cte=expected_cte): |
| 1329 | # Test precondition: Body is a single line < 998 characters, |
| 1330 | # but utf-8 encoding of body is > 998 octets (forcing a CTE |
| 1331 | # that avoids inserting newlines). |
| 1332 | self.assertLess(len(body), 998) |
| 1333 | self.assertGreater(len(body.encode()), 998) |
| 1334 | |
| 1335 | email = EmailMessage(body=body, to=["to@example.com"]) |
| 1336 | email.send() |
| 1337 | message = self.get_outbox_message() |
| 1338 | self.assertMessageHasHeaders( |
| 1339 | message, |
| 1340 | { |
| 1341 | ("MIME-Version", "1.0"), |
| 1342 | ("Content-Type", 'text/plain; charset="utf-8"'), |
| 1343 | ("Content-Transfer-Encoding", expected_cte), |
| 1344 | }, |
| 1345 | ) |
| 1346 | |
| 1347 | def test_address_header_handling(self): |
| 1348 | # This verifies the modern email API's address header handling. |
nothing calls this directly
no test coverage detected