1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Core |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Core\Notify; |
17
|
|
|
|
18
|
|
|
use Cake\ORM\Entity; |
19
|
|
|
use Cake\Mailer\Email as CakeEmail; |
20
|
|
|
use Cake\Mailer\Transport\MailTransport; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Email |
24
|
|
|
* |
25
|
|
|
* @package Core\Notify |
26
|
|
|
*/ |
27
|
|
|
class Email |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
const DEFAULT_MSG_TPL = 'Core.message'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Entity object. |
34
|
|
|
* |
35
|
|
|
* @var Entity |
36
|
|
|
*/ |
37
|
|
|
protected $_data; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Message template. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $_tpl; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Server name from env(). |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $_serverName; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $_fromName = 'CMS'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $_fromEmail = '[email protected]'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $_format = 'html'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Mail constructor. |
70
|
|
|
* |
71
|
|
|
* @param Entity|array $data |
72
|
|
|
* @param string $tpl |
73
|
|
|
*/ |
74
|
|
|
public function __construct($data = [], $tpl = self::DEFAULT_MSG_TPL) |
75
|
|
|
{ |
76
|
|
|
if (!($data instanceof Entity)) { |
77
|
|
|
$data = new Entity((array) $data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->_tpl = $tpl; |
81
|
|
|
$this->_data = $data; |
82
|
|
|
$this->_initialize(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Send message method. |
87
|
|
|
* |
88
|
|
|
* @param string $subject |
89
|
|
|
* @param string $content |
90
|
|
|
* @param string|array $to |
91
|
|
|
* @param string $fromName |
92
|
|
|
* @param string $fromEmail |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function send($subject, $content, $to, $fromName = '', $fromEmail = '') |
96
|
|
|
{ |
97
|
|
|
$mail = new CakeEmail(); |
98
|
|
|
$fromName = ($fromName !== '') ? $fromName : $this->_fromName; |
99
|
|
|
$fromEmail = ($fromEmail !== '') ? $fromEmail : $this->_fromEmail; |
100
|
|
|
$transport = new MailTransport(); |
101
|
|
|
|
102
|
|
|
$mail->viewBuilder()->setTemplate($this->_tpl); |
103
|
|
|
|
104
|
|
|
return $mail |
105
|
|
|
->setTransport($transport) |
106
|
|
|
->setEmailFormat($this->_format) |
107
|
|
|
->setFrom($fromEmail, $fromName) |
108
|
|
|
->setTo($to) |
109
|
|
|
->setSubject($subject) |
110
|
|
|
->setViewVars(['page_title' => $subject]) |
111
|
|
|
->send($content); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Constructor hook method. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
protected function _initialize() |
120
|
|
|
{ |
121
|
|
|
$this->_serverName = preg_replace('#^www\.#', '', mb_strtolower(env('SERVER_NAME'))); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|