Skip to content

Commit

Permalink
Merge pull request #1026 from kamalvk18/get_mime_for_email_type_attac…
Browse files Browse the repository at this point in the history
…hments

To solve issue #1025 (Being able to read mime content of .eml or .msg type attachments)
  • Loading branch information
alejcas committed Nov 28, 2023
2 parents 464c3e1 + 8833675 commit 31a914e
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions O365/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def save_as_eml(self, attachment, to_path=None):
:param MessageAttachment attachment: the MessageAttachment to store as eml.
:param Path or str to_path: the path where to store this file
"""
if not attachment or not isinstance(attachment, MessageAttachment) \
or attachment.attachment_id is None or attachment.attachment_type != 'item':
raise ValueError('Must provide a saved "item" attachment of type MessageAttachment')
mime_content = self.get_mime_content(attachment)
if not mime_content:
return False

if to_path is None:
to_path = Path('message_eml.eml')
Expand All @@ -71,6 +71,16 @@ def save_as_eml(self, attachment, to_path=None):
if not to_path.suffix:
to_path = to_path.with_suffix('.eml')

with to_path.open('wb') as file_obj:
file_obj.write(mime_content)
return True

def get_mime_content(self, attachment):
""" Returns the MIME contents of this attachment """
if not attachment or not isinstance(attachment, MessageAttachment) \
or attachment.attachment_id is None or attachment.attachment_type != 'item':
raise ValueError('Must provide a saved "item" attachment of type MessageAttachment')

msg_id = self._parent.object_id
if msg_id is None:
raise RuntimeError('Attempting to get the mime contents of an unsaved message')
Expand All @@ -80,15 +90,10 @@ def save_as_eml(self, attachment, to_path=None):
response = self._parent.con.get(url)

if not response:
return False
return None

mime_content = response.content
return response.content

if mime_content:
with to_path.open('wb') as file_obj:
file_obj.write(mime_content)
return True
return False


class MessageFlag(ApiComponent):
Expand Down

0 comments on commit 31a914e

Please sign in to comment.