| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 7 |
use FluentSupport\App\Services\Emogrifier; |
| 8 |
use FluentSupport\App\Services\Mailer; |
| 9 |
use FluentSupport\Framework\Support\Arr; |
| 10 |
|
| 11 |
class EmailNotificationHandler |
| 12 |
{ |
| 13 |
public function ticketCreated($ticket, $customer) |
| 14 |
{ |
| 15 |
$mailbox = $ticket->mailbox; |
| 16 |
|
| 17 |
if (!$mailbox) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
// Let's send welcome email to customer if enabled |
| 22 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_customer'); |
| 23 |
if ($customer->status == 'inactive' && $emailSettings && $emailSettings['status'] == 'yes') { |
| 24 |
|
| 25 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 26 |
'customer' => $customer, |
| 27 |
'business' => $mailbox, |
| 28 |
'ticket' => $ticket |
| 29 |
]); |
| 30 |
|
| 31 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 32 |
'customer' => $customer, |
| 33 |
'business' => $mailbox, |
| 34 |
'ticket' => $ticket, |
| 35 |
'email_type' => 'ticket_created_email_to_customer' |
| 36 |
]); |
| 37 |
|
| 38 |
$headers = $mailbox->getMailerHeader(); |
| 39 |
if ($ticket->message_id) { |
| 40 |
// $headers[] = 'Message-ID: ' . $ticket->message_id; |
| 41 |
} |
| 42 |
Mailer::send($customer->email, $subject, $emailBody, $headers); |
| 43 |
} |
| 44 |
|
| 45 |
// let's send email to admin if enabled |
| 46 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_admin'); |
| 47 |
if ($emailSettings && $emailSettings['status'] == 'yes' && is_email($mailbox->settings['admin_email_address'])) { |
| 48 |
|
| 49 |
$mailTo = Arr::get($mailbox->settings, 'admin_email_address'); |
| 50 |
if(!$mailTo || !is_email($mailTo)) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 55 |
'customer' => $customer, |
| 56 |
'business' => $mailbox, |
| 57 |
'ticket' => $ticket |
| 58 |
]); |
| 59 |
|
| 60 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 61 |
'customer' => $customer, |
| 62 |
'business' => $mailbox, |
| 63 |
'ticket' => $ticket, |
| 64 |
'email_type' => 'ticket_created_email_to_admin' |
| 65 |
]); |
| 66 |
|
| 67 |
$headers = $mailbox->getMailerHeader(); |
| 68 |
if ($ticket->message_id) { |
| 69 |
// $headers[] = 'Message-ID: ' . $ticket->message_id; |
| 70 |
} |
| 71 |
Mailer::send($mailTo, $subject, $emailBody, $headers); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public function agentReplied($response, $ticket, $agent) |
| 77 |
{ |
| 78 |
$mailbox = $ticket->mailbox; |
| 79 |
|
| 80 |
|
| 81 |
if (!$mailbox) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
// Let's send welcome email to customer if enabled |
| 86 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_agent_email_to_customer'); |
| 87 |
|
| 88 |
|
| 89 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 90 |
|
| 91 |
$ticket->load('customer'); |
| 92 |
$customer = $ticket->customer; |
| 93 |
|
| 94 |
if($customer->status == 'inactive') { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 99 |
'customer' => $customer, |
| 100 |
'business' => $mailbox, |
| 101 |
'ticket' => $ticket, |
| 102 |
'response' => $response, |
| 103 |
'agent' => $agent |
| 104 |
]); |
| 105 |
|
| 106 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 107 |
'customer' => $customer, |
| 108 |
'business' => $mailbox, |
| 109 |
'ticket' => $ticket, |
| 110 |
'response' => $response, |
| 111 |
'agent' => $agent, |
| 112 |
'email_type' => 'ticket_replied_by_agent_email_to_customer' |
| 113 |
]); |
| 114 |
|
| 115 |
|
| 116 |
$headers = $mailbox->getMailerHeader(); |
| 117 |
|
| 118 |
Mailer::send($customer->email, $subject, $emailBody, $headers); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public function closedByAgent($ticket, $agent) |
| 123 |
{ |
| 124 |
$mailbox = $ticket->mailbox; |
| 125 |
if (!$mailbox) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Let's send welcome email to customer if enabled |
| 130 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_closed_by_agent_email_to_customer'); |
| 131 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 132 |
|
| 133 |
$ticket->load('customer'); |
| 134 |
$customer = $ticket->customer; |
| 135 |
|
| 136 |
if($customer->status == 'inactive') { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 142 |
'customer' => $customer, |
| 143 |
'business' => $mailbox, |
| 144 |
'ticket' => $ticket, |
| 145 |
'agent' => $agent, |
| 146 |
]); |
| 147 |
|
| 148 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 149 |
'customer' => $customer, |
| 150 |
'business' => $mailbox, |
| 151 |
'ticket' => $ticket, |
| 152 |
'agent' => $agent, |
| 153 |
'email_type' => 'ticket_closed_by_agent_email_to_customer' |
| 154 |
]); |
| 155 |
|
| 156 |
$headers = $mailbox->getMailerHeader(); |
| 157 |
if ($ticket->message_id) { |
| 158 |
// $headers[] = 'Message-ID: ' . $ticket->message_id; |
| 159 |
} |
| 160 |
Mailer::send($customer->email, $subject, $emailBody, $headers); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public function customerReplied($response, $ticket, $customer) |
| 165 |
{ |
| 166 |
$mailbox = $ticket->mailbox; |
| 167 |
if (!$mailbox) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
// Let's send welcome email to customer if enabled |
| 172 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_customer_email_to_admin'); |
| 173 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 174 |
|
| 175 |
$ticket->load('agent'); |
| 176 |
$agent = $ticket->agent; |
| 177 |
|
| 178 |
$emailTo = $mailbox->settings['admin_email_address']; |
| 179 |
if($agent) { |
| 180 |
$emailTo = $agent->email; |
| 181 |
} |
| 182 |
|
| 183 |
if(!$emailTo || !is_email($emailTo)) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 188 |
'customer' => $customer, |
| 189 |
'business' => $mailbox, |
| 190 |
'ticket' => $ticket, |
| 191 |
'response' => $response, |
| 192 |
'agent' => $agent |
| 193 |
]); |
| 194 |
|
| 195 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 196 |
'customer' => $customer, |
| 197 |
'business' => $mailbox, |
| 198 |
'ticket' => $ticket, |
| 199 |
'response' => $response, |
| 200 |
'agent' => $agent, |
| 201 |
'email_type' => 'ticket_replied_by_customer_email_to_admin' |
| 202 |
]); |
| 203 |
|
| 204 |
$headers = $mailbox->getMailerHeader(); |
| 205 |
if ($ticket->message_id) { |
| 206 |
// $headers[] = 'Message-ID: ' . $ticket->message_id; |
| 207 |
} |
| 208 |
|
| 209 |
Mailer::send($emailTo, $subject, $emailBody, $headers); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private function emailTemplateCss() |
| 214 |
{ |
| 215 |
$app = App::getInstance(); |
| 216 |
return $app->view->make('emails.styles'); |
| 217 |
} |
| 218 |
|
| 219 |
protected function parseEmailBody($emailBody, $data) |
| 220 |
{ |
| 221 |
$data['email_body'] = apply_filters('fluent_support/parse_smartcode_data', $emailBody, $data); |
| 222 |
|
| 223 |
$app = App::getInstance(); |
| 224 |
|
| 225 |
if (isset($data['business'])) { |
| 226 |
$businessName = $data['business']->name; |
| 227 |
} else { |
| 228 |
$businessName = get_bloginfo('name'); |
| 229 |
} |
| 230 |
|
| 231 |
$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>.'); |
| 232 |
|
| 233 |
$data['email_footer'] = $footerText; |
| 234 |
|
| 235 |
$emailBody = $app->view->make('emails.ticket_template', $data); |
| 236 |
$emogrifier = new Emogrifier($emailBody, $this->emailTemplateCss()); |
| 237 |
$emogrifier->disableInvisibleNodeRemoval(); |
| 238 |
return $emogrifier->emogrify(); |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|