This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathActiveMessage.php
444 lines (402 loc) · 11.2 KB
/
ActiveMessage.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\activemail;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\helpers\StringHelper;
/**
* ActiveMessage represents particular mail sending process.
* It combines the data and the logic for the particular mail content composition and sending.
*
* For each mail sending event, which appears in the application, the child class of ActiveMessage
* should be created:
*
* ```php
* namespace app\mail\active;
*
* use yii2tech\activemail\ActiveMessage;
* use Yii;
*
* class ContactUs extends ActiveMessage
* {
* public function defaultFrom()
* {
* return Yii::$app->params['applicationEmail'];
* }
*
* public function defaultTo()
* {
* return Yii::$app->params->mail['adminEmail'];
* }
*
* public function defaultSubject()
* {
* return 'Contact message on ' . Yii::$app->name;
* }
*
* public function defaultBodyHtml()
* {
* return 'Contact message';
* }
* }
* ```
*
* Once message created and populated it can be sent via [[send()]] method:
*
* ```php
* use app\mail\active\Notification;
*
* $message = new Notification();
* $message->to = '[email protected]';
* $message->message = 'Notification message';
* $message->send();
* ```
*
* ActiveMessage supports using of the mail templates provided by [[\yii2tech\activemail\TemplateStorage]].
*
* @see yii2tech\activemail\TemplateStorage
*
* @property string|array $from message sender email address.
* @property string|array $replyTo the reply-to address.
* @property array $to message recipients.
* @property string $subject message subject.
* @property string $bodyText message plain text content.
* @property string $bodyHtml message HTML content.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
abstract class ActiveMessage extends Model
{
/**
* @event Event an event that is triggered before message is sent.
*/
const EVENT_BEFORE_SEND = 'beforeSend';
/**
* @var string|array message sender email address.
*/
private $_from;
/**
* @var string|array the reply-to address.
*/
private $_replyTo;
/**
* @var array message recipients.
*/
private $_to;
/**
* @var string message subject.
*/
private $_subject;
/**
* @var string message plain text content.
*/
private $_bodyText;
/**
* @var string message HTML content.
*/
private $_bodyHtml;
/**
* @param string|array $from message sender email address.
*/
public function setFrom($from)
{
$this->_from = $from;
}
/**
* @return string|array message sender email address.
*/
public function getFrom()
{
if (empty($this->_from)) {
$this->_from = $this->defaultFrom();
}
return $this->_from;
}
/**
* @param array|string $replyTo the reply-to address.
*/
public function setReplyTo($replyTo)
{
$this->_replyTo = $replyTo;
}
/**
* @return array|string the reply-to address.
*/
public function getReplyTo()
{
if (empty($this->_replyTo)) {
$this->_replyTo = $this->defaultReplyTo();
}
return $this->_replyTo;
}
/**
* @param array $to message recipients.
*/
public function setTo($to)
{
$this->_to = $to;
}
/**
* @return array message recipients.
*/
public function getTo()
{
if (empty($this->_to)) {
$this->_to = $this->defaultTo();
}
return $this->_to;
}
/**
* @param string $subject message subject.
*/
public function setSubject($subject)
{
$this->_subject = $subject;
}
/**
* @return string message subject.
*/
public function getSubject()
{
if (empty($this->_subject)) {
$this->_subject = $this->defaultSubject();
}
return $this->_subject;
}
/**
* @param string $bodyHtml message HTML content.
*/
public function setBodyHtml($bodyHtml)
{
$this->_bodyHtml = $bodyHtml;
}
/**
* @return string message HTML content.
*/
public function getBodyHtml()
{
if (empty($this->_bodyHtml)) {
$this->_bodyHtml = $this->defaultBodyHtml();
}
return $this->_bodyHtml;
}
/**
* @param string $bodyText message plain text content.
*/
public function setBodyText($bodyText)
{
$this->_bodyText = $bodyText;
}
/**
* @return string message plain text content.
*/
public function getBodyText()
{
if (empty($this->_bodyText)) {
$this->_bodyText = $this->defaultBodyText();
}
return $this->_bodyText;
}
/**
* @return \yii\mail\MailerInterface mailer instance.
*/
public function getMailer()
{
return Yii::$app->getMailer();
}
/**
* @return TemplateStorage template storage instance.
*/
public function getTemplateStorage()
{
return Yii::$app->get('mailTemplateStorage');
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[$this->attributes(), 'required'],
];
}
/**
* @return string default sender
*/
abstract public function defaultFrom();
/**
* @return string the default reply-to address.
*/
public function defaultReplyTo()
{
return $this->getFrom();
}
/**
* @return string default receiver email address.
*/
abstract public function defaultTo();
/**
* @return string default message subject
*/
abstract public function defaultSubject();
/**
* @return string default message HTML content.
*/
abstract public function defaultBodyHtml();
/**
* @return string default message plain text content.
*/
public function defaultBodyText()
{
return 'You need email client with HTML support to view this message.';
}
/**
* @return string message view name.
*/
public function viewName()
{
return '@yii2tech/activemail/views/activeMessage.php';
}
/**
* @return string message template name.
*/
public function templateName()
{
return StringHelper::basename(get_class($this));
}
/**
* Returns the hints for template placeholders.
* Hints are can be used, while composing edit form for the mail template.
* @return array template placeholder hints in format: placeholderName => hint
*/
public function templatePlaceholderHints()
{
return [];
}
/**
* Returns all this model error messages as single summary string.
* @param string $glue messages separator.
* @return string error summary.
*/
public function getErrorSummary($glue = "\n")
{
$errors = $this->getErrors();
$summaryParts = [];
foreach ($errors as $attributeErrors) {
$summaryParts = array_merge($summaryParts, $attributeErrors);
}
return implode($glue, $summaryParts);
}
/**
* Parses template string.
* @param string $template template string.
* @param array $data parsing data.
* @return string parsing result.
*/
protected function parseTemplate($template, array $data = [])
{
$replacePairs = [];
foreach ($data as $name => $value) {
$replacePairs['{' . $name . '}'] = $value;
}
return strtr($template, $replacePairs);
}
/**
* Sends this message
* @param boolean $runValidation whether to perform validation before sending the message.
* @return boolean success.
* @throws InvalidConfigException on failure
*/
public function send($runValidation = true)
{
if ($runValidation && !$this->validate()) {
throw new InvalidConfigException('Unable to send message: ' . $this->getErrorSummary());
}
$data = $this->templatePlaceholders();
//$this->beforeCompose($mailMessage, $data);
$this->applyTemplate();
$this->applyParse($data);
$data['activeMessage'] = $this;
$mailMessage = $this->getMailer()
->compose($this->viewName(), $data)
->setSubject($this->getSubject())
->setTo($this->getTo())
->setFrom($this->getFrom())
->setReplyTo($this->getReplyTo());
if ($this->beforeSend($mailMessage)) {
return $this->getMailer()->send($mailMessage);
} else {
return false;
}
}
/**
* Composes placeholders, which should be used to parse template.
* Those placeholders will also be passed to the mail view, while composition.
* By default this method returns all current message model attributes.
* Child classes may override this method to customize template placeholders.
* @return array template placeholders in format: placeholderName => value.
*/
protected function templatePlaceholders()
{
return $this->getAttributes();
}
/**
* Applies corresponding template to the message if it exist.
*/
protected function applyTemplate()
{
$templateAttributes = $this->getTemplateStorage()->getTemplate($this->templateName());
if (!empty($templateAttributes)) {
foreach ($templateAttributes as $name => $value) {
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} else {
$this->$name = $value;
}
}
}
}
/**
* Applies parsing to this message internal fields.
* @param array $data template parse data.
*/
protected function applyParse(array $data)
{
$propertyNames = [
'subject',
'bodyText',
'bodyHtml',
'bodyHtml',
];
foreach ($propertyNames as $propertyName) {
$getter = 'get' . $propertyName;
$setter = 'set' . $propertyName;
$content = $this->$getter();
$content = $this->parseTemplate($content, $data);
$this->$setter($content);
}
}
// Events :
/**
* This method is invoked before mail message sending.
* The default implementation raises a `beforeSend` event.
* You may override this method to do preliminary checks or adjustments before sending.
* Make sure the parent implementation is invoked so that the event can be raised.
* @param \yii\mail\MessageInterface $mailMessage mail message instance.
* @return boolean whether message should be sent. Defaults to true.
* If false is returned, no message sending will be performed.
*/
protected function beforeSend($mailMessage)
{
$event = new ActiveMessageEvent(['mailMessage' => $mailMessage]);
$this->trigger(self::EVENT_BEFORE_SEND, $event);
return $event->isValid;
}
}