|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | from datetime import date, datetime |
| 4 | +try: |
| 5 | + from email import message_from_bytes |
| 6 | +except ImportError: |
| 7 | + from email import message_from_string |
| 8 | + |
| 9 | + def message_from_bytes(s): |
| 10 | + return message_from_string(s.decode('utf-8')) |
| 11 | + |
4 | 12 | from email.mime.base import MIMEBase |
5 | 13 | from email.mime.image import MIMEImage |
6 | 14 |
|
|
14 | 22 | from anymail.message import attach_inline_image_file |
15 | 23 |
|
16 | 24 | from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin |
17 | | -from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin |
| 25 | +from .utils import sample_image_content, sample_image_path, SAMPLE_FORWARDED_EMAIL, SAMPLE_IMAGE_FILENAME, AnymailTestMixin |
18 | 26 |
|
19 | 27 |
|
20 | 28 | @override_settings(EMAIL_BACKEND='anymail.backends.mailgun.EmailBackend', |
@@ -131,13 +139,27 @@ def test_attachments(self): |
131 | 139 | mimeattachment.set_payload(pdf_content) |
132 | 140 | self.message.attach(mimeattachment) |
133 | 141 |
|
| 142 | + # And also with an message/rfc822 attachment |
| 143 | + forwarded_email = message_from_bytes(SAMPLE_FORWARDED_EMAIL) |
| 144 | + rfcmessage = MIMEBase("message", "rfc822") |
| 145 | + rfcmessage.attach(forwarded_email) |
| 146 | + self.message.attach(rfcmessage) |
| 147 | + |
134 | 148 | self.message.send() |
135 | 149 | files = self.get_api_call_files() |
136 | 150 | attachments = [value for (field, value) in files if field == 'attachment'] |
137 | | - self.assertEqual(len(attachments), 3) |
| 151 | + self.assertEqual(len(attachments), 4) |
138 | 152 | self.assertEqual(attachments[0], ('test.txt', text_content, 'text/plain')) |
139 | 153 | self.assertEqual(attachments[1], ('test.png', png_content, 'image/png')) # type inferred from filename |
140 | 154 | self.assertEqual(attachments[2], (None, pdf_content, 'application/pdf')) # no filename |
| 155 | + # Email messages can get a bit changed with respect to |
| 156 | + # whitespace characters in headers, without breaking the message, so we |
| 157 | + # tolerate that: |
| 158 | + self.assertEqual(attachments[3][0], None) |
| 159 | + self.assertEqual(attachments[3][1].replace(b'\n', b'').replace(b' ', b''), |
| 160 | + (b'Content-Type: message/rfc822\nMIME-Version: 1.0\n\n' + SAMPLE_FORWARDED_EMAIL).replace(b'\n', b'').replace(b' ', b'')) |
| 161 | + self.assertEqual(attachments[3][2], 'message/rfc822') |
| 162 | + |
141 | 163 | # Make sure the image attachment is not treated as embedded: |
142 | 164 | inlines = [value for (field, value) in files if field == 'inline'] |
143 | 165 | self.assertEqual(len(inlines), 0) |
|
0 commit comments