|
1
|
|
|
<?php namespace Camelcased\Postmark\Inbound\Parse; |
|
2
|
|
|
|
|
3
|
|
|
class Parser { |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @var array |
|
7
|
|
|
*/ |
|
8
|
|
|
protected $inbound; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $output; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($json) |
|
17
|
|
|
{ |
|
18
|
|
|
if (empty($json)) |
|
19
|
|
|
{ |
|
20
|
|
|
// Well we weren't given anything to parse |
|
21
|
|
|
$this->inbound = []; |
|
22
|
|
|
} else { |
|
23
|
|
|
// Check to see if the given JSON is already an array |
|
24
|
|
|
if (is_array($json)) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->inbound = $json; |
|
27
|
|
|
} else { |
|
28
|
|
|
// Convert the string to an array |
|
29
|
|
|
$this->inbound = $this->jsonToArray($json); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Parse the email from JSON to a formatted array |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public function parse() |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->inbound == []) |
|
42
|
|
|
{ |
|
43
|
|
|
// Well that was short lived. |
|
44
|
|
|
return []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Set the Body field based on whether it is HTMl or good old fashioned plain text |
|
48
|
|
|
if (!$this->htmlBody()) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->output['body'] = $this->inbound["TextBody"]; |
|
51
|
|
|
} else { |
|
52
|
|
|
$this->output['body'] = $this->inbound["HtmlBody"]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Easy stuff to parse. Self explainatory. |
|
56
|
|
|
$this->output['subject'] = $this->inbound["Subject"]; |
|
57
|
|
|
$this->output['to'] = $this->inbound["To"]; |
|
58
|
|
|
$this->output['replyTo'] = $this->replyTo(); |
|
59
|
|
|
$this->output['from'] = $this->inbound["From"]; |
|
60
|
|
|
|
|
61
|
|
|
// Set cc field if the email has any CC's set |
|
62
|
|
|
if ($this->has('Cc')) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->output['cc'] = $this->carbon('Cc'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Set bcc field if the email has any BCC's set |
|
68
|
|
|
if ($this->has('Bcc')) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->output['bcc'] = $this->carbon('Bcc'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Does the email have any attachments |
|
74
|
|
|
if ($this->has('Attachments')) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->output['Attachments'] = []; |
|
77
|
|
|
$i = 0; |
|
78
|
|
|
|
|
79
|
|
|
// Loop through each of the attachments and convert it to an array for later use |
|
80
|
|
|
foreach($this->inbound["Attachments"] as $attachment) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->output['Attachments'][$i] = ["Name" => $attachment["Name"], "Content" => $attachment["Content"], "MIME" => $attachment["ContentType"]]; |
|
83
|
|
|
$i++; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Return the parsed email |
|
88
|
|
|
return $this->output; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Converts the given JSON string to an array |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $json |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
private function jsonToArray($json) |
|
98
|
|
|
{ |
|
99
|
|
|
$source = json_decode($json, true); |
|
100
|
|
|
|
|
101
|
|
|
if (json_last_error() == JSON_ERROR_NONE) |
|
102
|
|
|
{ |
|
103
|
|
|
return $source; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return []; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Checks if there is an html body |
|
111
|
|
|
* |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
private function htmlBody() |
|
115
|
|
|
{ |
|
116
|
|
|
return ($this->inbound["HtmlBody"]) != null || ($this->inbound["HtmlBody"]) != "" ? true : false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Sets the correct ReplyTo field based on whether the email had a ReplyTo field or not. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function replyTo() |
|
125
|
|
|
{ |
|
126
|
|
|
// No ReplyTo field given from the email |
|
127
|
|
|
if (empty($this->inbound["ReplyTo"]) || $this->inbound["ReplyTo"] == '') |
|
128
|
|
|
{ |
|
129
|
|
|
// So we set the ReplyTo field as the address given in the From field |
|
130
|
|
|
return $this->inbound["From"]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->inbound["ReplyTo"]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Extracts the email from the input. Ex: "Full name" <[email protected]> |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $input |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
private function extractEmail($input) |
|
143
|
|
|
{ |
|
144
|
|
|
if (preg_match('~<(.*?)>~', $input, $output) == 1) |
|
145
|
|
|
{ |
|
146
|
|
|
return $output[1]; |
|
147
|
|
|
} |
|
148
|
|
|
return $input; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Converts BCC and CC fields into an array or string if needed |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $field |
|
155
|
|
|
* @return mixed |
|
156
|
|
|
*/ |
|
157
|
|
|
private function carbon($field) |
|
158
|
|
|
{ |
|
159
|
|
|
$carbons = explode(',', $this->inbound[$field]); |
|
160
|
|
|
|
|
161
|
|
|
// Is there more than one email in the given carbon field |
|
162
|
|
|
if ($carbons != [$this->inbound[$field]]) |
|
163
|
|
|
{ |
|
164
|
|
|
$final = []; |
|
165
|
|
|
foreach ($carbons as $carbon) { |
|
166
|
|
|
array_push($final, $this->extractEmail($carbon)); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $final; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->extractEmail($this->inbound[$field]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Simple helper function to check if the email has the given field |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $key |
|
179
|
|
|
* @return boolean |
|
180
|
|
|
*/ |
|
181
|
|
|
private function has($key) |
|
182
|
|
|
{ |
|
183
|
|
|
if (array_key_exists($key, $this->inbound)) { |
|
184
|
|
|
return count($this->inbound[$key]) > 0 ? true : false; |
|
185
|
|
|
} else { |
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|