HEX
Server: LiteSpeed
System: Linux s917.lon1.mysecurecloudhost.com 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: assibfaf (3034)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/assibfaf/public_html/prox-modules/mailgunmailer/mailgunmailer.php
<?php

use Mailgun\Mailgun;
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Module\Module;
use Proxim\Util\ArrayUtils;

require_once dirname(__FILE__) . '/vendor/autoload.php';

class MailgunMailer extends Module
{
    const MAILGUN_ENABLED = 'MAILGUN_ENABLED';
    const MAILGUN_DOMAIN = 'MAILGUN_DOMAIN';
    const PRIVATE_API_KEY = 'PRIVATE_API_KEY';
    const MAILGUN_API_HOSTNAME = 'MAILGUN_API_HOSTNAME';

    public function __construct()
    {
        $this->name = 'mailgunmailer';
        $this->icon = 'fa fa-envelope';
        $this->version = '1.0.0';
        $this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
        $this->author = 'Davison Pro';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = 'Mailgun';
        $this->description = 'Send Email Newsletters, Password Resets, Promotional Emails, Shipping Notifications With Confidence';
    }

    public function checkAccess() {
        $user = $this->application->container->user;
        return $user->is_admin || $user->is_sub_admin ? true : false;
    }

    public function install()
    {
        if (!parent::install()) {
            return false;
        }

        $this->registerHook([
            'actionEmailSendBefore',
            'actionMailAlterMessageBeforeSend'
        ]);
    }

    public function hookActionMailAlterMessageBeforeSend($payload) {
        if(!Configuration::get(self::MAILGUN_ENABLED)) {
            return true;
        }

        $message = ArrayUtils::get($payload, 'message');
        $fileAttachment = ArrayUtils::get($payload, 'fileAttachment');
        $htmlMessage = ArrayUtils::get($payload, 'htmlMessage');

        $from = $message->getFrom();
        $to = $message->getTo();

        $fromAddress = $toAddress = $attachment = array();
        if (is_array($from)) {
            foreach ($from as $addr => $addrName) {
                $addr = trim($addr);
                $fromAddress[] = $addr;
            }
        }

        if (is_array($to)) {
            foreach ($to as $addr => $addrName) {
                $addr = trim($addr);
                $toAddress[] = $addr;
            }
        }

        if ($fileAttachment && !empty($fileAttachment)) {
            // Multiple attachments?
            if (!is_array(current($fileAttachment))) {
                $fileAttachment = array($fileAttachment);
            }

            foreach ($fileAttachment as $att) {
                $attachment[] = array(
                    'filePath' => $att['file_path'],
                    'filename' => $att['name']
                );
            }
        }

        $params = array(
            'from'    => $fromAddress,
            'to'      => $toAddress,
            'subject' => $message->getSubject(),
            'html'    => $htmlMessage,
            'attachment' => $attachment
        );

        try {
            $domain = Configuration::get(self::MAILGUN_DOMAIN);
            $api_hostname = Configuration::get(self::MAILGUN_API_HOSTNAME);
            $mailgun = Mailgun::create( Configuration::get(self::PRIVATE_API_KEY, PROX_ADMIN_SITE_ID), $api_hostname );
            $mailgun->messages()->send($domain, $params);
        } catch (Exception $e) {
        }

        return false;
    }
}