| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Models\Meta; |
| 7 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 8 |
use FluentSupport\App\Services\Emogrifier; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\Mailer; |
| 11 |
use FluentSupport\Framework\Support\Arr; |
| 12 |
|
| 13 |
class EmailNotificationHandler |
| 14 |
{ |
| 15 |
public function ticketCreated($ticket, $customer) |
| 16 |
{ |
| 17 |
$mailbox = $ticket->mailbox; |
| 18 |
|
| 19 |
if (!$mailbox) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
// Let's send welcome email to customer if enabled |
| 24 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_customer'); |
| 25 |
|
| 26 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 27 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 28 |
'customer' => $customer, |
| 29 |
'business' => $mailbox, |
| 30 |
'ticket' => $ticket |
| 31 |
]); |
| 32 |
|
| 33 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 34 |
'customer' => $customer, |
| 35 |
'business' => $mailbox, |
| 36 |
'ticket' => $ticket, |
| 37 |
'email_type' => 'ticket_created_email_to_customer' |
| 38 |
]); |
| 39 |
|
| 40 |
$headers = $mailbox->getMailerHeader(); |
| 41 |
|
| 42 |
$headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [ |
| 43 |
'cc_email' => $ticket->getSettingsValue('cc_email', []), |
| 44 |
'hook_type' => 'ticket_created_email_to_customer' |
| 45 |
]); |
| 46 |
|
| 47 |
$attachments = []; |
| 48 |
|
| 49 |
if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) { |
| 50 |
foreach ($files as $file) { |
| 51 |
if ($file->driver == 'local') { |
| 52 |
$filePath = $file->file_path; |
| 53 |
} else { |
| 54 |
$filePath = Arr::get($file->settings, 'local_temp_path'); |
| 55 |
} |
| 56 |
if (file_exists($filePath)) { |
| 57 |
$attachments[] = $filePath; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments); |
| 63 |
} |
| 64 |
|
| 65 |
// let's send email to admin if enabled |
| 66 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_admin'); |
| 67 |
if ($emailSettings && $emailSettings['status'] == 'yes' && is_email($mailbox->settings['admin_email_address'])) { |
| 68 |
|
| 69 |
$mailTo = Arr::get($mailbox->settings, 'admin_email_address'); |
| 70 |
|
| 71 |
if (!$mailTo || !is_email($mailTo)) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 76 |
'customer' => $customer, |
| 77 |
'business' => $mailbox, |
| 78 |
'ticket' => $ticket |
| 79 |
]); |
| 80 |
|
| 81 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 82 |
'customer' => $customer, |
| 83 |
'business' => $mailbox, |
| 84 |
'ticket' => $ticket, |
| 85 |
'email_type' => 'ticket_created_email_to_admin' |
| 86 |
]); |
| 87 |
|
| 88 |
$headers = $mailbox->getMailerHeader(); |
| 89 |
|
| 90 |
$attachments = []; |
| 91 |
|
| 92 |
if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) { |
| 93 |
foreach ($files as $file) { |
| 94 |
|
| 95 |
if ($file->driver == 'local') { |
| 96 |
$filePath = $file->file_path; |
| 97 |
} else { |
| 98 |
$filePath = Arr::get($file->settings, 'local_temp_path'); |
| 99 |
} |
| 100 |
if (file_exists($filePath)) { |
| 101 |
$attachments[] = $filePath; |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
Mailer::send($mailTo, $subject, $emailBody, $headers, $attachments); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
public function agentReplied($response, $ticket, $agent) |
| 112 |
{ |
| 113 |
$mailbox = $ticket->mailbox; |
| 114 |
|
| 115 |
|
| 116 |
if (!$mailbox) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
// Let's send welcome email to customer if enabled |
| 121 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_agent_email_to_customer'); |
| 122 |
|
| 123 |
|
| 124 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 125 |
|
| 126 |
$ticket->load('customer'); |
| 127 |
$customer = $ticket->customer; |
| 128 |
|
| 129 |
if ($customer->status == 'inactive') { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 134 |
'customer' => $customer, |
| 135 |
'business' => $mailbox, |
| 136 |
'ticket' => $ticket, |
| 137 |
'response' => $response, |
| 138 |
'agent' => $agent |
| 139 |
]); |
| 140 |
|
| 141 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 142 |
'customer' => $customer, |
| 143 |
'business' => $mailbox, |
| 144 |
'ticket' => $ticket, |
| 145 |
'response' => $response, |
| 146 |
'agent' => $agent, |
| 147 |
'email_type' => 'ticket_replied_by_agent_email_to_customer' |
| 148 |
]); |
| 149 |
|
| 150 |
|
| 151 |
$headers = $mailbox->getMailerHeader(); |
| 152 |
$headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [ |
| 153 |
'cc_email' => $response->getSettingsValue('cc_email', []), |
| 154 |
'hook_type' => 'ticket_replied_by_agent_email_to_customer' |
| 155 |
]); |
| 156 |
$attachments = []; |
| 157 |
|
| 158 |
if ($emailSettings['send_attachments'] == 'yes' && ($files = $response->attachments)) { |
| 159 |
foreach ($files as $file) { |
| 160 |
if ($file->driver == 'local') { |
| 161 |
$filePath = $file->file_path; |
| 162 |
} else { |
| 163 |
$filePath = Arr::get($file->settings, 'local_temp_path'); |
| 164 |
} |
| 165 |
if (file_exists($filePath)) { |
| 166 |
$attachments[] = $filePath; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public function closedByAgent($ticket, $agent) |
| 176 |
{ |
| 177 |
$mailbox = $ticket->mailbox; |
| 178 |
if (!$mailbox) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
// Let's send welcome email to customer if enabled |
| 183 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_closed_by_agent_email_to_customer'); |
| 184 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 185 |
|
| 186 |
$ticket->load('customer'); |
| 187 |
$customer = $ticket->customer; |
| 188 |
|
| 189 |
if ($customer->status == 'inactive') { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 195 |
'customer' => $customer, |
| 196 |
'business' => $mailbox, |
| 197 |
'ticket' => $ticket, |
| 198 |
'agent' => $agent, |
| 199 |
]); |
| 200 |
|
| 201 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 202 |
'customer' => $customer, |
| 203 |
'business' => $mailbox, |
| 204 |
'ticket' => $ticket, |
| 205 |
'agent' => $agent, |
| 206 |
'email_type' => 'ticket_closed_by_agent_email_to_customer' |
| 207 |
]); |
| 208 |
|
| 209 |
$headers = $mailbox->getMailerHeader(); |
| 210 |
$headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [ |
| 211 |
'cc_email' => $ticket->getSettingsValue('cc_email', []), |
| 212 |
'hook_type' => 'ticket_closed_by_agent_email_to_customer' |
| 213 |
]); |
| 214 |
Mailer::send($customer->email, $subject, $emailBody, $headers); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
public function customerReplied($response, $ticket, $customer) |
| 219 |
{ |
| 220 |
$mailbox = $ticket->mailbox; |
| 221 |
if (!$mailbox) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
// Let's send welcome email to customer if enabled |
| 226 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_customer_email_to_admin'); |
| 227 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 228 |
|
| 229 |
$ticket->load('agent'); |
| 230 |
$agent = $ticket->agent; |
| 231 |
|
| 232 |
$emailTo = $mailbox->settings['admin_email_address']; |
| 233 |
if ($agent) { |
| 234 |
$emailTo = $agent->email; |
| 235 |
} |
| 236 |
|
| 237 |
if (!$emailTo || !is_email($emailTo)) { |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 242 |
'customer' => $customer, |
| 243 |
'business' => $mailbox, |
| 244 |
'ticket' => $ticket, |
| 245 |
'response' => $response, |
| 246 |
'agent' => $agent |
| 247 |
]); |
| 248 |
|
| 249 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 250 |
'customer' => $customer, |
| 251 |
'business' => $mailbox, |
| 252 |
'ticket' => $ticket, |
| 253 |
'response' => $response, |
| 254 |
'agent' => $agent, |
| 255 |
'email_type' => 'ticket_replied_by_customer_email_to_admin' |
| 256 |
]); |
| 257 |
|
| 258 |
$headers = $mailbox->getMailerHeader(); |
| 259 |
$attachments = []; |
| 260 |
|
| 261 |
if ($emailSettings['send_attachments'] == 'yes' && ($files = $response->attachments)) { |
| 262 |
foreach ($files as $file) { |
| 263 |
if ($file->driver == 'local') { |
| 264 |
$filePath = $file->file_path; |
| 265 |
} else { |
| 266 |
$filePath = Arr::get($file->settings, 'local_temp_path'); |
| 267 |
} |
| 268 |
if (file_exists($filePath)) { |
| 269 |
$attachments[] = $filePath; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
Mailer::send($emailTo, $subject, $emailBody, $headers, $attachments); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
public function onAgentAssign($agent, $ticket, $assigner) |
| 279 |
{ |
| 280 |
$currentUser = Helper::getAgentByUserId(); |
| 281 |
|
| 282 |
if ($currentUser && $currentUser->user_id == $agent->user_id) { |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
$mailbox = $ticket->mailbox; |
| 287 |
|
| 288 |
if (!$mailbox) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// Let's send notification email to agent if enabled |
| 293 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_agent_on_change'); |
| 294 |
|
| 295 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 296 |
|
| 297 |
if ($agent) { |
| 298 |
$emailTo = $agent->email; |
| 299 |
} |
| 300 |
|
| 301 |
if (!$emailTo || !is_email($emailTo)) { |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 306 |
'business' => $mailbox, |
| 307 |
'ticket' => $ticket, |
| 308 |
'agent' => $agent, |
| 309 |
'assigner' => $assigner |
| 310 |
]); |
| 311 |
|
| 312 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 313 |
'business' => $mailbox, |
| 314 |
'ticket' => $ticket, |
| 315 |
'agent' => $agent, |
| 316 |
'assigner' => $assigner, |
| 317 |
'email_type' => 'ticket_agent_on_change' |
| 318 |
]); |
| 319 |
|
| 320 |
$headers = $mailbox->getMailerHeader(); |
| 321 |
|
| 322 |
Mailer::send($emailTo, $subject, $emailBody, $headers); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
public function ticketCreatedByAgent($ticket, $customer, $agent) |
| 327 |
{ |
| 328 |
$mailbox = $ticket->mailbox; |
| 329 |
|
| 330 |
if (!$mailbox) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
// Let's send welcome email to customer if enabled |
| 335 |
$emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_by_agent_email_to_customer'); |
| 336 |
if ($emailSettings && $emailSettings['status'] == 'yes') { |
| 337 |
$subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [ |
| 338 |
'customer' => $customer, |
| 339 |
'agent' => $agent, |
| 340 |
'business' => $mailbox, |
| 341 |
'ticket' => $ticket |
| 342 |
]); |
| 343 |
|
| 344 |
$emailBody = $this->parseEmailBody($emailSettings['email_body'], [ |
| 345 |
'customer' => $customer, |
| 346 |
'business' => $mailbox, |
| 347 |
'agent' => $agent, |
| 348 |
'ticket' => $ticket, |
| 349 |
'email_type' => 'ticket_created_by_agent_email_to_customer' |
| 350 |
]); |
| 351 |
|
| 352 |
$headers = $mailbox->getMailerHeader(); |
| 353 |
|
| 354 |
$attachments = []; |
| 355 |
|
| 356 |
if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) { |
| 357 |
foreach ($files as $file) { |
| 358 |
if ($file->driver == 'local') { |
| 359 |
$filePath = $file->file_path; |
| 360 |
} else { |
| 361 |
$filePath = Arr::get($file->settings, 'local_temp_path'); |
| 362 |
} |
| 363 |
if (file_exists($filePath)) { |
| 364 |
$attachments[] = $filePath; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private function emailTemplateCss() |
| 374 |
{ |
| 375 |
$app = App::getInstance(); |
| 376 |
return $app->view->make('emails.styles'); |
| 377 |
} |
| 378 |
|
| 379 |
protected function parseEmailBody($emailBody, $data) |
| 380 |
{ |
| 381 |
$data['email_body'] = apply_filters('fluent_support/parse_smartcode_data', $emailBody, $data); |
| 382 |
|
| 383 |
$app = App::getInstance(); |
| 384 |
|
| 385 |
if (isset($data['business'])) { |
| 386 |
$businessName = $data['business']->name; |
| 387 |
} else { |
| 388 |
$businessName = get_bloginfo('name'); |
| 389 |
} |
| 390 |
|
| 391 |
$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>.'); |
| 392 |
|
| 393 |
$data['email_footer'] = $footerText; |
| 394 |
|
| 395 |
$emailTypeForCustomerFooter = ['ticket_created_email_to_customer', 'ticket_replied_by_agent_email_to_customer', 'ticket_closed_by_agent_email_to_customer']; |
| 396 |
|
| 397 |
if (defined('FLUENTSUPPORTPRO') && in_array($data['email_type'], $emailTypeForCustomerFooter) |
| 398 |
&& !is_null($data['business']->email_footer)) { |
| 399 |
$data['email_footer'] = apply_filters('fluent_support/parse_smartcode_data', $data['business']->email_footer, $data); |
| 400 |
} |
| 401 |
|
| 402 |
$emailBody = $app->view->make('emails.ticket_template', $data); |
| 403 |
$emogrifier = new Emogrifier($emailBody, $this->emailTemplateCss()); |
| 404 |
$emogrifier->disableInvisibleNodeRemoval(); |
| 405 |
return $emogrifier->emogrify(); |
| 406 |
} |
| 407 |
|
| 408 |
public function getMailerHeaderWithCc($headers, $info) |
| 409 |
{ |
| 410 |
if (isset($info['cc_email'])) { |
| 411 |
$CcHeaders = !empty($info['cc_email']) && is_array($info['cc_email']) ? implode(', ', $info['cc_email']) : ''; |
| 412 |
$headers[] = $CcHeaders ? "Cc: $CcHeaders" : ''; |
| 413 |
} |
| 414 |
|
| 415 |
return $headers; |
| 416 |
} |
| 417 |
|
| 418 |
} |
| 419 |
|