1
|
|
|
<?php namespace Helpers; |
2
|
|
|
|
3
|
|
|
include_once(MODX_BASE_PATH . 'assets/lib/APIHelpers.class.php'); |
4
|
|
|
include_once(MODX_BASE_PATH . 'assets/lib/Helpers/FS.php'); |
5
|
|
|
include_once(MODX_MANAGER_PATH . 'includes/extenders/modxmailer.class.inc.php'); |
6
|
|
|
|
7
|
|
|
use MODxMailer; |
|
|
|
|
8
|
|
|
use DocumentParser; |
9
|
|
|
use PHPMailer\PHPMailer\Exception as phpmailerException; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Mailer |
13
|
|
|
* @package Helpers |
14
|
|
|
*/ |
15
|
|
|
class Mailer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var MODxMailer $mail |
19
|
|
|
*/ |
20
|
|
|
protected $mail = null; |
21
|
|
|
protected $modx = null; |
22
|
|
|
public $config = array(); |
23
|
|
|
protected $debug = false; |
24
|
|
|
protected $queuePath = 'assets/cache/mail/'; |
25
|
|
|
protected $noemail = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $Body = ''; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $Subject = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Mailer constructor. |
38
|
|
|
* @param DocumentParser $modx |
39
|
|
|
* @param $cfg |
40
|
|
|
* @param bool $debug |
41
|
|
|
*/ |
42
|
|
|
public function __construct(DocumentParser $modx, $cfg, $debug = false) |
43
|
|
|
{ |
44
|
|
|
$this->modx = $modx; |
45
|
|
|
$this->noemail = (bool)(isset($cfg['noemail']) ? $cfg['noemail'] : 0); |
46
|
|
|
if (!$this->noemail) { |
47
|
|
|
$this->mail = new MODxMailer(); |
48
|
|
|
if (method_exists('MODxMailer', 'init')) { |
49
|
|
|
$this->mail->init($modx); |
50
|
|
|
} |
51
|
|
|
$this->config = $cfg; |
52
|
|
|
$this->debug = $debug; |
53
|
|
|
$this->applyMailConfig(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $type |
59
|
|
|
* @param string $addr |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function addAddressToMailer($type, $addr) |
63
|
|
|
{ |
64
|
|
|
if (!$this->noemail && !empty($addr)) { |
65
|
|
|
$a = array_filter(array_map('trim', explode(',', $addr))); |
66
|
|
|
foreach ($a as $address) { |
67
|
|
|
switch ($type) { |
68
|
|
|
case 'to': |
69
|
|
|
$this->mail->AddAddress($address); |
70
|
|
|
break; |
71
|
|
|
case 'cc': |
72
|
|
|
$this->mail->AddCC($address); |
73
|
|
|
break; |
74
|
|
|
case 'bcc': |
75
|
|
|
$this->mail->AddBCC($address); |
76
|
|
|
break; |
77
|
|
|
case 'replyTo': |
78
|
|
|
$this->mail->AddReplyTo($address); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $filelist |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function attachFiles($filelist = array()) |
91
|
|
|
{ |
92
|
|
|
if (!$this->noemail) { |
93
|
|
|
$fs = FS::getInstance(); |
|
|
|
|
94
|
|
|
$contentType = "application/octetstream"; |
95
|
|
|
foreach ($filelist as $file) { |
96
|
|
|
if (is_file($file['filepath']) && is_readable($file['filepath'])) { |
97
|
|
|
$this->mail->AddAttachment($file['filepath'], $file['filename'], "base64", $contentType); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $report |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function send($report) |
110
|
|
|
{ |
111
|
|
|
//если отправлять некуда или незачем, то делаем вид, что отправили |
112
|
|
|
if (!$this->getCFGDef('to') || $this->noemail) { |
113
|
|
|
return true; |
114
|
|
|
} elseif (empty($report)) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->mail->Body = $this->getCFGDef('isHtml',1) ? $this->mail->msgHTML($report, MODX_BASE_PATH) : $report; |
119
|
|
|
|
120
|
|
|
$result = $this->mail->send(); |
121
|
|
|
if ($result) { |
122
|
|
|
$this->mail->ClearAllRecipients(); |
123
|
|
|
$this->mail->ClearAttachments(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $report |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function toQueue($report) |
134
|
|
|
{ |
135
|
|
|
//если отправлять некуда или незачем, то делаем вид, что отправили |
136
|
|
|
if (!$this->getCFGDef('to') || $this->noemail) { |
137
|
|
|
return true; |
138
|
|
|
} elseif (empty($report)) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->mail->Body = $this->getCFGDef('isHtml',1) ? $this->mail->msgHTML($report, MODX_BASE_PATH) : $report; |
143
|
|
|
|
144
|
|
|
$this->Body = $this->modx->removeSanitizeSeed($this->mail->Body); |
145
|
|
|
$this->Subject = $this->modx->removeSanitizeSeed($this->mail->Subject); |
146
|
|
|
try { |
147
|
|
|
$result = $this->mail->preSend() && $this->saveMessage(); |
148
|
|
|
} catch (phpmailerException $e) { |
149
|
|
|
$this->mail->SetError($e->getMessage()); |
150
|
|
|
|
151
|
|
|
$result = false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($result) { |
155
|
|
|
$this->mail->ClearAllRecipients(); |
156
|
|
|
$this->mail->ClearAttachments(); |
157
|
|
|
$result = $this->getFileName(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $path |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function setQueuePath($path = '') { |
168
|
|
|
if (!empty($path)) { |
169
|
|
|
$this->queuePath = $path; |
170
|
|
|
return true; |
171
|
|
|
} else { |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
protected function saveMessage() |
180
|
|
|
{ |
181
|
|
|
$data = serialize(array( |
182
|
|
|
"header" => $this->mail->getMIMEHeader(), |
183
|
|
|
"body" => $this->mail->getMIMEBody(), |
184
|
|
|
"config" => $this->config |
185
|
|
|
)); |
186
|
|
|
$file = $this->getFileName(); |
187
|
|
|
FS::getInstance()->makeDir($this->queuePath); |
188
|
|
|
$result = @file_put_contents(MODX_BASE_PATH . $this->queuePath . $file, $data) !== false; |
189
|
|
|
if ($result) { |
|
|
|
|
190
|
|
|
$result = $file; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected function getFileName() { |
200
|
|
|
return $this->mail->getMessageID() . '.eml'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $file |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function fromQueue($file) |
208
|
|
|
{ |
209
|
|
|
$result = false; |
210
|
|
|
$dir = MODX_BASE_PATH . $this->queuePath; |
211
|
|
|
$fs = FS::getInstance(); |
212
|
|
|
if ($fs->checkFile($dir)) { |
213
|
|
|
$message = unserialize(file_get_contents($dir . $file)); |
214
|
|
|
$this->config = $message['config']; |
215
|
|
|
$this->applyMailConfig(); |
216
|
|
|
$this->mail->setMIMEHeader($message['header'])->setMIMEBody($message['body']); |
217
|
|
|
unset($message); |
218
|
|
|
$result = $this->mail->postSend(); |
219
|
|
|
if ($result) { |
220
|
|
|
$this->mail->setMIMEBody()->setMIMEHeader(); |
221
|
|
|
FS::getInstance()->unlink($dir . $file); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $result; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
protected function applyMailConfig() |
232
|
|
|
{ |
233
|
|
|
$this->mail->IsHTML($this->getCFGDef('isHtml', 1)); |
234
|
|
|
$this->mail->From = $this->getCFGDef('from', $this->modx->config['emailsender']); |
235
|
|
|
$this->mail->FromName = $this->getCFGDef('fromName', $this->modx->config['site_name']); |
236
|
|
|
$this->mail->Subject = $this->getCFGDef('subject'); |
237
|
|
|
$this->addAddressToMailer("replyTo", $this->getCFGDef('replyTo')); |
238
|
|
|
$this->addAddressToMailer("to", $this->getCFGDef('to')); |
239
|
|
|
$this->addAddressToMailer("cc", $this->getCFGDef('cc')); |
240
|
|
|
$this->addAddressToMailer("bcc", $this->getCFGDef('bcc')); |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param string $param |
247
|
|
|
* @param mixed $default |
248
|
|
|
* @return mixed |
249
|
|
|
*/ |
250
|
|
|
public function getCFGDef($param, $default = null) |
251
|
|
|
{ |
252
|
|
|
return \APIhelpers::getkey($this->config, $param, $default); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths