1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall\Panel\Sandbox;
24

25
use Shieldon\Messenger\Mailgun as MailgunTest;
26
use function explode;
27
use function filter_var;
28
use function str_replace;
29

30
/**
31
 * The sandbox for Sendgrid.
32
 */
33
class Mailgun
34
{
35
    /**
36
     * Invoker.
37
     *
38
     * @param array $args The arguments.
39
     *
40
     * @return bool
41
     */
42 3
    public function __invoke(array $args): bool
43
    {
44 3
        return $this->sandbox($args[0], $args[1]);
45
    }
46

47
    /**
48
     * Test Mailgun.
49
     *
50
     * @param array $getParams The GET params passed from tryMessenger method.
51
     * @param array $message   The message title and body.
52
     *
53
     * @return bool
54
     */
55 3
    private function sandbox($getParams, $message)
56
    {
57 3
        $apiKey = $getParams['apiKey'] ?? '';
58 3
        $domain = $getParams['domain'] ?? '';
59 3
        $sender = $getParams['sender'] ?? '';
60 3
        $recipients = $getParams['recipients'] ?? '';
61

62 3
        if (!empty($sender) && !empty($recipients) && !empty($apiKey) && !empty($domain)) {
63 3
            $recipients = str_replace("\r", '|', $recipients);
64 3
            $recipients = str_replace("\n", '|', $recipients);
65 3
            $recipients = explode('|', $recipients);
66

67 3
            $messenger = new MailgunTest($apiKey, $domain);
68

69 3
            foreach ($recipients as $recipient) {
70 3
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
71 3
                    $messenger->addRecipient($recipient);
72
                }
73
            }
74

75 3
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
76 3
                $messenger->addSender($sender);
77
            }
78

79 3
            $messenger->setSubject($message['title']);
80

81 3
            if ($messenger->send($message['body'])) {
82
                // @codeCoverageIgnoreStart
83
                return true;
84
                // @codeCoverageIgnoreEnd
85
            }
86
        }
87 3
        return false;
88
    }
89
}

Read our documentation on viewing source code .

Loading