# fluent-support/1.4.5/app/Hooks/Handlers/EmailNotificationHandler.php

Fluent Support – Helpdesk &amp; Customer Support Ticket System, version 1.4.5. 242 lines.

- Page: https://pluginprobe.com/plugins/fluent-support/1.4.5/code/app/Hooks/Handlers/EmailNotificationHandler.php
- Raw: https://pluginprobe.com/plugins/fluent-support/1.4.5/raw/app/Hooks/Handlers/EmailNotificationHandler.php
- Modified: 2021-11-12T14:57:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-support/1.4.5/code/app/Hooks/Handlers/EmailNotificationHandler.php#L10-L20`.

```php
<?php

namespace FluentSupport\App\Hooks\Handlers;

use FluentSupport\App\App;
use FluentSupport\App\Services\EmailNotification\Settings;
use FluentSupport\App\Services\Emogrifier;
use FluentSupport\App\Services\Mailer;
use FluentSupport\Framework\Support\Arr;

class EmailNotificationHandler
{
    public function ticketCreated($ticket, $customer)
    {
        $mailbox = $ticket->mailbox;

        if (!$mailbox) {
            return;
        }

        // Let's send welcome email to customer if enabled
        $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_customer');
        if ($customer->status == 'inactive' && $emailSettings && $emailSettings['status'] == 'yes') {

            $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
                'customer' => $customer,
                'business' => $mailbox,
                'ticket'   => $ticket
            ]);

            $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
                'customer'   => $customer,
                'business'   => $mailbox,
                'ticket'     => $ticket,
                'email_type' => 'ticket_created_email_to_customer'
            ]);

            $headers = $mailbox->getMailerHeader();
            if ($ticket->message_id) {
               // $headers[] = 'Message-ID: ' . $ticket->message_id;
            }
            Mailer::send($customer->email, $subject, $emailBody, $headers);
        }

        // let's send email to admin if enabled
        $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_admin');
        if ($emailSettings && $emailSettings['status'] == 'yes' && is_email($mailbox->settings['admin_email_address'])) {

            $mailTo = Arr::get($mailbox->settings, 'admin_email_address');
            if(!$mailTo || !is_email($mailTo)) {
                return;
            }

            $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
                'customer' => $customer,
                'business' => $mailbox,
                'ticket'   => $ticket
            ]);

            $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
                'customer'   => $customer,
                'business'   => $mailbox,
                'ticket'     => $ticket,
                'email_type' => 'ticket_created_email_to_admin'
            ]);

            $headers = $mailbox->getMailerHeader();
            if ($ticket->message_id) {
               // $headers[] = 'Message-ID: ' . $ticket->message_id;
            }
            Mailer::send($mailTo, $subject, $emailBody, $headers);
        }

    }

    public function agentReplied($response, $ticket, $agent)
    {
        $mailbox = $ticket->mailbox;


        if (!$mailbox) {
            return false;
        }

        // Let's send welcome email to customer if enabled
        $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_agent_email_to_customer');


        if ($emailSettings && $emailSettings['status'] == 'yes') {

            $ticket->load('customer');
            $customer = $ticket->customer;

            if($customer->status == 'inactive') {
                return false;
            }

            $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
                'customer' => $customer,
                'business' => $mailbox,
                'ticket'   => $ticket,
                'response' => $response,
                'agent'    => $agent
            ]);

            $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
                'customer'   => $customer,
                'business'   => $mailbox,
                'ticket'     => $ticket,
                'response'   => $response,
                'agent'      => $agent,
                'email_type' => 'ticket_replied_by_agent_email_to_customer'
            ]);


            $headers = $mailbox->getMailerHeader();

            Mailer::send($customer->email, $subject, $emailBody, $headers);
        }
    }

    public function closedByAgent($ticket, $agent)
    {
        $mailbox = $ticket->mailbox;
        if (!$mailbox) {
            return;
        }

        // Let's send welcome email to customer if enabled
        $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_closed_by_agent_email_to_customer');
        if ($emailSettings && $emailSettings['status'] == 'yes') {

            $ticket->load('customer');
            $customer = $ticket->customer;

            if($customer->status == 'inactive') {
                return false;
            }


            $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
                'customer' => $customer,
                'business' => $mailbox,
                'ticket'   => $ticket,
                'agent'    => $agent,
            ]);

            $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
                'customer'   => $customer,
                'business'   => $mailbox,
                'ticket'     => $ticket,
                'agent'      => $agent,
                'email_type' => 'ticket_closed_by_agent_email_to_customer'
            ]);

            $headers = $mailbox->getMailerHeader();
            if ($ticket->message_id) {
                // $headers[] = 'Message-ID: ' . $ticket->message_id;
            }
            Mailer::send($customer->email, $subject, $emailBody, $headers);
        }
    }

    public function customerReplied($response, $ticket, $customer)
    {
        $mailbox = $ticket->mailbox;
        if (!$mailbox) {
            return;
        }

        // Let's send welcome email to customer if enabled
        $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_customer_email_to_admin');
        if ($emailSettings && $emailSettings['status'] == 'yes') {

            $ticket->load('agent');
            $agent = $ticket->agent;

            $emailTo = $mailbox->settings['admin_email_address'];
            if($agent) {
                $emailTo = $agent->email;
            }

            if(!$emailTo || !is_email($emailTo)) {
                return;
            }

            $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
                'customer' => $customer,
                'business' => $mailbox,
                'ticket'   => $ticket,
                'response' => $response,
                'agent'    => $agent
            ]);

            $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
                'customer'   => $customer,
                'business'   => $mailbox,
                'ticket'     => $ticket,
                'response'   => $response,
                'agent'      => $agent,
                'email_type' => 'ticket_replied_by_customer_email_to_admin'
            ]);

            $headers = $mailbox->getMailerHeader();
            if ($ticket->message_id) {
               // $headers[] = 'Message-ID: ' . $ticket->message_id;
            }

            Mailer::send($emailTo, $subject, $emailBody, $headers);
        }
    }

    private function emailTemplateCss()
    {
        $app = App::getInstance();
        return $app->view->make('emails.styles');
    }

    protected function parseEmailBody($emailBody, $data)
    {
        $data['email_body'] = apply_filters('fluent_support/parse_smartcode_data', $emailBody, $data);

        $app = App::getInstance();

        if (isset($data['business'])) {
            $businessName = $data['business']->name;
        } else {
            $businessName = get_bloginfo('name');
        }

        $footerText = apply_filters('fluent_support/email_footer_credit', 'This email is a service from ' . $businessName . '. Support Plugin is Powered by <a href="https://fluentsupport.com/?utm_source=user&utm_medium=wp&utm_campaign=mail_footer" style="color:#9e9e9e;" target="_new">FluentSupport</a>.');

        $data['email_footer'] = $footerText;

        $emailBody = $app->view->make('emails.ticket_template', $data);
        $emogrifier = new Emogrifier($emailBody, $this->emailTemplateCss());
        $emogrifier->disableInvisibleNodeRemoval();
        return $emogrifier->emogrify();
    }

}

```
