Completed
Push — master ( fdda97...b0b47a )
by Arman
17s queued 11s
created

src/Libraries/Mailer/Mailer.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.0.0
13
 */
14
15
namespace Quantum\Libraries\Mailer;
16
17
use PHPMailer\PHPMailer\PHPMailer;
18
19
/**
20
 * Mailer class
21
 *
22
 * @package Quantum
23
 * @subpackage Libraries.Mailer
24
 * @category Libraries
25
 * @uses \PHPMailer
26
 */
27
class Mailer
28
{
29
30
    /**
31
     * PHP Mailer instance
32
     * @var object
33
     */
34
    private $mailer;
35
36
    /**
37
     * From address and name
38
     * @var array 
39
     */
40
    private $from = [];
41
42
    /**
43
     * To addresses
44
     * @var array 
45
     */
46
    private $addresses = [];
47
48
    /**
49
     * Reply To addresses
50
     * @var array 
51
     */
52
    private $replyToAddresses = [];
53
54
    /**
55
     * CC addresses
56
     * @var array 
57
     */
58
    private $ccAddresses = [];
59
60
    /**
61
     * BCC addresses
62
     * @var array
63
     */
64
    private $bccAddresses = [];
65
66
    /**
67
     * Email subject
68
     * @var string 
69
     */
70
    private $subject = null;
71
72
    /**
73
     * Email body
74
     * @var string|array 
75
     */
76
    private $message = null;
77
78
    /**
79
     * Email attachments
80
     * @var array
81
     */
82
    private $attachments = [];
83
84
    /**
85
     * Email attachments created from string 
86
     * @var array
87
     */
88
    private $stringAttachments = [];
89
90
    /**
91
     * Template path
92
     * @var string 
93
     */
94
    private $templatePath;
95
96
    /**
97
     * PHP Mailer Log
98
     * @var string
99
     */
100
    private $log;
101
102
    /**
103
     * Class constructor
104
     */
105
    public function __construct()
106
    {
107
        $this->mailer = new PHPMailer();
108
109
        if (strlen(env('MAIL_HOST')) > 0) {
0 ignored issues
show
It seems like env('MAIL_HOST') can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        if (strlen(/** @scrutinizer ignore-type */ env('MAIL_HOST')) > 0) {
Loading history...
110
            $this->setupSmtp();
111
            $this->setupDebugging();
112
        } else {
113
            $this->mailer->isMail();
114
        }
115
116
        $this->mailer->AllowEmpty = true;
117
        $this->mailer->isHTML(true);
118
    }
119
120
    /**
121
     * Creates the from email and the name
122
     * @param array $from
123
     * @return $this
124
     */
125
    public function setFrom($email, $name = null)
126
    {
127
        $this->from['email'] = $email;
128
        $this->from['name'] = $name;
129
        return $this;
130
    }
131
132
    /**
133
     * Gets from email and the name
134
     * @return array
135
     */
136
    public function getFrom()
137
    {
138
        return $this->from;
139
    }
140
141
    /**
142
     * Creates "To" addresses
143
     * @param string $email
144
     * @param string $name
145
     * @return $this
146
     */
147
    public function setAddress(string $email, string $name = null)
148
    {
149
        array_push($this->addresses, [
150
            'email' => $email,
151
            'name' => $name
152
        ]);
153
154
        return $this;
155
    }
156
157
    /**
158
     * Gets "To" addresses
159
     * @return array
160
     */
161
    public function getAddresses()
162
    {
163
        return $this->addresses;
164
    }
165
166
    /**
167
     * Creates "Reply-To" addresses
168
     * @param string $email
169
     * @param string $name
170
     * @return $this
171
     */
172
    public function setReplay(string $email, string $name = null)
173
    {
174
        array_push($this->replyToAddresses, [
175
            'email' => $email,
176
            'name' => $name
177
        ]);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Gets "Reply-To" addresses
184
     * @return array
185
     */
186
    public function getReplayes()
187
    {
188
        return $this->replyToAddresses;
189
    }
190
191
    /**
192
     * Creates "CC" addresses
193
     * @param string $email
194
     * @param string $name
195
     * @return $this
196
     */
197
    public function setCC(string $email, string $name = null)
198
    {
199
        array_push($this->ccAddresses, [
200
            'email' => $email,
201
            'name' => $name
202
        ]);
203
204
        return $this;
205
    }
206
207
    /**
208
     * Gets "CC" addresses
209
     * @return array
210
     */
211
    public function getCCs()
212
    {
213
        return $this->ccAddresses;
214
    }
215
216
    /**
217
     * Creates "BCC" addresses
218
     * @param string $email
219
     * @param string|null $name
220
     * @return $this
221
     */
222
    public function setBCC(string $email, $name = null)
223
    {
224
        array_push($this->bccAddresses, [
225
            'email' => $email,
226
            'name' => $name
227
        ]);
228
229
        return $this;
230
    }
231
232
    /**
233
     * Get "BCC" addresses
234
     * @return array
235
     */
236
    public function getBCCs()
237
    {
238
        return $this->bccAddresses;
239
    }
240
241
    /**
242
     * Creates the subject
243
     * @param string $subject
244
     * @return $this
245
     */
246
    public function setSubject($subject)
247
    {
248
        $this->subject = $subject;
249
        return $this;
250
    }
251
252
    /**
253
     * Gets the subject
254
     * @return string
255
     */
256
    public function getSubject()
257
    {
258
        return $this->subject;
259
    }
260
261
    /**
262
     * Sets the template
263
     * @param string $templatePath
264
     * @return $this
265
     */
266
    public function setTemplate($templatePath)
267
    {
268
        $this->templatePath = $templatePath;
269
        return $this;
270
    }
271
272
    /**
273
     * Gets the template
274
     * @return string
275
     */
276
    public function getTemplate()
277
    {
278
        return $this->templatePath;
279
    }
280
281
    /**
282
     * Creates the body
283
     * @param string|array $message
284
     * @return $this
285
     */
286
    public function setBody($message)
287
    {
288
        $this->message = $message;
289
        return $this;
290
    }
291
292
    /**
293
     * Gets the body
294
     * @return string|array
295
     */
296
    public function getBody()
297
    {
298
        return $this->message;
299
    }
300
301
    /**
302
     * Creates attachments from the path on the filesystem
303
     * @param mixed $attachments
304
     * @return $this
305
     */
306
    public function setAttachments(array $attachments)
307
    {
308
        $this->attachments = $attachments;
309
        return $this;
310
    }
311
312
    /**
313
     * Gets the attachments
314
     * @return array
315
     */
316
    public function getAttachments()
317
    {
318
        return $this->attachments;
319
    }
320
321
    /**
322
     * Creates attachments from the string
323
     * @param string $content
324
     * @param string $filename
325
     * @return $this
326
     */
327
    public function setStringAttachment($content, $filename)
328
    {
329
        array_push($this->stringAttachments, [
330
            'content' => $content,
331
            'filename' => $filename
332
        ]);
333
334
        return $this;
335
    }
336
337
    /**
338
     * Gets the string attachments
339
     * @return array
340
     */
341
    public function getStringAttachment()
342
    {
343
        return $this->stringAttachments;
344
    }
345
346
    /**
347
     * Sends the email
348
     * @param array|null $from
349
     * @param array|null $addresses
350
     * @param string|null $message
351
     * @param array $options
352
     * @return bool
353
     */
354
    public function send(array $from = null, array $address = null, $message = null, $options = [])
355
    {
356
        if ($from) {
357
            $this->setFrom(extract($from));
358
        }
359
360
        if ($address) {
361
            $this->setAddress(extract($address));
362
        }
363
364
        if (isset($options['replayto'])) {
365
            $this->setReplay(extract($options['replayto']));
366
        }
367
368
        if (isset($options['cc'])) {
369
            $this->setCC(extract($options['cc']));
370
        }
371
372
        if (isset($options['bcc'])) {
373
            $this->setBCC(extract($options['bcc']));
374
        }
375
376
        if (isset($options['subject'])) {
377
            $this->setSubject($options['subject']);
378
        }
379
380
        if ($message) {
381
            $this->setBody($message);
382
        }
383
384
        if (isset($options['attachments'])) {
385
            $this->setAttachments($options['attachments']);
386
        }
387
388
        if (isset($options['stringAttachment'])) {
389
            $this->setStringAttachment($options['content'], $options['filename']);
390
        }
391
392
        $this->prepare();
393
394
        if ($this->mailer->send()) {
395
            return true;
396
        } else {
397
            return false;
398
        }
399
    }
400
401
    /**
402
     * Prepares the data
403
     */
404
    private function prepare()
405
    {
406
        $this->mailer->setFrom($this->from['email'], $this->from['name']);
407
408
        if (!empty($this->addresses)) {
409
            foreach ($this->addresses as $address) {
410
                $this->mailer->addAddress($address['email'], $address['name']);
411
            }
412
        }
413
414
        if (!empty($this->replyToAddresses)) {
415
            foreach ($this->replyToAddresses as $address) {
416
                $this->mailer->addReplyTo($address['email'], $address['name']);
417
            }
418
        }
419
420
        if (!empty($this->ccAddresses)) {
421
            foreach ($this->ccAddresses as $address) {
422
                $this->mailer->addCC($address['email'], $address['name']);
423
            }
424
        }
425
        if (!empty($this->bccAddresses)) {
426
            foreach ($this->bccAddresses as $address) {
427
                $this->mailer->addBCC($address['email'], $address['name']);
428
            }
429
        }
430
431
        if ($this->subject) {
432
            $this->mailer->Subject = $this->subject;
433
        }
434
435
        if ($this->message) {
436
            $body = '';
437
438
            if ($this->templatePath) {
439
                $body = $this->createFromTemplate();
440
            } else {
441
                $body = is_array($this->message) ? implode($this->message) : $this->message;
442
            }
443
444
            $this->mailer->Body = $body;
445
        }
446
447
        if (!empty($this->attachments)) {
448
            foreach ($this->attachments as $attachment) {
449
                $this->mailer->addAttachment($attachment);
450
            }
451
        }
452
453
        if (!empty($this->stringAttachments)) {
454
            foreach ($this->stringAttachments as $attachment) {
455
                $this->mailer->addStringAttachment($attachment['content'], $attachment['filename']);
456
            }
457
        }
458
    }
459
460
    /**
461
     * Setups SMTP
462
     */
463
    private function setupSmtp()
464
    {
465
        $this->mailer->isSMTP();
466
        $this->mailer->SMTPAuth = true;
467
        $this->mailer->Host = env('MAIL_HOST');
468
        $this->mailer->SMTPSecure = env('MAIL_SMTP_SECURE');
469
        $this->mailer->Port = env('MAIL_PORT');
470
        $this->mailer->Username = env('MAIL_USERNAME');
471
        $this->mailer->Password = env('MAIL_PASSWORD');
472
    }
473
474
    /**
475
     * Setups the debugging
476
     */
477
    private function setupDebugging()
478
    {
479
        if (config()->get('debug')) {
480
            $this->mailer->SMTPDebug = 1;
481
            $this->mailer->Debugoutput = function ($str, $level) {
482
                $this->log .= $str . '&';
483
                session()->set('_qt_mailer_log', $this->log);
484
            };
485
        } else {
486
            $this->mailer->SMTPDebug = 0;
487
        }
488
    }
489
490
    /**
491
     * Create message body from email template
492
     * @return string
493
     */
494
    private function createFromTemplate()
495
    {
496
        ob_start();
497
        ob_implicit_flush(0);
498
499
        if (!empty($this->message) && is_array($this->message)) {
500
            extract($this->message, EXTR_OVERWRITE);
501
        }
502
503
        require $this->templatePath . '.php';
504
505
        return ob_get_clean();
506
    }
507
508
}
509