1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jamesiarmes\PEWS\Mail; |
4
|
|
|
|
5
|
|
|
use jamesiarmes\PEWS\API; |
6
|
|
|
use jamesiarmes\PEWS\API\Type; |
7
|
|
|
use \DateTime; |
8
|
|
|
use jamesiarmes\PEWS\API\Type\MessageType; |
9
|
|
|
|
10
|
|
|
class MailAPI extends API |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Type\FolderIdType |
14
|
|
|
*/ |
15
|
|
|
protected $folderId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return Type\FolderIdType |
19
|
|
|
*/ |
20
|
6 |
|
public function getFolderId() |
21
|
|
|
{ |
22
|
6 |
|
if (!$this->folderId) { |
23
|
1 |
|
$this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
|
|
|
|
24
|
1 |
|
} |
25
|
|
|
|
26
|
6 |
|
return $this->folderId; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Type\FolderIdType $folderId |
31
|
|
|
*/ |
32
|
1 |
|
public function setFolderId($folderId) |
33
|
|
|
{ |
34
|
1 |
|
$this->folderId = $folderId; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $displayName |
39
|
|
|
* @param string|Type\FolderIdType $parentFolder |
40
|
|
|
*/ |
41
|
6 |
|
public function pickMailFolder($displayName = null, $parentFolder = 'inbox') |
42
|
|
|
{ |
43
|
6 |
|
if ($displayName === null) { |
44
|
1 |
|
$this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
|
|
|
|
45
|
1 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
$folder = $this->getFolderByDisplayName($displayName, $parentFolder); |
49
|
6 |
|
$this->folderId = $folder->getFolderId(); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
1 |
|
protected function formatRestrictions($restrictions) |
53
|
|
|
{ |
54
|
1 |
|
foreach ($restrictions as $restrictionType => $query) { |
55
|
1 |
|
$formattedRestrictionType = array(); |
56
|
1 |
|
foreach ($query as $key => $value) { |
57
|
1 |
|
if ($value === false) { |
58
|
1 |
|
$value = 'false'; |
59
|
1 |
|
} |
60
|
1 |
|
$formattedRestrictionType[] = array( |
61
|
1 |
|
'FieldURI' => array('FieldURI' => $this->getFieldUriByName($key, 'message')), |
62
|
1 |
|
'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value)) |
63
|
1 |
|
); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
$restrictions[$restrictionType] = $formattedRestrictionType; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
return $restrictions; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get all mail items in the inbox |
74
|
|
|
* |
75
|
|
|
* @param Type\FolderIdType |
76
|
|
|
* @param array $options |
77
|
|
|
* @return Type\MessageType[] |
78
|
|
|
*/ |
79
|
6 |
|
public function getMailItems($folderId = null, $options = array()) |
80
|
|
|
{ |
81
|
6 |
|
if (!$folderId) { |
82
|
6 |
|
$folderId = $this->getFolderId(); |
83
|
6 |
|
} |
84
|
|
|
|
85
|
|
|
$request = array( |
86
|
6 |
|
'Traversal' => 'Shallow', |
87
|
|
|
'ItemShape' => array( |
88
|
|
|
'BaseShape' => 'AllProperties' |
89
|
6 |
|
), |
90
|
|
|
'ParentFolderIds' => array( |
91
|
6 |
|
'FolderId' => $folderId->toXmlObject() |
92
|
6 |
|
) |
93
|
6 |
|
); |
94
|
|
|
|
95
|
6 |
|
if (!empty($options['Restriction'])) { |
96
|
1 |
|
$options['Restriction'] = $this->formatRestrictions($options['Restriction']); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
6 |
|
$request = array_replace_recursive($request, $options); |
100
|
|
|
|
101
|
6 |
|
$request = Type::buildFromArray($request); |
102
|
6 |
|
$response = $this->getClient()->FindItem($request); |
103
|
6 |
|
$messages = $response->getItems()->getMessage(); |
|
|
|
|
104
|
|
|
|
105
|
6 |
|
if ($messages === null) { |
106
|
6 |
|
$messages = array(); |
107
|
6 |
|
} |
108
|
|
|
|
109
|
6 |
|
if (!is_array($messages)) { |
110
|
3 |
|
$messages = array($messages); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
6 |
|
return $messages; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Type\FolderIdType $folderId |
118
|
|
|
* @param array $options |
119
|
|
|
* @return Type\MessageType[] |
120
|
|
|
*/ |
121
|
1 |
|
public function getUnreadMailItems($folderId = null, $options = array()) |
122
|
|
|
{ |
123
|
|
|
$unReadOption = array( |
124
|
|
|
'Restriction' => array( |
125
|
|
|
'IsEqualTo' => array( |
126
|
|
|
'IsRead' => false |
127
|
1 |
|
) |
128
|
1 |
|
) |
129
|
1 |
|
); |
130
|
|
|
|
131
|
1 |
|
$options = array_replace_recursive($unReadOption, $options); |
132
|
|
|
|
133
|
1 |
|
return $this->getMailItems($folderId, $options); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Updates a calendar item with changes |
138
|
|
|
* |
139
|
|
|
* @param $itemId Type\ItemIdType|Type |
140
|
|
|
* @param $changes |
141
|
|
|
* @return Type\MessageType[] |
142
|
|
|
*/ |
143
|
1 |
|
public function updateMailItem($itemId, $changes) |
144
|
|
|
{ |
145
|
|
|
//Create the request |
146
|
|
|
$request = array( |
147
|
|
|
'ItemChange' => array( |
148
|
1 |
|
'ItemId' => $itemId->toArray(), |
149
|
|
|
'Updates' => array( |
150
|
1 |
|
'SetItemField' => $this->buildUpdateItemChanges('Message', 'message', $changes) |
151
|
1 |
|
) |
152
|
1 |
|
) |
153
|
1 |
|
); |
154
|
|
|
|
155
|
1 |
|
$items = $this->updateItems($request); |
156
|
|
|
|
157
|
1 |
|
if (!is_array($items)) { |
158
|
1 |
|
$items = array($items); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
1 |
|
return $items; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $mailItem Type\MessageType|Type\ItemIdType |
166
|
|
|
* @param $isRead boolean |
167
|
|
|
*/ |
168
|
1 |
|
public function markMailAsRead($mailItem, $isRead = true) |
169
|
|
|
{ |
170
|
1 |
|
if ($mailItem instanceof Type\MessageType) { |
|
|
|
|
171
|
1 |
|
$mailItem = $mailItem->getItemId(); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
1 |
|
$this->updateMailItem($mailItem, array( |
175
|
|
|
'IsRead' => $isRead |
176
|
1 |
|
)); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
3 |
|
public function sendMail(MessageType $message, $options = array()) |
180
|
|
|
{ |
181
|
3 |
|
$items = array('Message' => $message->toXmlObject()); |
182
|
|
|
$defaultOptions = array( |
183
|
3 |
|
'MessageDisposition' => 'SendAndSaveCopy', |
184
|
3 |
|
); |
185
|
|
|
|
186
|
3 |
|
if ($this->getPrimarySmtpMailbox() != null) { |
187
|
|
|
$sentItems = $this->getFolderByDistinguishedId('sentitems')->getFolderId(); |
|
|
|
|
188
|
|
|
$defaultOptions['SavedItemFolderId'] = |
189
|
|
|
array('FolderId' => $sentItems->toXmlObject()); |
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
$options = array_replace_recursive($defaultOptions, $options); |
193
|
|
|
|
194
|
3 |
|
return $this->createItems($items, $options); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getAttachment(Type\AttachmentIdType $attachmentId) |
198
|
|
|
{ |
199
|
|
|
$request = array ( |
200
|
|
|
'AttachmentIds' => array( |
201
|
|
|
$attachmentId->toXmlObject() |
202
|
|
|
), |
203
|
|
|
'AttachmentShape' => array( |
204
|
|
|
'IncludeMimeContent' => true |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$attachment = $this->getClient()->GetAttachment($request); |
|
|
|
|
209
|
|
|
return $attachment; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: