-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLetter.php
326 lines (275 loc) · 7.49 KB
/
Letter.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
<?php
/**
* Letter.php
* @author Roman Revin http://phptime.ru
*/
namespace rmrevin\yii\postman;
use PHPMailer;
use rmrevin\yii\postman\models\LetterModel;
use yii\base\Event;
use yii\db\Expression;
use yii\helpers\Json;
/**
* Class Letter
* The abstract class that implements the basic functionality of letters;
*
* @package rmrevin\yii\postman
*/
abstract class Letter extends \yii\base\Component
{
/** @var PHPMailer object */
protected $_mailer = null;
/** @var string a random code */
protected $code;
/** @var string a subject */
protected $raw_subject;
/** @var string a subject */
protected $subject;
/** @var string a body of a message */
protected $body;
/** @var array recipients */
protected $recipients = [];
/** @var array attachments */
protected $attachments;
/** @var string a last error of a message */
private $_error = null;
/** @var Component */
private $Postman;
/** the name of the event that occurs before sending emails */
const EVENT_BEFORE_SEND = 'beforeSend';
/** the name of the event that occurs after sending emails */
const EVENT_AFTER_SEND = 'afterSend';
/**
* @inheritdoc
*/
public function __construct($config = [])
{
parent::__construct($config);
if (!\Yii::$app->has(Component::COMPONENT)) {
throw new LetterException(\Yii::t('app', 'You need to configure the component `{component}`.', [
'component' => Component::COMPONENT,
]));
}
$this->Postman = \rmrevin\yii\postman\Component::get();
$this->setFrom($this->Postman->default_from);
$this->code = \Yii::$app->getSecurity()->generateRandomString();
}
/**
* the method sets the "subject"
* @param string $subject
* @return static
*/
public function setSubject($subject)
{
$this->raw_subject = (string)$subject;
$this->subject = (string)$this->Postman->subject_prefix . (string)$subject . (string)$this->Postman->subject_suffix;
return $this;
}
/**
* the method sets the "body"
* @param string $body
* @return static
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* the method sets the value of the "From" field
* @param array $from = ['[email protected]'] || ['[email protected]', 'John Smith']
* @return static
*/
public function setFrom($from)
{
$this->recipients['from'] = $from;
return $this;
}
/**
* @return array
*/
public function getRecipients()
{
return $this->recipients;
}
/**
* @return int
*/
public function getCountRecipients()
{
$count = 1; // one is "from"
foreach ($this->recipients as $type => $recipients) {
if ($type === 'from') {
continue;
}
$count += count($recipients);
}
return $count;
}
/**
* the method adds a recipient
* @param array $address = ['[email protected]'] || ['[email protected]', 'John Smith']
* @return static
*/
public function addAddress($address)
{
$args = func_get_args();
foreach ($args as $address) {
$this->_addAddr('to', $address);
}
return $this;
}
/**
* the method adds a recipient to Cc
* @param array $address = ['[email protected]'] || ['[email protected]', 'John Smith']
*
* @return static
*/
public function addCcAddress($address)
{
$args = func_get_args();
foreach ($args as $address) {
$this->_addAddr('cc', $address);
}
return $this;
}
/**
* the method adds a recipient to Bcc
* @param array $address = ['[email protected]'] || ['[email protected]', 'John Smith']
*
* @return static
*/
public function addBccAddress($address)
{
$args = func_get_args();
foreach ($args as $address) {
$this->_addAddr('bcc', $address);
}
return $this;
}
/**
* the method adds a "Reply-to" address
* @param array $address = ['[email protected]'] || ['[email protected]', 'John Smith']
*
* @return static
*/
public function addReplyTo($address)
{
$args = func_get_args();
foreach ($args as $address) {
$this->_addAddr('reply', $address);
}
return $this;
}
/**
* the method adds a recipient by type
* @param $type
* @param $address
*
* @return static
*/
private function _addAddr($type, $address)
{
$address = !is_array($address) ? [$address] : $address;
if (!isset($this->recipients[$type])) {
$this->recipients[$type] = [];
}
$this->recipients[$type][] = $address;
return $this;
}
/**
* the method adds an attachment
* @param string $path
* @param string $name
* @param string $encoding
* @param string $type
*
* @return static
*/
public function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream')
{
$this->attachments[] = [
'path' => $path,
'name' => $name,
'encoding' => $encoding,
'type' => $type
];
return $this;
}
/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* the method sends a letter
* @param bool $immediately
*
* @return bool
*/
public function send($immediately = false)
{
$this->beforeSend();
$LetterModel = $this->_dataToModel();
$LetterModel->code = $this->code;
$LetterModel->date_create = new Expression('NOW()');
if ($LetterModel->validate()) {
$result = $LetterModel->save();
if ($immediately === true) {
$LetterModel
->setMailer(Component::get()->getCloneMailerObject())
->sendImmediately();
}
$this->_error = $LetterModel->getLastError();
} else {
$result = false;
$firstErrors = $LetterModel->getFirstErrors();
$this->_error = array_shift($firstErrors);
}
$this->afterSend();
return $result === true ? (int)$LetterModel->id : false;
}
/**
* the method gets the message about the last error
*
* @return null|string
*/
public function getLastError()
{
return $this->_error;
}
/**
* the method converts the letter data to the letter model
*
* @return LetterModel
*/
private function _dataToModel()
{
$LetterModel = new LetterModel();
$LetterModel->recipients = Json::encode($this->recipients);
$LetterModel->subject = $this->subject;
$LetterModel->body = $this->body;
$LetterModel->attachments = Json::encode($this->attachments);
return $LetterModel;
}
/**
* before send event
*/
public function beforeSend()
{
$Event = new Event();
$Event->sender = $this;
$this->trigger(self::EVENT_BEFORE_SEND, $Event);
}
/**
* after send event
*/
public function afterSend()
{
$Event = new Event();
$Event->sender = $this;
$this->trigger(self::EVENT_AFTER_SEND, $Event);
}
}