1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mail;
use yii\base\Component;
use yii\base\InvalidConfigException;
use Yii;
/**
* BaseMailer provides the basic interface for the email mailer application component.
* It provides the default configuration for the email messages.
* Particular implementation of mailer should provide implementation for the [[send()]] method.
*
* @see BaseMessage
*
* @property \yii\base\View|array $view view instance or its array configuration.
* @property \yii\mail\ViewResolver|array $viewResolver view resolver instance or its array configuration.
* @property array $defaultMessageConfig configuration, which should be applied by default to any
* new created email message instance.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
abstract class BaseMailer extends Component
{
/**
* @var \yii\base\View|array view instance or its array configuration.
*/
private $_view = [];
/**
* @var \yii\mail\ViewResolver|array view resolver instance or its array configuration.
*/
private $_viewResolver = [];
/**
* @var array configuration, which should be applied by default to any new created
* email message instance.
* For example:
* ~~~
* array(
* 'encoding' => 'UTF-8',
* 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com',
* )
* ~~~
*/
private $_defaultMessageConfig = [];
/**
* @param array|\yii\base\View $view view instance or its array configuration.
* @throws \yii\base\InvalidConfigException on invalid argument.
*/
public function setView($view)
{
if (!is_array($view) && !is_object($view)) {
throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or array, "' . gettype($view) . '" given.');
}
$this->_view = $view;
}
/**
* @return \yii\base\View view instance.
*/
public function getView()
{
if (!is_object($this->_view)) {
$this->_view = $this->createView($this->_view);
}
return $this->_view;
}
/**
* @param array|\yii\mail\ViewResolver $viewResolver view resolver instance or its array configuration.
* @throws \yii\base\InvalidConfigException on invalid argument.
*/
public function setViewResolver($viewResolver)
{
if (!is_array($viewResolver) && !is_object($viewResolver)) {
throw new InvalidConfigException('"' . get_class($this) . '::viewResolver" should be either object or array, "' . gettype($viewResolver) . '" given.');
}
$this->_viewResolver = $viewResolver;
}
/**
* @return \yii\mail\ViewResolver view resolver.
*/
public function getViewResolver()
{
if (!is_object($this->_viewResolver)) {
$this->_viewResolver = $this->createViewResolver($this->_viewResolver);
}
return $this->_viewResolver;
}
/**
* @param array $defaultMessageConfig default message config
*/
public function setDefaultMessageConfig(array $defaultMessageConfig)
{
$this->_defaultMessageConfig = $defaultMessageConfig;
}
/**
* @return array default message config
*/
public function getDefaultMessageConfig()
{
return $this->_defaultMessageConfig;
}
/**
* Creates view instance from given configuration.
* @param array $config view configuration.
* @return \yii\base\View view instance.
*/
protected function createView(array $config)
{
if (!array_key_exists('class', $config)) {
$config['class'] = '\yii\base\View';
}
return Yii::createObject($config);
}
/**
* Creates view resolver instance from given configuration.
* @param array $config view resolver configuration.
* @return \yii\mail\ViewResolver view resolver instance.
*/
protected function createViewResolver(array $config)
{
if (!array_key_exists('class', $config)) {
$config['class'] = '\yii\mail\ViewResolver';
}
return Yii::createObject($config);
}
/**
* Sends the given email message.
* @param object $message email message instance
* @return boolean whether the message has been sent.
*/
abstract public function send($message);
/**
* Sends a couple of messages at once.
* Note: some particular mailers may benefit from sending messages as batch,
* saving resources, for example on open/close connection operations,
* they may override this method to create their specific implementation.
* @param array $messages list of email messages, which should be sent.
* @return integer number of successfull sends
*/
public function sendMultiple(array $messages) {
$successCount = 0;
foreach ($messages as $message) {
if ($this->send($message)) {
$successCount++;
}
}
return $successCount;
}
/**
* Renders a view.
* @param string $view the view name or the path alias of the view file.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return string string the rendering result
*/
public function render($view, $params = [])
{
return $this->getView()->renderFile($this->getViewResolver()->findViewFile($view), $params, $this);
}
}