Skip to content

Commit df4c77b

Browse files
committed
version 2.2.0
1 parent 6260ed1 commit df4c77b

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[![Latest Stable Version](https://poser.pugx.org/ddrv/mailer/v/stable?format=flat-square)](https://packagist.org/packages/ddrv/mailer)
2+
[![Total Downloads](https://poser.pugx.org/ddrv/mailer/downloads?format=flat-square)](https://packagist.org/packages/ddrv/mailer)
3+
[![License](https://poser.pugx.org/ddrv/mailer/license?format=flat-square)](https://packagist.org/packages/ddrv/mailer)
4+
15
# Mailer
26
PHP Class for sending email.
37

@@ -73,4 +77,40 @@ $mailer->removeAddress('[email protected]');
7377
* Send email to addresses
7478
*/
7579
$mailer->send();
76-
```
80+
```
81+
82+
## Templates
83+
84+
Content of file /path/to/template.tmpl:
85+
```text
86+
<h1>This template</h1>
87+
<p>Hello, {username}!</p>
88+
<p>This message generated automatically.</p>
89+
<p>{date}</p>
90+
```
91+
92+
PHP code:
93+
```php
94+
/**
95+
* You can using templates for your mails
96+
*/
97+
98+
/**
99+
* Add template from string
100+
*/
101+
$mailer->addTemplateFromString('tmpl1', '<p>Hello, {username}</p>');
102+
103+
/**
104+
* Add template from file
105+
*/
106+
$mailer->addTemplateFromFile('tmpl2', '/path/to/template.tmpl');
107+
108+
/**
109+
* Set body from template
110+
*/
111+
$context = [
112+
'username' => 'Anonymous',
113+
'date' => date('Y-m-d'),
114+
];
115+
$mailer->template('tmpl2',$context);
116+
```

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ddrv/mailer",
3-
"version":"2.1.0",
3+
"version":"2.2.0",
44
"require":{
55
"php":">=5.4.0"
66
},

src/Mailer.php

+80-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @property array $body
1616
* @property array $attachments
1717
* @property array $address
18+
* @property array $templates
1819
* @property string $log
1920
* @property boolean $smtp
2021
* @property resource $socket
@@ -23,7 +24,7 @@ class Mailer {
2324
/**
2425
* Version of Mailer
2526
*/
26-
const MAILER_VERSION = '2.1.0';
27+
const MAILER_VERSION = '2.2.0';
2728

2829
/**
2930
* End of line symbol
@@ -72,6 +73,13 @@ class Mailer {
7273
*/
7374
protected $address;
7475

76+
/**
77+
* Templates.
78+
*
79+
* @var array
80+
*/
81+
protected $templates;
82+
7583
/**
7684
* SMTP Socket.
7785
*
@@ -202,7 +210,7 @@ public function subject($subject)
202210
}
203211

204212
/**
205-
* Set body of message.
213+
* Set body of message from string.
206214
*
207215
* @param string $text
208216
* @void
@@ -217,6 +225,38 @@ public function body($text)
217225
];
218226
}
219227

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+
220260
/**
221261
* Add attachment from string.
222262
*
@@ -306,6 +346,44 @@ public function getLog()
306346
return $this->log;
307347
}
308348

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+
309387
/**
310388
* Reset body and headers for nex mail
311389
* @void

0 commit comments

Comments
 (0)