forked from cBackup/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomMailer.php
249 lines (191 loc) · 7.04 KB
/
CustomMailer.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
<?php
/**
* This file is part of cBackup, network equipment configuration backup tool
* Copyright (C) 2017, Oļegs Čapligins, Imants Černovs, Dmitrijs Galočkins
*
* cBackup is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace app\mailer;
use yii\helpers\Inflector;
use app\models\MailerEventsTasks;
use app\models\MailerEvents;
use toriphes\console\Runner;
use cbackup\console\ConsoleRunner;
/**
* @package app\mailer
*/
class CustomMailer
{
/**
* Class which contains mailer methods
*
* @var string
*/
private $mailer_methods = 'app\mailer\MailerMethods';
/**
* @var array
*/
private $template_params = [];
/**
* Create and send new Mail
*
* @param string $task_name
* @param bool $background
* @return bool|string
* @throws \yii\db\Exception
*/
public function sendMail($task_name, $background = false)
{
/** Stop processing script if mailer is disabled in system settings */
if (\Y::param('mailer') == 0) {
\Yii::error(["Mailer is disabled. To use mailer please enable it in 'System settings'", null, 'SEND MAIL'], 'mailer.writeLog');
return false;
}
/** Create new event task */
$task_id = $this->createEventTask($task_name);
/** Run event task */
if ($task_id != false) {
$command = "mailer/send {$task_id}";
if(!$background) {
$output = '';
$runner = new Runner();
/** @noinspection PhpParamsInspection */
$runner->run($command, $output);
return $output;
} else {
$console = new ConsoleRunner(['file' => '@app/yii']);
$console->run($command);
return null;
}
}
return false;
}
/**
* Send test mail
*
* @return bool|string
*/
public function sendTestMail()
{
$output = '';
$runner = new Runner();
/** @noinspection PhpParamsInspection */
$runner->run('mailer/send-test-mail', $output);
return $output;
}
/**
* Create event task
* Return event task id on success, false on error
*
* @param string $task_name
* @return bool|int
* @throws \yii\db\Exception
*/
private function createEventTask($task_name)
{
$event = MailerEvents::findOne($task_name);
if (!is_null($event)) {
$event_task = new MailerEventsTasks();
$transaction = \Yii::$app->db->beginTransaction();
try {
/** Set event task attributes*/
$event_task->event_name = $event->name;
$event_task->subject = $event->subject;
$event_task->body = $this->generateMailBody($event->template);
/** Save event task */
if ($event_task->save()) {
$transaction->commit();
\Yii::info(["Event task with ID {$event_task->id} created", $event_task->id, 'CREATE'], 'mailer.writeLog');
\Yii::getLogger()->flush(true);
return $event_task->id;
} else {
$transaction->rollBack();
\Yii::error(["An error occurred while creating new event task", null, 'CREATE'], 'mailer.writeLog');
}
} catch (\Exception $e) {
$transaction->rollBack();
\Yii::error(["An error occurred while creating new event task.\nException:\n{$e->getMessage()}", null, 'CREATE'], 'mailer.writeLog');
}
} else {
\Yii::error(["Event with name `{$task_name}` does not exist", null, 'CREATE'], 'mailer.writeLog');
}
return false;
}
/**
* Generate email body
*
* @param string $event_template
* @return string
*/
public function generateMailBody($event_template)
{
$result = '';
if (!empty($event_template) && !is_null($event_template)) {
$templ_vars = $this->parseTemplateParams($event_template)->prepareVariables();
$result = strtr($event_template, $templ_vars);
}
return $result;
}
/**
* Parse user created mail template
*
* @param string $template
* @return $this
*/
private function parseTemplateParams($template)
{
if (!empty($template) && !is_null($template)) {
/** Find all template params */
preg_match_all('/{{.*?}}/i', $template, $variables);
foreach ($variables[0] as $template_var) {
preg_match_all('/(?<={{)\w+|:.*?(?=>)|>.*?(?=}})/', $template_var, $var_params);
foreach ($var_params[0] as $key => $value) {
/** Get method name in camelcase */
$this->template_params[$template_var]['method'] = 'get' . Inflector::camelize(strtolower($var_params[0][0]));
/** Get all necessary arguments from string */
if (strpos($var_params[0][$key], ':') !== false) {
$this->template_params[$template_var]['args'] = array_filter(explode(':', $var_params[0][$key]));
}
/** Get all output params from string */
if (strpos($var_params[0][$key], '>') !== false) {
$this->template_params[$template_var]['output'] = array_filter(explode('>', strtolower($var_params[0][$key])));
}
}
}
}
return $this;
}
/**
* Prepare all variables based on user template
*
* @return array|string
*/
private function prepareVariables()
{
$result = [];
try {
foreach ($this->template_params as $key => $value) {
if (method_exists($this->mailer_methods, $value['method'])) {
$reflection = new \ReflectionMethod($this->mailer_methods, $value['method']);
$args = (array_key_exists('args', $value)) ? $value['args'] : [];
$output = (array_key_exists('output', $value)) ? $value['output'] : [];
$result[$key] = $reflection->invoke(new MailerMethods($args, $output));
}
}
} catch (\Exception $e) {
return $e->getMessage();
}
return $result;
}
}