Skip to content

Commit 076cb0a

Browse files
Merge pull request #407 from TeamMsgExtractor/next-release
Version 0.48.2
2 parents 235a27c + 20f2f76 commit 076cb0a

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
**v0.48.2**
2+
* Fixed bugs with `MessageBase.asEmailMessage()`. Numerous improvements to how it handles the data.
3+
14
**v0.48.1**
25
* Added an option (`-s`, `--stdin`) to the command line to take an MSG file from stdin. This allows the user to pipe the MSG data from another program directly instead of having to write a middleman that uses the `extract-msg` library directly or having to write the file to the disk first.
36
* Changed main function to allow for manual argument list to be passed to it.

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ your access to the newest major version of extract-msg.
260260
.. |License: GPL v3| image:: https://img.shields.io/badge/License-GPLv3-blue.svg
261261
:target: LICENSE.txt
262262

263-
.. |PyPI3| image:: https://img.shields.io/badge/pypi-0.48.1-blue.svg
264-
:target: https://pypi.org/project/extract-msg/0.48.1/
263+
.. |PyPI3| image:: https://img.shields.io/badge/pypi-0.48.2-blue.svg
264+
:target: https://pypi.org/project/extract-msg/0.48.2/
265265

266266
.. |PyPI2| image:: https://img.shields.io/badge/python-3.8+-brightgreen.svg
267267
:target: https://www.python.org/downloads/release/python-3810/

extract_msg/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2828

2929
__author__ = 'Destiny Peterson & Matthew Walker'
30-
__date__ = '2024-03-08'
31-
__version__ = '0.48.1'
30+
__date__ = '2024-03-09'
31+
__version__ = '0.48.2'
3232

3333
__all__ = [
3434
# Modules:

extract_msg/msg_classes/message_base.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import RTFDE.exceptions
2525

2626
from email import policy
27+
from email.charset import Charset, QP
2728
from email.message import EmailMessage
29+
from email.mime.multipart import MIMEMultipart
30+
from email.mime.text import MIMEText
2831
from email.parser import HeaderParser
2932
from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Type, Union
3033

@@ -151,13 +154,24 @@ def asEmailMessage(self) -> EmailMessage:
151154

152155
# Copy the headers.
153156
for key, value in self.header.items():
154-
ret[key] = value
157+
if key.lower() != 'content-type':
158+
ret[key] = value.replace('\r\n', '').replace('\n', '')
159+
160+
ret['Content-Type'] = 'multipart/mixed'
155161

156162
# Attach the body to the EmailMessage instance.
163+
msgMain = MIMEMultipart('related')
164+
ret.attach(msgMain)
165+
bodyParts = MIMEMultipart('alternative')
166+
msgMain.attach(bodyParts)
167+
168+
c = Charset('utf-8')
169+
c.body_encoding = QP
170+
171+
if self.body:
172+
bodyParts.attach(MIMEText(self.body, 'plain', c))
157173
if self.htmlBody:
158-
ret.set_content(self.body, subtype = 'html', cte = 'quoted-printable')
159-
elif self.body:
160-
ret.set_content(self.body, cte = 'quoted-printable')
174+
bodyParts.attach(MIMEText(self.htmlBody.decode('utf-8'), 'html', c))
161175

162176
# Process attachments.
163177
for att in self.attachments:
@@ -167,7 +181,7 @@ def asEmailMessage(self) -> EmailMessage:
167181
filename = att.getFilename()
168182
if filename.lower().endswith('.msg'):
169183
filename = filename[:-4] + '.eml'
170-
ret.add_attachment(
184+
msgMain.add_attachment(
171185
att.data.asEmailMessage(),
172186
filename = filename,
173187
cid = att.contentId)
@@ -183,11 +197,17 @@ def asEmailMessage(self) -> EmailMessage:
183197
raise ConversionError(f'Could not find a suitable method to attach attachment data type "{att.dataType}".')
184198
mime = att.mimetype or 'application/octet-stream'
185199
mainType, subType = mime.split('/')[0], mime.split('/')[-1]
186-
ret.add_attachment(data,
187-
maintype = mainType,
188-
subtype = subType,
189-
filename = att.getFilename(),
190-
cid = att.contentId)
200+
# Need to do this manually instead of using add_attachment.
201+
attachment = EmailMessage()
202+
attachment.set_content(data,
203+
maintype = mainType,
204+
subtype = subType,
205+
cid = att.contentId)
206+
# This is just a very basic check.
207+
attachment['Content-Disposition'] = f'{"inline" if att.hidden else "attachment"}; filename="{att.getFilename()}"'
208+
209+
# Add the attachment.
210+
msgMain.attach(attachment)
191211

192212
return ret
193213

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bdist_wheel]
2-
universal=1
2+
python-tag=py3
33

44
[options.extras_require]
55
all =

0 commit comments

Comments
 (0)