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
|
|
|
|
137
|
|
|
switch (true) { |
|
|
|
|
138
|
|
|
case $this->fromAddresses: |
|
|
|
|
139
|
|
|
$mail->setFrom($this->fromAddresses, $this->fromName); |
140
|
|
|
$mail->setSender($this->fromAddresses, $this->fromName); |
|
|
|
|
141
|
|
|
case $this->toAddresses: |
|
|
|
|
142
|
|
|
$mail->setTo($this->toAddresses, $this->toName); |
143
|
|
|
case $this->bccAddresses: |
|
|
|
|
144
|
|
|
$mail->setBcc($this->bccAddresses, $this->bccName); |
145
|
|
|
case $this->ccAddresses: |
|
|
|
|
146
|
|
|
$mail->setCc($this->ccAddresses, $this->ccName); |
147
|
|
|
case $this->replyToAddresses: |
|
|
|
|
148
|
|
|
$mail->setReplyTo($this->replyToAddresses, $this->replyToName); |
149
|
|
|
case $this->maxLineLength: |
|
|
|
|
150
|
|
|
$mail->setMaxLineLength($this->maxLineLength); |
151
|
|
|
case $this->priority: |
|
|
|
|
152
|
|
|
$mail->setPriority($this->priority); |
153
|
|
|
case $this->readReceiptTo: |
|
|
|
|
154
|
|
|
$mail->setReadReceiptTo($this->readReceiptTo); |
|
|
|
|
155
|
|
|
case $this->returnPath: |
|
|
|
|
156
|
|
|
$mail->setReturnPath($this->returnPath); |
157
|
|
|
default: |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $mail; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array|string $fromAddresses |
166
|
|
|
*/ |
167
|
|
|
public function setFromAddresses($fromAddresses) |
168
|
|
|
{ |
169
|
|
|
$this->fromAddresses = $fromAddresses; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $fromName |
174
|
|
|
*/ |
175
|
|
|
public function setFromName($fromName) |
176
|
|
|
{ |
177
|
|
|
$this->fromName = $fromName; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array|string $toAddresses |
182
|
|
|
*/ |
183
|
|
|
public function setToAddresses($toAddresses) |
184
|
|
|
{ |
185
|
|
|
$this->toAddresses = $toAddresses; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $toName |
190
|
|
|
*/ |
191
|
|
|
public function setToName($toName) |
192
|
|
|
{ |
193
|
|
|
$this->toName = $toName; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array|string $bccAddresses |
198
|
|
|
*/ |
199
|
|
|
public function setBccAddresses($bccAddresses) |
200
|
|
|
{ |
201
|
|
|
$this->bccAddresses = $bccAddresses; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $bccName |
206
|
|
|
*/ |
207
|
|
|
public function setBccName($bccName) |
208
|
|
|
{ |
209
|
|
|
$this->bccName = $bccName; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param array|string $ccAddresses |
214
|
|
|
*/ |
215
|
|
|
public function setCcAddresses($ccAddresses) |
216
|
|
|
{ |
217
|
|
|
$this->ccAddresses = $ccAddresses; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $ccName |
222
|
|
|
*/ |
223
|
|
|
public function setCcName($ccName) |
224
|
|
|
{ |
225
|
|
|
$this->ccName = $ccName; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param array|string $replyToAddresses |
230
|
|
|
*/ |
231
|
|
|
public function setReplyToAddresses($replyToAddresses) |
232
|
|
|
{ |
233
|
|
|
$this->replyToAddresses = $replyToAddresses; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $replyToName |
238
|
|
|
*/ |
239
|
|
|
public function setReplyToName($replyToName) |
240
|
|
|
{ |
241
|
|
|
$this->replyToName = $replyToName; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param int $maxLineLength |
246
|
|
|
*/ |
247
|
|
|
public function setMaxLineLength($maxLineLength) |
248
|
|
|
{ |
249
|
|
|
$this->maxLineLength = $maxLineLength; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param int $priority |
254
|
|
|
*/ |
255
|
|
|
public function setPriority($priority) |
256
|
|
|
{ |
257
|
|
|
$this->priority = $priority; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $readReceiptTo |
262
|
|
|
*/ |
263
|
|
|
public function setReadReceiptTo($readReceiptTo) |
264
|
|
|
{ |
265
|
|
|
$this->readReceiptTo = $readReceiptTo; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $returnPath |
270
|
|
|
*/ |
271
|
|
|
public function setReturnPath($returnPath) |
272
|
|
|
{ |
273
|
|
|
$this->returnPath = $returnPath; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Removes the HTML tags from the text. |
278
|
|
|
* |
279
|
|
|
* @param string $s |
280
|
|
|
* @param string $keep The list of tags to keep |
281
|
|
|
* @param string $expand The list of tags to remove completely, along their content |
282
|
|
|
*/ |
283
|
|
|
private function removeHtml(string $s , string $keep = '' , string $expand = 'script|style|noframes|select|option') :string |
284
|
|
|
{ |
285
|
|
|
/**///prep the string |
286
|
|
|
$s = ' ' . $s; |
287
|
|
|
|
288
|
|
|
/**///initialize keep tag logic |
289
|
|
|
if(strlen($keep) > 0){ |
290
|
|
|
$k = explode('|',$keep); |
291
|
|
View Code Duplication |
for($i=0;$i<count($k);$i++){ |
|
|
|
|
292
|
|
|
$s = str_replace('<' . $k[$i],'[{(' . $k[$i],$s); |
293
|
|
|
$s = str_replace('</' . $k[$i],'[{(/' . $k[$i],$s); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
$pos = array(); |
297
|
|
|
$len = array(); |
298
|
|
|
|
299
|
|
|
//begin removal |
300
|
|
|
/**///remove comment blocks |
301
|
|
View Code Duplication |
while(stripos($s,'<!--') > 0){ |
|
|
|
|
302
|
|
|
$pos[1] = stripos($s,'<!--'); |
303
|
|
|
$pos[2] = stripos($s,'-->', $pos[1]); |
304
|
|
|
$len[1] = $pos[2] - $pos[1] + 3; |
305
|
|
|
$x = substr($s,$pos[1],$len[1]); |
306
|
|
|
$s = str_replace($x,'',$s); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/**///remove tags with content between them |
310
|
|
|
if(strlen($expand) > 0){ |
311
|
|
|
$e = explode('|',$expand); |
312
|
|
|
for($i=0;$i<count($e);$i++){ |
|
|
|
|
313
|
|
|
while(stripos($s,'<' . $e[$i]) > 0){ |
314
|
|
|
$len[1] = strlen('<' . $e[$i]); |
315
|
|
|
$pos[1] = stripos($s,'<' . $e[$i]); |
316
|
|
|
$pos[2] = stripos($s,$e[$i] . '>', $pos[1] + $len[1]); |
317
|
|
|
$len[2] = $pos[2] - $pos[1] + $len[1]; |
318
|
|
|
$x = substr($s,$pos[1],$len[2]); |
319
|
|
|
$s = str_replace($x,'',$s); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/**///remove remaining tags |
325
|
|
View Code Duplication |
while(stripos($s,'<') > 0){ |
|
|
|
|
326
|
|
|
$pos[1] = stripos($s,'<'); |
327
|
|
|
$pos[2] = stripos($s,'>', $pos[1]); |
328
|
|
|
$len[1] = $pos[2] - $pos[1] + 1; |
329
|
|
|
$x = substr($s,$pos[1],$len[1]); |
330
|
|
|
$s = str_replace($x,'',$s); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/**///finalize keep tag |
334
|
|
|
if (isset($k)) { |
335
|
|
View Code Duplication |
for($i=0;$i<count($k);$i++){ |
|
|
|
|
336
|
|
|
$s = str_replace('[{(' . $k[$i],'<' . $k[$i],$s); |
337
|
|
|
$s = str_replace('[{(/' . $k[$i],'</' . $k[$i],$s); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return trim($s); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
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.