5
5
# noinspection PyPep8Naming
6
6
from bs4 import BeautifulSoup as bs
7
7
from dateutil .parser import parse
8
+ from pathlib import Path
8
9
9
10
from .utils import OutlookWellKnowFolderNames , ApiComponent , \
10
11
BaseAttachments , BaseAttachment , AttachableMixin , ImportanceLevel , \
@@ -31,17 +32,55 @@ class Flag(CaseEnum):
31
32
class MessageAttachment (BaseAttachment ):
32
33
_endpoints = {
33
34
'attach' : '/messages/{id}/attachments' ,
34
- 'attachment' : '/messages/{id}/attachments/{ida}'
35
+ 'attachment' : '/messages/{id}/attachments/{ida}' ,
35
36
}
36
37
37
38
38
39
class MessageAttachments (BaseAttachments ):
39
40
_endpoints = {
40
41
'attachments' : '/messages/{id}/attachments' ,
41
- 'attachment' : '/messages/{id}/attachments/{ida}'
42
+ 'attachment' : '/messages/{id}/attachments/{ida}' ,
43
+ 'get_mime' : '/messages/{id}/attachments/{ida}/$value' ,
42
44
}
43
45
_attachment_constructor = MessageAttachment
44
46
47
+ def save_as_eml (self , attachment , to_path = None ):
48
+ """ Saves this message as and EML to the file system
49
+ :param MessageAttachment attachment: the MessageAttachment to store as eml.
50
+ :param Path or str to_path: the path where to store this file
51
+ """
52
+ if not attachment or not isinstance (attachment , MessageAttachment ) \
53
+ or attachment .attachment_id is None or attachment .attachment_type != 'item' :
54
+ raise ValueError ('Must provide a saved "item" attachment of type MessageAttachment' )
55
+
56
+ if to_path is None :
57
+ to_path = Path ()
58
+ else :
59
+ if not isinstance (to_path , Path ):
60
+ to_path = Path (to_path )
61
+
62
+ if not to_path .suffix :
63
+ to_path = to_path .with_suffix ('.eml' )
64
+
65
+ msg_id = self ._parent .object_id
66
+ if msg_id is None :
67
+ raise RuntimeError ('Attempting to get the mime contents of an unsaved message' )
68
+
69
+ url = self .build_url (self ._endpoints .get ('get_mime' ).format (id = msg_id , ida = attachment .attachment_id ))
70
+
71
+ response = self ._parent .con .get (url )
72
+
73
+ if not response :
74
+ return False
75
+
76
+ mime_content = response .content
77
+
78
+ if mime_content :
79
+ with to_path .open ('wb' ) as file_obj :
80
+ file_obj .write (mime_content )
81
+ return True
82
+ return False
83
+
45
84
46
85
class MessageFlag (ApiComponent ):
47
86
""" A flag on a message """
@@ -172,7 +211,8 @@ class Message(ApiComponent, AttachableMixin, HandleRecipientsMixin):
172
211
'copy_message' : '/messages/{id}/copy' ,
173
212
'create_reply' : '/messages/{id}/createReply' ,
174
213
'create_reply_all' : '/messages/{id}/createReplyAll' ,
175
- 'forward_message' : '/messages/{id}/createForward'
214
+ 'forward_message' : '/messages/{id}/createForward' ,
215
+ 'get_mime' : '/messages/{id}/$value' ,
176
216
}
177
217
178
218
def __init__ (self , * , parent = None , con = None , ** kwargs ):
@@ -961,3 +1001,39 @@ def get_event(self):
961
1001
event_data = data .get (self ._cc ('event' ))
962
1002
963
1003
return Event (parent = self , ** {self ._cloud_data_key : event_data })
1004
+
1005
+ def get_mime_content (self ):
1006
+ """ Returns the MIME contents of this message """
1007
+ if self .object_id is None :
1008
+ raise RuntimeError ('Attempting to get the mime contents of an unsaved message' )
1009
+
1010
+ url = self .build_url (self ._endpoints .get ('get_mime' ).format (id = self .object_id ))
1011
+
1012
+ response = self .con .get (url )
1013
+
1014
+ if not response :
1015
+ return None
1016
+
1017
+ return response .content
1018
+
1019
+ def save_as_eml (self , to_path = None ):
1020
+ """ Saves this message as and EML to the file system
1021
+ :param Path or str to_path: the path where to store this file
1022
+ """
1023
+
1024
+ if to_path is None :
1025
+ to_path = Path ()
1026
+ else :
1027
+ if not isinstance (to_path , Path ):
1028
+ to_path = Path (to_path )
1029
+
1030
+ if not to_path .suffix :
1031
+ to_path = to_path .with_suffix ('.eml' )
1032
+
1033
+ mime_content = self .get_mime_content ()
1034
+
1035
+ if mime_content :
1036
+ with to_path .open ('wb' ) as file_obj :
1037
+ file_obj .write (mime_content )
1038
+ return True
1039
+ return False
0 commit comments