1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inbounder\Parsers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Inbounder\Parsers\Contracts\EmailParserInterface; |
7
|
|
|
use Inbounder\Parsers\Contracts\ParserInterface; |
8
|
|
|
use Inbounder\Parsers\Objects\Attachment; |
9
|
|
|
use Inbounder\Parsers\Objects\Email\Address; |
10
|
|
|
use Inbounder\Parsers\Objects\Email\Header; |
11
|
|
|
|
12
|
|
|
class PostmarkappParser extends AbstractParser implements EmailParserInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Parse the request and return itself. |
16
|
|
|
* |
17
|
|
|
* @return ParserInterface |
18
|
|
|
*/ |
19
|
|
|
public function parse() : ParserInterface |
20
|
|
|
{ |
21
|
|
|
$this->from = $this->from(); |
|
|
|
|
22
|
|
|
$this->replyTo = $this->replyTo(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
$this->to = $this->to(); |
|
|
|
|
25
|
|
|
$this->cc = $this->cc(); |
|
|
|
|
26
|
|
|
$this->bcc = $this->bcc(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->subject = $this->subject(); |
|
|
|
|
29
|
|
|
$this->body = $this->body(); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->attachements = $this->attachements(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return an Address object from an input. |
38
|
|
|
* |
39
|
|
|
* @param array $input |
40
|
|
|
* |
41
|
|
|
* @return Address |
42
|
|
|
*/ |
43
|
|
|
protected function address(array $input) : Address |
44
|
|
|
{ |
45
|
|
|
return new Address($input['Email'], $input['Name'], $input['MailboxHash']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Parse an array of addresses. |
50
|
|
|
* |
51
|
|
|
* @param array $input |
52
|
|
|
* |
53
|
|
|
* @return Collection |
54
|
|
|
*/ |
55
|
|
|
protected function foreachAddress(array $input) : Collection |
56
|
|
|
{ |
57
|
|
|
$collection = new Collection(); |
58
|
|
|
|
59
|
|
|
foreach ($input as $address) { |
60
|
|
|
$collection->push($this->address($address)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $collection; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* From property. |
68
|
|
|
* |
69
|
|
|
* @return Address |
70
|
|
|
*/ |
71
|
|
|
public function from() : Address |
72
|
|
|
{ |
73
|
|
|
return $this->address($this->input('FromFull')); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Reply-to property. |
78
|
|
|
* |
79
|
|
|
* @todo: edit this to handler the reply-to parameter |
80
|
|
|
* |
81
|
|
|
* @return Address |
82
|
|
|
*/ |
83
|
|
|
public function replyTo() : Address |
84
|
|
|
{ |
85
|
|
|
return $this->address($this->input('FromFull')); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* To property. |
90
|
|
|
* |
91
|
|
|
* @return Collection<Address> |
|
|
|
|
92
|
|
|
*/ |
93
|
|
|
public function to() : Collection |
94
|
|
|
{ |
95
|
|
|
return $this->foreachAddress($this->input('ToFull')); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* CC property. |
100
|
|
|
* |
101
|
|
|
* @return Collection<Address> |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public function cc() : Collection |
104
|
|
|
{ |
105
|
|
|
return $this->foreachAddress($this->input('CcFull')); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* BCC property. |
110
|
|
|
* |
111
|
|
|
* @return Collection<Address> |
|
|
|
|
112
|
|
|
*/ |
113
|
|
|
public function bcc() : Collection |
114
|
|
|
{ |
115
|
|
|
return $this->foreachAddress($this->input('BccFull')); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Subject property. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function subject() |
124
|
|
|
{ |
125
|
|
|
return $this->input('Subject'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Body. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function body() |
134
|
|
|
{ |
135
|
|
|
$body = $this->bodyHtml(); |
|
|
|
|
136
|
|
|
|
137
|
|
|
if ($this->bodyIsText()) { |
138
|
|
|
$body = $this->bodyText(); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $body; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the body html. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function bodyHtml() |
150
|
|
|
{ |
151
|
|
|
return $this->input('HtmlBody'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return the body text. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function bodyText() |
160
|
|
|
{ |
161
|
|
|
return $this->input('TextBody'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* IsText. |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function bodyIsText() |
170
|
|
|
{ |
171
|
|
|
return !$this->bodyIsHtml(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* IsHtml. |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function bodyIsHtml() |
180
|
|
|
{ |
181
|
|
|
return !is_null($this->input('HtmlBody')) || $this->input('HtmlBody') !== ''; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get email headers. |
186
|
|
|
* |
187
|
|
|
* @return Collection |
188
|
|
|
*/ |
189
|
|
|
public function headers() : Collection |
190
|
|
|
{ |
191
|
|
|
$collection = new Collection(); |
192
|
|
|
|
193
|
|
|
foreach ($this->input('Headers') as $header) { |
|
|
|
|
194
|
|
|
$collection->push(new Header($header['Name'], $header['Value'])); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $collection; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* retrieve a email header. |
202
|
|
|
* |
203
|
|
|
* return Array |
204
|
|
|
*/ |
205
|
|
|
public function header($name) : Header |
206
|
|
|
{ |
207
|
|
|
return $this->headers()->filter(function ($header) use ($name) { |
208
|
|
|
return $header->name === $name; |
209
|
|
|
})->first(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Determinate if the email is marked as spam. |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function isSpam() |
218
|
|
|
{ |
219
|
|
|
$header = $this->header('X-Spam-Flag'); |
220
|
|
|
|
221
|
|
|
return $header->value === 'NO' ? false : true; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Has attachements. |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function hasAttachements() |
230
|
|
|
{ |
231
|
|
|
return $this->attachements->isEmpty(); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Attachments. |
236
|
|
|
* |
237
|
|
|
* @return Collection<File> |
|
|
|
|
238
|
|
|
*/ |
239
|
|
|
public function attachements() : Collection |
240
|
|
|
{ |
241
|
|
|
$collection = new Collection(); |
242
|
|
|
|
243
|
|
|
foreach ($this->input('Attachments') as $attachment) { |
|
|
|
|
244
|
|
|
$collection->push( |
245
|
|
|
new Attachment( |
246
|
|
|
$attachment['Name'], |
247
|
|
|
(isset($attachment['Content']) ? $attachment['Content'] : ''), |
248
|
|
|
$attachment['ContentType'], |
249
|
|
|
$attachment['ContentLength'] |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $collection; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.