Passed
Push — 2.x ( 92ec38...c080f0 )
by Terry
02:07
created

PhpNativeMail::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Panel\Sandbox;
14
15
use Shieldon\Messenger as Messenger;
16
17
/**
18
 * The sandbox for PHP native mail.
19
 */
20
class PhpNativeMail
21
{
22
    /**
23
     * Invoker.
24
     *
25
     * @param array $args
26
     *
27
     * @return bool
28
     */
29
    public function __invoke(array $args): bool
30
    {
31
        return $this->sandbox($args[0], $args[1]);
32
    }
33
34
    /**
35
     * Test PHP native mail.
36
     *
37
     * @param array $getParams The GET params passed from tryMessenger method.
38
     * @param array $message   The message title and body.
39
     *
40
     * @return bool
41
     */
42
    private function sandbox($getParams, $message)
43
    {
44
        $sender = $getParams['sender'] ?? '';
45
        $recipients = $getParams['recipients'] ?? '';
46
47
        if (!empty($sender) && !empty($recipients)) {
48
            $recipients = str_replace("\r", '|', $recipients);
49
            $recipients = str_replace("\n", '|', $recipients);
50
            $recipients = explode('|', $recipients);
51
52
            $messenger = new Messenger\Mail();
53
54
            foreach($recipients as $recipient) {
55
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
56
                    $messenger->addRecipient($recipient);
57
                }
58
            }
59
60
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
61
                $messenger->addSender($sender);
62
            }
63
64
            $messenger->setSubject($message['title']);
65
66
            if ($messenger->send($message['body'])) {
67
                return true;
68
            }
69
        }
70
        return false;
71
    }
72
}