|
4 | 4 | """A parser of RFC 2822 and MIME email messages. |
5 | 5 | """ |
6 | 6 |
|
7 | | -import re |
8 | 7 | from cStringIO import StringIO |
9 | 8 |
|
10 | 9 | # Intrapackage imports |
11 | 10 | import Errors |
12 | 11 | import Message |
13 | 12 |
|
14 | | -bcre = re.compile('boundary="?([^"]+)"?', re.IGNORECASE) |
15 | 13 | EMPTYSTRING = '' |
16 | 14 | NL = '\n' |
17 | 15 |
|
@@ -92,13 +90,8 @@ def _parseheaders(self, container, fp): |
92 | 90 | def _parsebody(self, container, fp): |
93 | 91 | # Parse the body, but first split the payload on the content-type |
94 | 92 | # boundary if present. |
95 | | - boundary = isdigest = None |
96 | | - ctype = container['content-type'] |
97 | | - if ctype: |
98 | | - mo = bcre.search(ctype) |
99 | | - if mo: |
100 | | - boundary = mo.group(1) |
101 | | - isdigest = container.get_type() == 'multipart/digest' |
| 93 | + boundary = container.get_boundary() |
| 94 | + isdigest = (container.get_type() == 'multipart/digest') |
102 | 95 | # If there's a boundary, split the payload text into its constituent |
103 | 96 | # parts and parse each separately. Otherwise, just parse the rest of |
104 | 97 | # the body as a single message. Note: any exceptions raised in the |
@@ -141,7 +134,20 @@ def _parsebody(self, container, fp): |
141 | 134 | container.preamble = preamble |
142 | 135 | container.epilogue = epilogue |
143 | 136 | container.add_payload(msgobj) |
144 | | - elif ctype == 'message/rfc822': |
| 137 | + elif container.get_type() == 'message/delivery-status': |
| 138 | + # This special kind of type contains blocks of headers separated |
| 139 | + # by a blank line. We'll represent each header block as a |
| 140 | + # separate Message object |
| 141 | + blocks = [] |
| 142 | + while 1: |
| 143 | + blockmsg = self._class() |
| 144 | + self._parseheaders(blockmsg, fp) |
| 145 | + if not len(blockmsg): |
| 146 | + # No more header blocks left |
| 147 | + break |
| 148 | + blocks.append(blockmsg) |
| 149 | + container.set_payload(blocks) |
| 150 | + elif container.get_main_type() == 'message': |
145 | 151 | # Create a container for the payload, but watch out for there not |
146 | 152 | # being any headers left |
147 | 153 | try: |
|
0 commit comments