1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Mail\Template; |
4
|
|
|
|
5
|
|
|
use TheCodingMachine\Mail\SwiftMailTemplate; |
6
|
|
|
|
7
|
|
|
class SwiftTwigMailTemplate implements SwiftMailTemplate |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Twig_Environment |
11
|
|
|
*/ |
12
|
|
|
protected $twigEnvironment; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $twigPath; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|array |
21
|
|
|
*/ |
22
|
|
|
protected $fromAddresses; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $fromName = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|array |
31
|
|
|
*/ |
32
|
|
|
protected $toAddresses; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $toName = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string|array |
41
|
|
|
*/ |
42
|
|
|
protected $bccAddresses; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $bccName = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string|array |
51
|
|
|
*/ |
52
|
|
|
protected $ccAddresses; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $ccName = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|array |
61
|
|
|
*/ |
62
|
|
|
protected $replyToAddresses; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $replyToName = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
protected $maxLineLength = 1000; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
protected $priority; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $readReceiptTo; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected $returnPath; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* SwiftTwigMailGenerator constructor. |
91
|
|
|
* |
92
|
|
|
* @param \Twig_Environment $twig_Environment |
93
|
|
|
* @param string $twigPath |
94
|
|
|
*/ |
95
|
4 |
|
public function __construct(\Twig_Environment $twig_Environment, string $twigPath) |
96
|
|
|
{ |
97
|
4 |
|
$this->twigEnvironment = $twig_Environment; |
98
|
4 |
|
$this->twigPath = $twigPath; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $data |
103
|
|
|
* |
104
|
|
|
* @return \Swift_Message |
105
|
|
|
*/ |
106
|
4 |
|
public function renderMail(array $data = []) :\Swift_Message |
107
|
|
|
{ |
108
|
4 |
|
$mail = new \Swift_Message(); |
109
|
|
|
|
110
|
4 |
|
$twigEnvironment = clone $this->twigEnvironment; |
111
|
4 |
|
$function = new \Twig_SimpleFunction('embedImage', function ($imgPath) use ($mail) { |
112
|
|
|
return $mail->embed(\Swift_Image::fromPath($imgPath)); |
113
|
4 |
|
}); |
114
|
4 |
|
$twigEnvironment->addFunction($function); |
115
|
|
|
|
116
|
4 |
|
$template = $twigEnvironment->loadTemplate($this->twigPath); |
117
|
|
|
|
118
|
4 |
|
if (!$template->hasBlock('subject') || (!$template->hasBlock('body_html') && !$template->hasBlock('body_text'))) { |
119
|
1 |
|
throw MissingBlockException::missingBlock($template->getBlockNames()); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
$subject = $template->renderBlock('subject', $data); |
|
|
|
|
123
|
3 |
|
$mail->setSubject($subject); |
124
|
|
|
|
125
|
3 |
|
if ($template->hasBlock('body_html')) { |
126
|
2 |
|
$bodyHtml = $template->renderBlock('body_html', $data); |
|
|
|
|
127
|
2 |
|
if (!$template->hasBlock('body_text')) { |
128
|
1 |
|
$bodyText = $this->removeHtml($bodyHtml); |
129
|
|
|
} else { |
130
|
1 |
|
$bodyText = $template->renderBlock('body_text', $data); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
$mail->setBody($bodyHtml, 'text/html'); |
134
|
2 |
|
$mail->addPart($bodyText, 'text/plain'); |
135
|
|
|
} else { |
136
|
1 |
|
$bodyText = $template->renderBlock('body_text', $data); |
|
|
|
|
137
|
|
|
|
138
|
1 |
|
$mail->setBody($bodyText, 'text/plain'); |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
if( $this->fromAddresses) { |
142
|
1 |
|
$mail->setFrom($this->fromAddresses, $this->fromName); |
143
|
1 |
|
$mail->setSender($this->fromAddresses, $this->fromName); |
144
|
|
|
} |
145
|
3 |
|
if( $this->toAddresses) { |
146
|
1 |
|
$mail->setTo($this->toAddresses, $this->toName); |
147
|
|
|
} |
148
|
3 |
|
if( $this->bccAddresses) { |
149
|
1 |
|
$mail->setBcc($this->bccAddresses, $this->bccName); |
150
|
|
|
} |
151
|
3 |
|
if( $this->ccAddresses) { |
152
|
1 |
|
$mail->setCc($this->ccAddresses, $this->ccName); |
153
|
|
|
} |
154
|
3 |
|
if( $this->replyToAddresses) { |
155
|
1 |
|
$mail->setReplyTo($this->replyToAddresses, $this->replyToName); |
156
|
|
|
} |
157
|
3 |
|
if( $this->maxLineLength) { |
158
|
3 |
|
$mail->setMaxLineLength($this->maxLineLength); |
159
|
|
|
} |
160
|
3 |
|
if( $this->priority) { |
161
|
1 |
|
$mail->setPriority($this->priority); |
162
|
|
|
} |
163
|
3 |
|
if( $this->readReceiptTo) { |
164
|
1 |
|
$mail->setReadReceiptTo($this->readReceiptTo); |
|
|
|
|
165
|
|
|
} |
166
|
3 |
|
if( $this->returnPath) { |
167
|
1 |
|
$mail->setReturnPath($this->returnPath); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
3 |
|
return $mail; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array|string $fromAddresses |
176
|
|
|
*/ |
177
|
1 |
|
public function setFromAddresses($fromAddresses) |
178
|
|
|
{ |
179
|
1 |
|
$this->fromAddresses = $fromAddresses; |
180
|
1 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $fromName |
184
|
|
|
*/ |
185
|
1 |
|
public function setFromName($fromName) |
186
|
|
|
{ |
187
|
1 |
|
$this->fromName = $fromName; |
188
|
1 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array|string $toAddresses |
192
|
|
|
*/ |
193
|
1 |
|
public function setToAddresses($toAddresses) |
194
|
|
|
{ |
195
|
1 |
|
$this->toAddresses = $toAddresses; |
196
|
1 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $toName |
200
|
|
|
*/ |
201
|
1 |
|
public function setToName($toName) |
202
|
|
|
{ |
203
|
1 |
|
$this->toName = $toName; |
204
|
1 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param array|string $bccAddresses |
208
|
|
|
*/ |
209
|
1 |
|
public function setBccAddresses($bccAddresses) |
210
|
|
|
{ |
211
|
1 |
|
$this->bccAddresses = $bccAddresses; |
212
|
1 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $bccName |
216
|
|
|
*/ |
217
|
1 |
|
public function setBccName($bccName) |
218
|
|
|
{ |
219
|
1 |
|
$this->bccName = $bccName; |
220
|
1 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param array|string $ccAddresses |
224
|
|
|
*/ |
225
|
1 |
|
public function setCcAddresses($ccAddresses) |
226
|
|
|
{ |
227
|
1 |
|
$this->ccAddresses = $ccAddresses; |
228
|
1 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $ccName |
232
|
|
|
*/ |
233
|
1 |
|
public function setCcName($ccName) |
234
|
|
|
{ |
235
|
1 |
|
$this->ccName = $ccName; |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param array|string $replyToAddresses |
240
|
|
|
*/ |
241
|
1 |
|
public function setReplyToAddresses($replyToAddresses) |
242
|
|
|
{ |
243
|
1 |
|
$this->replyToAddresses = $replyToAddresses; |
244
|
1 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $replyToName |
248
|
|
|
*/ |
249
|
1 |
|
public function setReplyToName($replyToName) |
250
|
|
|
{ |
251
|
1 |
|
$this->replyToName = $replyToName; |
252
|
1 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param int $maxLineLength |
256
|
|
|
*/ |
257
|
1 |
|
public function setMaxLineLength($maxLineLength) |
258
|
|
|
{ |
259
|
1 |
|
$this->maxLineLength = $maxLineLength; |
260
|
1 |
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param int $priority |
264
|
|
|
*/ |
265
|
1 |
|
public function setPriority($priority) |
266
|
|
|
{ |
267
|
1 |
|
$this->priority = $priority; |
268
|
1 |
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $readReceiptTo |
272
|
|
|
*/ |
273
|
1 |
|
public function setReadReceiptTo($readReceiptTo) |
274
|
|
|
{ |
275
|
1 |
|
$this->readReceiptTo = $readReceiptTo; |
276
|
1 |
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $returnPath |
280
|
|
|
*/ |
281
|
1 |
|
public function setReturnPath($returnPath) |
282
|
|
|
{ |
283
|
1 |
|
$this->returnPath = $returnPath; |
284
|
1 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Removes the HTML tags from the text. |
288
|
|
|
* |
289
|
|
|
* @param string $s |
290
|
|
|
* @param string $keep The list of tags to keep |
291
|
|
|
* @param string $expand The list of tags to remove completely, along their content |
292
|
|
|
*/ |
293
|
1 |
|
private function removeHtml(string $s, string $keep = '', string $expand = 'script|style|noframes|select|option') :string |
294
|
|
|
{ |
295
|
|
|
/**///prep the string |
296
|
1 |
|
$s = ' '.$s; |
297
|
|
|
|
298
|
|
|
/**///initialize keep tag logic |
299
|
1 |
|
if (strlen($keep) > 0) { |
300
|
|
|
$k = explode('|', $keep); |
301
|
|
|
$s = $this->keepTag($s, $k, '<', '[{('); |
302
|
|
|
} |
303
|
|
|
//begin removal |
304
|
|
|
/**///remove comment blocks |
305
|
1 |
|
$s = $this->removeElement($s, '<!--', '-->'); |
306
|
|
|
|
307
|
|
|
/**///remove tags with content between them |
308
|
1 |
|
if (strlen($expand) > 0) { |
309
|
1 |
|
$e = explode('|', $expand); |
310
|
1 |
|
$count = count($e); |
311
|
1 |
|
$pos = []; |
312
|
1 |
|
$len = []; |
313
|
1 |
|
for ($i = 0;$i < $count;++$i) { |
314
|
1 |
|
while (stripos($s, '<'.$e[$i]) > 0) { |
315
|
|
|
$len[1] = strlen('<'.$e[$i]); |
316
|
|
|
$pos[1] = stripos($s, '<'.$e[$i]); |
317
|
|
|
$pos[2] = stripos($s, $e[$i].'>', $pos[1] + $len[1]); |
318
|
|
|
$len[2] = $pos[2] - $pos[1] + $len[1]; |
319
|
|
|
$x = substr($s, $pos[1], $len[2]); |
320
|
|
|
$s = str_replace($x, '', $s); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/**///remove remaining tags |
326
|
1 |
|
$s = $this->removeElement($s, '<', '>'); |
327
|
|
|
|
328
|
|
|
/**///finalize keep tag |
329
|
1 |
|
if (isset($k)) { |
330
|
|
|
$s = $this->keepTag($s, $k, '[{(', '<'); |
331
|
|
|
} |
332
|
|
|
|
333
|
1 |
|
return trim($s); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param string $s |
338
|
|
|
* @param string $openTag |
339
|
|
|
* @param string $closeTag |
340
|
|
|
* |
341
|
|
|
* @return mixed|string |
342
|
|
|
*/ |
343
|
1 |
|
private function removeElement(string $s, string $openTag, string $closeTag) |
344
|
|
|
{ |
345
|
1 |
|
$pos = []; |
346
|
1 |
|
$len = []; |
347
|
1 |
|
while (stripos($s, $openTag) > 0) { |
348
|
1 |
|
$pos[1] = stripos($s, $openTag); |
349
|
1 |
|
$pos[2] = stripos($s, $closeTag, $pos[1]); |
350
|
1 |
|
$len[1] = $pos[2] - $pos[1] + 1; |
351
|
1 |
|
$x = substr($s, $pos[1], $len[1]); |
352
|
1 |
|
$s = str_replace($x, '', $s); |
353
|
|
|
} |
354
|
|
|
|
355
|
1 |
|
return $s; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param string $s |
360
|
|
|
* @param array $tagToKeep |
361
|
|
|
* @param string $initial |
362
|
|
|
* @param string $finalize |
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
|
|
private function keepTag(string $s, array $tagToKeep, string $initial, string $finalize):string |
367
|
|
|
{ |
368
|
|
|
$count = count($tagToKeep); |
369
|
|
|
for ($i = 0;$i < $count;++$i) { |
370
|
|
|
$s = str_replace($initial.$tagToKeep[$i], $finalize.$tagToKeep[$i], $s); |
371
|
|
|
$s = str_replace($initial.'/'.$tagToKeep[$i], $finalize.'/'.$tagToKeep[$i], $s); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return $s; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.