Completed
Push — master ( 8cf846...d72f2e )
by
unknown
02:11
created
assets/lib/Helpers/Mailer.php 1 patch
Braces   +43 added lines, -41 removed lines patch added patch discarded remove patch
@@ -7,8 +7,8 @@  discard block
 block discarded – undo
7 7
  * Class Mailer
8 8
  * @package Helpers
9 9
  */
10
-class Mailer
11
-{
10
+class Mailer
11
+{
12 12
     /**
13 13
      * @var \PHPMailer $mail
14 14
      */
@@ -24,11 +24,11 @@  discard block
 block discarded – undo
24 24
      * @param $cfg
25 25
      * @param bool $debug
26 26
      */
27
-    public function __construct(\DocumentParser $modx, $cfg, $debug = false)
28
-    {
27
+    public function __construct(\DocumentParser $modx, $cfg, $debug = false)
28
+    {
29 29
         $this->modx = $modx;
30 30
         $this->mail = new \MODxMailer();
31
-        if (method_exists('\MODxMailer', 'init')) {
31
+        if (method_exists('\MODxMailer', 'init')) {
32 32
             $this->mail->init($modx);
33 33
         }
34 34
         $this->config = $cfg;
@@ -41,12 +41,12 @@  discard block
 block discarded – undo
41 41
      * @param string $addr
42 42
      * @return $this
43 43
      */
44
-    public function addAddressToMailer($type, $addr)
45
-    {
46
-        if (!empty($addr)) {
44
+    public function addAddressToMailer($type, $addr)
45
+    {
46
+        if (!empty($addr)) {
47 47
             $a = array_filter(array_map('trim', explode(',', $addr)));
48
-            foreach ($a as $address) {
49
-                switch ($type) {
48
+            foreach ($a as $address) {
49
+                switch ($type) {
50 50
                     case 'to':
51 51
                         $this->mail->AddAddress($address);
52 52
                         break;
@@ -69,10 +69,10 @@  discard block
 block discarded – undo
69 69
      * @param array $filelist
70 70
      * @return $this
71 71
      */
72
-    public function attachFiles($filelist = array())
73
-    {
72
+    public function attachFiles($filelist = array())
73
+    {
74 74
         $contentType = "application/octetstream";
75
-        foreach ($filelist as $file) {
75
+        foreach ($filelist as $file) {
76 76
             $this->mail->AddAttachment($file['filepath'], $file['filename'], "base64", $contentType);
77 77
         }
78 78
 
@@ -83,19 +83,19 @@  discard block
 block discarded – undo
83 83
      * @param $report
84 84
      * @return bool
85 85
      */
86
-    public function send($report)
87
-    {
86
+    public function send($report)
87
+    {
88 88
         //если отправлять некуда или незачем, то делаем вид, что отправили
89
-        if (!$this->getCFGDef('to') || $this->getCFGDef('noemail')) {
89
+        if (!$this->getCFGDef('to') || $this->getCFGDef('noemail')) {
90 90
             return true;
91
-        } elseif (empty($report)) {
91
+        } elseif (empty($report)) {
92 92
             return false;
93 93
         }
94 94
 
95 95
         $this->mail->Body = $report;
96 96
 
97 97
         $result = $this->mail->send();
98
-        if ($result) {
98
+        if ($result) {
99 99
             $this->mail->ClearAllRecipients();
100 100
             $this->mail->ClearAttachments();
101 101
         }
@@ -107,12 +107,12 @@  discard block
 block discarded – undo
107 107
      * @param $report
108 108
      * @return bool
109 109
      */
110
-    public function toQueue($report)
111
-    {
110
+    public function toQueue($report)
111
+    {
112 112
         //если отправлять некуда или незачем, то делаем вид, что отправили
113
-        if (!$this->getCFGDef('to') || $this->getCFGDef('noemail')) {
113
+        if (!$this->getCFGDef('to') || $this->getCFGDef('noemail')) {
114 114
             return true;
115
-        } elseif (empty($report)) {
115
+        } elseif (empty($report)) {
116 116
             return false;
117 117
         }
118 118
 
@@ -120,15 +120,15 @@  discard block
 block discarded – undo
120 120
 
121 121
         $this->Body = $this->modx->removeSanitizeSeed($this->mail->Body);
122 122
         $this->Subject = $this->modx->removeSanitizeSeed($this->mail->Subject);
123
-        try {
123
+        try {
124 124
             $result = $this->mail->preSend() && $this->saveMessage();
125
-        } catch (\phpmailerException $e) {
125
+        } catch (\phpmailerException $e) {
126 126
             $this->mail->SetError($e->getMessage());
127 127
 
128 128
             $result = false;
129 129
         }
130 130
 
131
-        if ($result) {
131
+        if ($result) {
132 132
             $this->mail->ClearAllRecipients();
133 133
             $this->mail->ClearAttachments();
134 134
             $result = $this->getFileName();
@@ -141,11 +141,12 @@  discard block
 block discarded – undo
141 141
      * @param string $path
142 142
      * @return bool
143 143
      */
144
-    public function setQueuePath($path = '') {
145
-        if (!empty($path)) {
144
+    public function setQueuePath($path = '')
145
+    {
146
+        if (!empty($path)) {
146 147
             $this->queuePath = $path;
147 148
             return true;
148
-        } else {
149
+        } else {
149 150
             return false;
150 151
         }
151 152
     }
@@ -153,8 +154,8 @@  discard block
 block discarded – undo
153 154
     /**
154 155
      * @return mixed
155 156
      */
156
-    protected function saveMessage()
157
-    {
157
+    protected function saveMessage()
158
+    {
158 159
         $data = serialize(array(
159 160
             "header" => $this->mail->getMIMEHeader(),
160 161
             "body"   => $this->mail->getMIMEBody(),
@@ -162,11 +163,11 @@  discard block
 block discarded – undo
162 163
         ));
163 164
         $file = $this->getFileName();
164 165
         $dir = MODX_BASE_PATH . $this->queuePath;
165
-        if (!is_dir($dir)) {
166
+        if (!is_dir($dir)) {
166 167
             @mkdir($dir);
167 168
         }
168 169
         $result = @file_put_contents($dir . $file, $data) !== false;
169
-        if ($result) {
170
+        if ($result) {
170 171
             $result = $file;
171 172
         }
172 173
 
@@ -176,7 +177,8 @@  discard block
 block discarded – undo
176 177
     /**
177 178
      * @return string
178 179
      */
179
-    protected function getFileName() {
180
+    protected function getFileName()
181
+    {
180 182
         return $this->mail->getMessageID() . '.eml';
181 183
     }
182 184
 
@@ -184,18 +186,18 @@  discard block
 block discarded – undo
184 186
      * @param $file
185 187
      * @return bool
186 188
      */
187
-    public function fromQueue($file)
188
-    {
189
+    public function fromQueue($file)
190
+    {
189 191
         $result = false;
190 192
         $dir = MODX_BASE_PATH . $this->queuePath;
191
-        if (file_exists($dir . $file) && is_readable($dir . $file)) {
193
+        if (file_exists($dir . $file) && is_readable($dir . $file)) {
192 194
             $message = unserialize(file_get_contents($dir . $file));
193 195
             $this->config = $message['config'];
194 196
             $this->applyMailConfig();
195 197
             $this->mail->setMIMEHeader($message['header'])->setMIMEBody($message['body']);
196 198
             unset($message);
197 199
             $result = $this->mail->postSend();
198
-            if ($result) {
200
+            if ($result) {
199 201
                 $this->mail->setMIMEBody()->setMIMEHeader();
200 202
                 @unlink($dir . $file);
201 203
             }
@@ -207,8 +209,8 @@  discard block
 block discarded – undo
207 209
     /**
208 210
      * @return $this
209 211
      */
210
-    protected function applyMailConfig()
211
-    {
212
+    protected function applyMailConfig()
213
+    {
212 214
         $this->mail->IsHTML($this->getCFGDef('isHtml', 1));
213 215
         $this->mail->From = $this->getCFGDef('from', $this->modx->config['emailsender']);
214 216
         $this->mail->FromName = $this->getCFGDef('fromName', $this->modx->config['site_name']);
@@ -226,8 +228,8 @@  discard block
 block discarded – undo
226 228
      * @param mixed $default
227 229
      * @return mixed
228 230
      */
229
-    public function getCFGDef($param, $default = null)
230
-    {
231
+    public function getCFGDef($param, $default = null)
232
+    {
231 233
         return \APIhelpers::getkey($this->config, $param, $default);
232 234
     }
233 235
 }
Please login to merge, or discard this patch.