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