|
39 | 39 | ) |
40 | 40 | from ..exceptions import ( |
41 | 41 | ConversionError, DataNotFoundError, DeencapMalformedData, |
42 | | - DeencapNotEncapsulated, IncompatibleOptionsError, WKError |
| 42 | + DeencapNotEncapsulated, IncompatibleOptionsError, MimetypeFailureError, |
| 43 | + WKError |
43 | 44 | ) |
44 | 45 | from .msg import MSGFile |
45 | 46 | from ..structures.report_tag import ReportTag |
@@ -178,7 +179,7 @@ def asEmailMessage(self) -> EmailMessage: |
178 | 179 | if att.dataType: |
179 | 180 | if hasattr(att.dataType, 'asEmailMessage'): |
180 | 181 | # Replace the extension with '.eml'. |
181 | | - filename = att.getFilename() |
| 182 | + filename = att.name or '' |
182 | 183 | if filename.lower().endswith('.msg'): |
183 | 184 | filename = filename[:-4] + '.eml' |
184 | 185 | msgMain.add_attachment( |
@@ -1198,12 +1199,35 @@ def htmlBodyPrepared(self) -> Optional[bytes]: |
1198 | 1199 | for tag in tags: |
1199 | 1200 | # Iterate through the attachments until we get the right one. |
1200 | 1201 | cid = tag['src'][4:] |
1201 | | - data = next((attachment.data for attachment in self.attachments if attachment.cid == cid), None) |
| 1202 | + att = next((attachment for attachment in self.attachments if hasattr(attachment, 'cid') and attachment.cid == cid), None) |
1202 | 1203 | # If we found anything, inject it. |
1203 | | - if data: |
1204 | | - tag['src'] = (b'data:image;base64,' + base64.b64encode(data)).decode('utf-8') |
| 1204 | + if att and isinstance(att.data, bytes): |
| 1205 | + # Try to get the mimetype. If we can't, see if the item has an |
| 1206 | + # extension and guess the mimtype for a few known ones. |
| 1207 | + mime = att.mimetype |
| 1208 | + if not mime: |
| 1209 | + ext = (att.name or '').split('.')[-1].lower() |
| 1210 | + if ext == 'png': |
| 1211 | + mime = 'image/png' |
| 1212 | + elif ext == 'jpg' or ext == 'jpeg': |
| 1213 | + mime = 'image/jpeg' |
| 1214 | + elif ext == 'gif': |
| 1215 | + mime = 'image/gif' |
| 1216 | + elif ext == 'tiff' or ext == 'tif': |
| 1217 | + mime = 'image/tif' |
| 1218 | + elif ext == 'bmp': |
| 1219 | + mime = 'image/bmp' |
| 1220 | + elif ext == 'svg': |
| 1221 | + mime = 'image/svg+xml' |
| 1222 | + # Final check. |
| 1223 | + if mime: |
| 1224 | + tag['src'] = (b'data:' + mime.encode() + b';base64,' + base64.b64encode(att.data)).decode('utf-8') |
| 1225 | + else: |
| 1226 | + # We don't know what to actually put for this item, and we |
| 1227 | + # really should never end up here, so throw an error. |
| 1228 | + raise MimetypeFailureError('Could not get the mimetype to use for htmlBodyPrepared.') |
1205 | 1229 |
|
1206 | | - return soup.prettify('utf-8') |
| 1230 | + return soup.encode('utf-8') |
1207 | 1231 |
|
1208 | 1232 | @functools.cached_property |
1209 | 1233 | def htmlInjectableHeader(self) -> str: |
|
0 commit comments