Completed
Push — master ( 16dcd4...314e3c )
by Cheren
03:50 queued 01:12
created

Email::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 5
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
21
/**
22
 * Class Email
23
 *
24
 * @package Core\Notify
25
 */
26
class Email
27
{
28
29
    const DEFAULT_MSG_TPL = 'Core.message';
30
31
    /**
32
     * Entity object.
33
     *
34
     * @var Entity
35
     */
36
    protected $_data;
37
38
    /**
39
     * Message template.
40
     *
41
     * @var string
42
     */
43
    protected $_tpl;
44
45
    /**
46
     * Server name from env().
47
     *
48
     * @var string
49
     */
50
    protected $_serverName;
51
52
    /**
53
     * @var string
54
     */
55
    protected $_fromName = 'CMS';
56
57
    /**
58
     * @var string
59
     */
60
    protected $_fromEmail = '[email protected]';
61
62
    /**
63
     * @var string
64
     */
65
    protected $_format = 'html';
66
67
    /**
68
     * Mail constructor.
69
     *
70
     * @param Entity|array $data
71
     * @param string $tpl
72
     */
73
    public function __construct($data = [], $tpl = self::DEFAULT_MSG_TPL)
74
    {
75
        if (!($data instanceof Entity)) {
76
            $data = new Entity((array) $data);
77
        }
78
79
        $this->_tpl  = $tpl;
80
        $this->_data = $data;
81
        $this->_initialize();
82
    }
83
84
    /**
85
     * Send message method.
86
     *
87
     * @param string $subject
88
     * @param string $content
89
     * @param string|array $to
90
     * @param string $fromName
91
     * @param string $fromEmail
92
     * @return array
93
     */
94
    public function send($subject, $content, $to, $fromName = '', $fromEmail = '')
95
    {
96
        $mail      = new CakeEmail();
97
        $fromName  = ($fromName !== '') ? $fromName : $this->_fromName;
98
        $fromEmail = ($fromEmail !== '') ? $fromEmail : $this->_fromEmail;
99
100
        return $mail
101
            ->setTemplate($this->_tpl)
102
            ->setEmailFormat($this->_format)
103
            ->setFrom($fromEmail, $fromName)
104
            ->setTo($to)
105
            ->setSubject($subject)
106
            ->setViewVars(['page_title' => $subject])
107
            ->send($content);
108
    }
109
110
    /**
111
     * Constructor hook method.
112
     *
113
     * @return void
114
     */
115
    protected function _initialize()
116
    {
117
        $this->_serverName = preg_replace('#^www\.#', '', mb_strtolower(env('SERVER_NAME')));
118
    }
119
}
120