15
15
* @property array $body
16
16
* @property array $attachments
17
17
* @property array $address
18
+ * @property array $templates
18
19
* @property string $log
19
20
* @property boolean $smtp
20
21
* @property resource $socket
@@ -23,7 +24,7 @@ class Mailer {
23
24
/**
24
25
* Version of Mailer
25
26
*/
26
- const MAILER_VERSION = '2.1 .0 ' ;
27
+ const MAILER_VERSION = '2.2 .0 ' ;
27
28
28
29
/**
29
30
* End of line symbol
@@ -72,6 +73,13 @@ class Mailer {
72
73
*/
73
74
protected $ address ;
74
75
76
+ /**
77
+ * Templates.
78
+ *
79
+ * @var array
80
+ */
81
+ protected $ templates ;
82
+
75
83
/**
76
84
* SMTP Socket.
77
85
*
@@ -202,7 +210,7 @@ public function subject($subject)
202
210
}
203
211
204
212
/**
205
- * Set body of message.
213
+ * Set body of message from string .
206
214
*
207
215
* @param string $text
208
216
* @void
@@ -217,6 +225,38 @@ public function body($text)
217
225
];
218
226
}
219
227
228
+ /**
229
+ * Set body of message from template.
230
+ *
231
+ * @param string $key
232
+ * @param array $context
233
+ * @void
234
+ */
235
+ public function template ($ key ,$ context )
236
+ {
237
+ $ key = (string )$ key ;
238
+ $ context = (array )$ context ;
239
+ if (empty ($ this ->templates [$ key ]['type ' ])) return ;
240
+ $ text = '' ;
241
+ switch ($ this ->templates [$ key ]['type ' ]) {
242
+ case 'string ' :
243
+ $ text = (string )$ this ->templates [$ key ]['tmpl ' ];
244
+ break ;
245
+ case 'file ' :
246
+ $ text = is_readable ($ this ->templates [$ key ]['tmpl ' ])?file_get_contents ($ this ->templates [$ key ]['tmpl ' ]):'' ;
247
+ break ;
248
+ }
249
+ foreach ($ context as $ placeholder =>$ value ) {
250
+ $ text = str_replace ('{ ' .$ placeholder .'} ' , $ value , $ text );
251
+ }
252
+ $ this ->body = [
253
+ 'headers ' => [
254
+ 'Content-Type ' => 'text/html; charset=utf8 ' ,
255
+ ],
256
+ 'content ' => $ text ,
257
+ ];
258
+ }
259
+
220
260
/**
221
261
* Add attachment from string.
222
262
*
@@ -306,6 +346,44 @@ public function getLog()
306
346
return $ this ->log ;
307
347
}
308
348
349
+ /**
350
+ * Add template from string
351
+ *
352
+ * @param string $key
353
+ * @param string $template
354
+ * @void
355
+ */
356
+ public function addTemplateFromString ($ key ,$ template )
357
+ {
358
+ $ key = (string )$ key ;
359
+ $ template = (string )$ template ;
360
+ if ($ key && $ template ) {
361
+ $ this ->templates [$ key ] = [
362
+ 'tmpl ' => $ template ,
363
+ 'type ' => 'string ' ,
364
+ ];
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Add template from file
370
+ *
371
+ * @param string $key
372
+ * @param string $template
373
+ * @void
374
+ */
375
+ public function addTemplateFromFile ($ key ,$ template )
376
+ {
377
+ $ key = (string )$ key ;
378
+ $ template = (string )$ template ;
379
+ if ($ key && is_readable ($ template )) {
380
+ $ this ->templates [$ key ] = [
381
+ 'tmpl ' => $ template ,
382
+ 'type ' => 'file ' ,
383
+ ];
384
+ }
385
+ }
386
+
309
387
/**
310
388
* Reset body and headers for nex mail
311
389
* @void
0 commit comments