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