1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg; |
4
|
|
|
|
5
|
|
|
use ElggEntity; |
6
|
|
|
use ElggUser; |
7
|
|
|
use InvalidParameterException; |
8
|
|
|
use Elgg\Email\Attachment; |
9
|
|
|
use Zend\Mail\Address; |
10
|
|
|
use Zend\Mime\Part; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Email message |
14
|
|
|
*/ |
15
|
|
|
final class Email { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Address |
19
|
|
|
*/ |
20
|
|
|
protected $from; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Address |
24
|
|
|
*/ |
25
|
|
|
protected $to; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $subject; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $body; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $params = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $headers = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Part[] |
49
|
|
|
*/ |
50
|
|
|
protected $attachments = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create an email instance form an array of options |
54
|
|
|
* |
55
|
|
|
* @param array $options Options |
56
|
|
|
* 'from' - ElggEntity, or email string, or Zend Address |
57
|
|
|
* 'to' - ElggEntity, or email string, or Zend Address |
58
|
|
|
* 'subject' - subject string |
59
|
|
|
* 'body' - body string |
60
|
|
|
* 'params' - additional parameters |
61
|
|
|
* 'headers' - HTTP/IMF headers |
62
|
|
|
* @return \Elgg\Email |
63
|
|
|
*/ |
64
|
10 |
|
public static function factory(array $options = []) { |
65
|
10 |
|
$from = elgg_extract('from', $options); |
66
|
10 |
|
$to = elgg_extract('to', $options); |
67
|
10 |
|
$subject = elgg_extract('subject', $options); |
68
|
10 |
|
$body = elgg_extract('body', $options); |
69
|
10 |
|
$params = elgg_extract('params', $options, []); |
70
|
10 |
|
$headers = elgg_extract('headers', $options, []); |
71
|
|
|
|
72
|
10 |
|
$email = new self(); |
73
|
10 |
|
$email->setFrom(self::prepareFrom($from)); |
74
|
10 |
|
$email->setTo(self::prepareTo($to)); |
75
|
10 |
|
$email->setSubject($subject); |
76
|
10 |
|
$email->setBody($body); |
77
|
10 |
|
$email->setParams($params); |
78
|
10 |
|
$email->setHeaders($headers); |
79
|
|
|
|
80
|
10 |
|
if (isset($params['attachments']) && is_array($params['attachments'])) { |
81
|
3 |
|
foreach ($params['attachments'] as $attachment) { |
82
|
3 |
|
$email->addAttachment($attachment); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
return $email; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets sender address |
91
|
|
|
* |
92
|
|
|
* @param Address $from Sender address |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
10 |
|
public function setFrom(Address $from) { |
96
|
10 |
|
$this->from = $from; |
97
|
10 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns sender address |
102
|
|
|
* @return Address |
103
|
|
|
*/ |
104
|
7 |
|
public function getFrom() { |
105
|
7 |
|
return $this->from; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets recipient address |
110
|
|
|
* |
111
|
|
|
* @param Address $to Recipient address |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
10 |
|
public function setTo(Address $to) { |
115
|
10 |
|
$this->to = $to; |
116
|
10 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns recipient address |
121
|
|
|
* @return Address |
122
|
|
|
*/ |
123
|
7 |
|
public function getTo() { |
124
|
7 |
|
return $this->to; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets email subject |
129
|
|
|
* |
130
|
|
|
* @param string $subject Subject |
131
|
|
|
* @return self |
132
|
|
|
*/ |
133
|
10 |
|
public function setSubject($subject = '') { |
134
|
10 |
|
$this->subject = $subject; |
135
|
10 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the subject |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
3 |
|
public function getSubject() { |
143
|
3 |
|
return $this->subject; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets the email message body |
148
|
|
|
* |
149
|
|
|
* @param string $body Body |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
10 |
|
public function setBody($body = '') { |
153
|
10 |
|
$this->body = $body; |
154
|
10 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns email body |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
3 |
|
public function getBody() { |
162
|
3 |
|
return $this->body; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets additional params |
167
|
|
|
* |
168
|
|
|
* @param array $params Params |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
10 |
|
public function setParams(array $params = []) { |
172
|
10 |
|
$this->params = $params; |
173
|
10 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns additional params |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
2 |
|
public function getParams() { |
181
|
2 |
|
return $this->params; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Adds/replaces an HTTP/IMF header |
186
|
|
|
* |
187
|
|
|
* @param string $name Header name |
188
|
|
|
* @param mixed $value Header value |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
1 |
|
public function addHeader($name, $value) { |
192
|
1 |
|
$this->headers[$name] = $value; |
193
|
1 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Replaces header bag |
198
|
|
|
* |
199
|
|
|
* @param array $headers Headers |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
10 |
|
public function setHeaders(array $headers = []) { |
203
|
10 |
|
$this->headers = $headers; |
204
|
10 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns headers |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
3 |
|
public function getHeaders() { |
212
|
3 |
|
return $this->headers; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Add an attachment |
217
|
|
|
* |
218
|
|
|
* @param mixed $attachment \Zend\Mime\Part or \Elgg\Email\Attachment or \ElggFile or an array |
219
|
|
|
* |
220
|
|
|
* @see \Elgg\Email\Attachment::factory() |
221
|
|
|
* |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
7 |
|
public function addAttachment($attachment) { |
225
|
|
|
|
226
|
7 |
|
if ($attachment instanceof Part) { |
227
|
1 |
|
$this->attachments[] = $attachment; |
228
|
1 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
6 |
|
if ($attachment instanceof \ElggFile) { |
232
|
3 |
|
$this->attachments[] = Attachment::fromElggFile($attachment); |
233
|
3 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
5 |
|
$attachment = Attachment::factory($attachment); |
237
|
5 |
|
if (!empty($attachment)) { |
238
|
4 |
|
$this->attachments[] = $attachment; |
239
|
|
|
} |
240
|
|
|
|
241
|
5 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get all attachments |
246
|
|
|
* |
247
|
|
|
* @return \Zend\Mime\Part[] |
248
|
|
|
*/ |
249
|
9 |
|
public function getAttachments() { |
250
|
9 |
|
return $this->attachments; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Converts mixed input to an instance of Zend addres |
255
|
|
|
* |
256
|
|
|
* @param mixed $from From input |
257
|
|
|
* @return Address |
258
|
|
|
* @throws InvalidParameterException |
259
|
|
|
*/ |
260
|
10 |
|
protected static function prepareFrom($from) { |
261
|
10 |
|
if ($from instanceof ElggEntity) { |
262
|
|
|
// If there's an email address, use it - but only if it's not from a user. |
263
|
1 |
|
if (!$from instanceof ElggUser && $from->email) { |
|
|
|
|
264
|
|
|
$from = new Address($from->email, $from->getDisplayName()); |
|
|
|
|
265
|
|
|
} else { |
266
|
|
|
// get the site email address |
267
|
1 |
|
$site = elgg_get_site_entity(); |
268
|
1 |
|
$from = new Address($site->getEmailAddress(), $site->getDisplayName()); |
269
|
|
|
} |
270
|
9 |
|
} else if (is_string($from)) { |
271
|
8 |
|
$from = self::fromString($from); |
272
|
|
|
} |
273
|
|
|
|
274
|
10 |
|
if (!$from instanceof Address) { |
275
|
|
|
throw new InvalidParameterException("From address is not in a valid format"); |
276
|
|
|
} |
277
|
|
|
|
278
|
10 |
|
return $from; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Converts mixed input to an instance of Zend addres |
283
|
|
|
* |
284
|
|
|
* @param mixed $to To input |
285
|
|
|
* @return Address |
286
|
|
|
* @throws InvalidParameterException |
287
|
|
|
*/ |
288
|
10 |
|
protected static function prepareTo($to) { |
289
|
10 |
|
if ($to instanceof ElggEntity) { |
290
|
1 |
|
$to = new Address($to->email, $to->getDisplayName()); |
|
|
|
|
291
|
9 |
|
} else if (is_string($to)) { |
292
|
8 |
|
$to = self::fromString($to); |
293
|
|
|
} |
294
|
|
|
|
295
|
10 |
|
if (!$to instanceof Address) { |
296
|
|
|
throw new InvalidParameterException("To address is not in a valid format"); |
297
|
|
|
} |
298
|
|
|
|
299
|
10 |
|
return $to; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Parses strings like "Evan <[email protected]>" into name/email objects. |
304
|
|
|
* |
305
|
|
|
* This is not very sophisticated and only used to provide a light BC effort. |
306
|
|
|
* |
307
|
|
|
* @param string $contact e.g. "Evan <[email protected]>" |
308
|
|
|
* |
309
|
|
|
* @return Address |
310
|
|
|
*/ |
311
|
8 |
|
public static function fromString($contact) { |
312
|
8 |
|
$containsName = preg_match('/<(.*)>/', $contact, $matches) == 1; |
313
|
8 |
|
if ($containsName) { |
314
|
2 |
|
$name = trim(substr($contact, 0, strpos($contact, '<'))); |
315
|
2 |
|
return new Address($matches[1], $name); |
316
|
|
|
} else { |
317
|
6 |
|
return new Address(trim($contact)); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Format an email address and name into a formatted email address |
323
|
|
|
* |
324
|
|
|
* eg "Some name <[email protected]>" |
325
|
|
|
* |
326
|
|
|
* @param string $email the email address |
327
|
|
|
* @param string $name the name |
328
|
|
|
* |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
|
|
public static function getFormattedEmailAddress($email, $name = null) { |
332
|
|
|
$mail = new Address($email, $name); |
333
|
|
|
return $mail->toString(); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|