/
Mailer.php
308 lines (281 loc) · 7.01 KB
/
Mailer.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Mailer;
use ArrayAccess;
use Cake\Datasource\ModelAwareTrait;
use Cake\Event\EventListenerInterface;
use Cake\Mailer\Exception\MissingActionException;
use Cake\Utility\Inflector;
/**
* Mailer base class.
*
* Mailer classes let you encapsulate related Email logic into a reusable
* and testable class.
*
* ## Defining Messages
*
* Mailers make it easy for you to define methods that handle email formatting
* logic. For example:
*
* ```
* class UserMailer extends Mailer
* {
* public function resetPassword($user)
* {
* $this->subject = 'Reset Password';
* $this->to = $user->email;
* $this->set(['token' => $user->token]);
* }
* }
* ```
*
* Is a trivial example but shows how a mailer could be declared.
*
* ## Sending Messages
*
* After you have defined some messages you will want to send them:
*
* ```
* $mailer = new UserMailer();
* $mailer->send('resetPassword', $user);
* ```
*
* ## Event Listener
*
* Mailers can also subscribe to application event allowing you to
* decouple email delivery from your application code. By re-declaring the
* `implementedEvents()` method you can define event handlers that can
* convert events into email. For example, if your application had a user
* registation event:
*
* ```
* public function implementedEvents()
* {
* return [
* 'Model.afterSave' => 'onRegistration',
* ];
* }
*
* public function onRegistration(Event $event, Entity $entity, ArrayObject $options)
* {
* if ($entity->isNew()) {
* $this->send('welcome', [$entity]);
* }
* }
* ```
*
* The onRegistration method converts the application event into a mailer method.
* Our mailer could either be registered in the application bootstrap, or
* in the Table class' initialize() hook.
*/
abstract class Mailer implements ArrayAccess, EventListenerInterface
{
use ModelAwareTrait;
/**
* Mailer's name.
*
* @var string
*/
static public $name;
/**
* Layout.
*
* @var string
*/
public $layout;
/**
* Email instance.
*
* @var \Cake\Mailer\Email
*/
protected $_email;
/**
* Constructor.
*
* @param \Cake\Mailer\Email|null $email Email instance.
*/
public function __construct(Email $email = null)
{
if ($email === null) {
$email = new Email();
}
$this->_email = $email;
}
/**
* Returns the mailer's name.
*
* @return string
*/
public function getName()
{
if (!static::$name) {
static::$name = str_replace(
'Mailer',
'',
join('', array_slice(explode('\\', get_class($this)), -1))
);
}
return static::$name;
}
/**
* Sets layout to use. Defaults to configured layout template if a custom layout
* could not be found.
*
* @param string $layout Name of the layout to use.
* @return $this object.
*/
public function layout($layout)
{
$this->layout = $layout;
return $this;
}
/**
* Sets headers.
*
* @param array $headers Headers to set.
* @return $this object.
*/
public function setHeaders(array $headers)
{
$this->_email->setHeaders($headers);
return $this;
}
/**
* Adds headers.
*
* @param array $headers Headers to set.
* @return $this object.
*/
public function addHeaders(array $headers)
{
$this->_email->addHeaders($headers);
return $this;
}
/**
* Sets attachments.
*
* @param string|array $attachments String with the filename or array with filenames
* @return $this object.
* @throws \InvalidArgumentException
*/
public function attachments($attachments)
{
$this->_email->attachments($attachments);
return $this;
}
/**
* Sets email view vars.
*
* @param string|array $key Variable name or hash of view variables.
* @param mixed $value View variable value.
* @return $this object.
*/
public function set($key, $value = null)
{
$this->_email->viewVars(is_string($key) ? [$key => $value] : $key);
return $this;
}
/**
* Sends email.
*
* @param string $action The name of the mailer action to trigger.
* @param array $args Arguments to pass to the triggered mailer action.
* @param array $headers Headers to set.
* @return array
* @throws \Cake\Mailer\Exception\MissingActionException
* @throws \BadMethodCallException
*/
public function send($action, $args = [], $headers = [])
{
if (!method_exists($this, $action)) {
throw new MissingActionException([
'mailer' => $this->getName() . 'Mailer',
'action' => $action,
]);
}
$this->setHeaders($headers);
call_user_func_array([$this, $action], $args);
$result = $this->_email
->profile((array)$this)
->send();
$this->reset();
return $result;
}
/**
* Resets email instance to original config.
*
* @return $this object.
*/
public function reset()
{
$this->_email->reset();
return $this;
}
/**
* Checks if the property exists.
*
* @param string $offset Property name.
* @return bool True if it exists.
*/
public function offsetExists($offset)
{
return property_exists($this, $offset) ||
method_exists($this->_email, $offset);
}
/**
* Gets the property value if it exists.
*
* @param string $offset Property name.
* @return mixed Value.
*/
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
return null;
}
if (isset($this->{$offset})) {
return $this->{$offset};
}
return call_user_func([$this->_email, $offset]);
}
/**
* Sets property's value.
*
* @param string $offset Property name.
* @param mixed $value Value.
* @return void
*/
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
}
/**
* Unset a property.
*
* @param string $offset Property name.
* @return void
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
}
/**
* Implemented events.
*
* @return array
*/
public function implementedEvents()
{
return [];
}
}