PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.1.2
Fluent Support – Helpdesk & Customer Support Ticket System v2.1.2
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Hooks / Handlers / EmailNotificationHandler.php

EmailNotificationHandler.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.1.2, at app/Hooks/Handlers/EmailNotificationHandler.php

517 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ($this->shouldSendEmail($emailSettings, 'ticket_created_email_to_customer', $ticket, $customer)) {
27 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
28 'customer' => $customer,
29 'business' => $mailbox,
30 'ticket' => $ticket
31 ]);
32
33 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_created_email_to_customer');
34
35 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
36 'customer' => $customer,
37 'business' => $mailbox,
38 'ticket' => $ticket,
39 'email_type' => 'ticket_created_email_to_customer'
40 ]);
41
42 $headers = $mailbox->getMailerHeader();
43
44 $headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [
45 'cc_email' => $ticket->getSettingsValue('cc_email', []),
46 'hook_type' => 'ticket_created_email_to_customer'
47 ]);
48
49 $attachments = [];
50
51 if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) {
52 foreach ($files as $file) {
53 if ($file->driver == 'local') {
54 $filePath = $file->file_path;
55 } else {
56 $filePath = Arr::get($file->settings, 'local_temp_path');
57 }
58 if (file_exists($filePath)) {
59 $attachments[] = $filePath;
60 }
61 }
62 }
63
64 Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments);
65 }
66
67 // let's send email to admin if enabled
68 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_email_to_admin');
69 if ($this->shouldSendEmail($emailSettings, 'ticket_created_email_to_admin', $ticket, $customer)
70 && is_email($mailbox->settings['admin_email_address'])
71 ) {
72
73 $mailTo = Arr::get($mailbox->settings, 'admin_email_address');
74
75 if (!$mailTo || !is_email($mailTo)) {
76 return;
77 }
78
79 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
80 'customer' => $customer,
81 'business' => $mailbox,
82 'ticket' => $ticket
83 ]);
84
85 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_created_email_to_admin');
86
87 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
88 'customer' => $customer,
89 'business' => $mailbox,
90 'ticket' => $ticket,
91 'email_type' => 'ticket_created_email_to_admin'
92 ]);
93
94 $headers = $mailbox->getMailerHeader();
95
96 $attachments = [];
97
98 if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) {
99 foreach ($files as $file) {
100
101 if ($file->driver == 'local') {
102 $filePath = $file->file_path;
103 } else {
104 $filePath = Arr::get($file->settings, 'local_temp_path');
105 }
106 if (file_exists($filePath)) {
107 $attachments[] = $filePath;
108 }
109
110 }
111 }
112
113 Mailer::send($mailTo, $subject, $emailBody, $headers, $attachments);
114 }
115 }
116
117 public function agentReplied($response, $ticket, $agent)
118 {
119 $mailbox = $ticket->mailbox;
120
121
122 if (!$mailbox) {
123 return false;
124 }
125
126 // Let's send welcome email to customer if enabled
127 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_agent_email_to_customer');
128
129
130 if ($emailSettings && $emailSettings['status'] == 'yes') {
131
132 $ticket->load('customer');
133 $customer = $ticket->customer;
134
135 if ($customer->status == 'inactive') {
136 return false;
137 }
138
139 if (!$this->shouldSendEmail($emailSettings, 'ticket_replied_by_agent_email_to_customer', $ticket, $customer)) {
140 return false;
141 }
142
143 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
144 'customer' => $customer,
145 'business' => $mailbox,
146 'ticket' => $ticket,
147 'response' => $response,
148 'agent' => $agent
149 ]);
150
151 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_replied_by_agent_email_to_customer');
152
153 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
154 'customer' => $customer,
155 'business' => $mailbox,
156 'ticket' => $ticket,
157 'response' => $response,
158 'agent' => $agent,
159 'email_type' => 'ticket_replied_by_agent_email_to_customer'
160 ]);
161
162
163 $headers = $mailbox->getMailerHeader();
164 $headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [
165 'cc_email' => $response->getSettingsValue('cc_email', []),
166 'hook_type' => 'ticket_replied_by_agent_email_to_customer'
167 ]);
168 $attachments = [];
169
170 if ($emailSettings['send_attachments'] == 'yes' && ($files = $response->attachments)) {
171 foreach ($files as $file) {
172 if ($file->driver == 'local') {
173 $filePath = $file->file_path;
174 } else {
175 $filePath = Arr::get($file->settings, 'local_temp_path');
176 }
177 if (file_exists($filePath)) {
178 $attachments[] = $filePath;
179 }
180 }
181 }
182
183 Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments);
184 }
185 }
186
187 public function closedByAgent($ticket, $agent)
188 {
189 $mailbox = $ticket->mailbox;
190 if (!$mailbox) {
191 return;
192 }
193
194 // Let's send welcome email to customer if enabled
195 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_closed_by_agent_email_to_customer');
196 if ($emailSettings && $emailSettings['status'] == 'yes') {
197
198 $ticket->load('customer');
199 $customer = $ticket->customer;
200
201 if ($customer->status == 'inactive') {
202 return false;
203 }
204
205 if (!$this->shouldSendEmail($emailSettings, 'ticket_closed_by_agent_email_to_customer', $ticket, $customer)) {
206 return false;
207 }
208
209 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
210 'customer' => $customer,
211 'business' => $mailbox,
212 'ticket' => $ticket,
213 'agent' => $agent,
214 ]);
215
216 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_closed_by_agent_email_to_customer');
217
218 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
219 'customer' => $customer,
220 'business' => $mailbox,
221 'ticket' => $ticket,
222 'agent' => $agent,
223 'email_type' => 'ticket_closed_by_agent_email_to_customer'
224 ]);
225
226 $headers = $mailbox->getMailerHeader();
227 $headers = apply_filters('fluent_support/mail_to_customer_header', $headers, [
228 'cc_email' => $ticket->getSettingsValue('cc_email', []),
229 'hook_type' => 'ticket_closed_by_agent_email_to_customer'
230 ]);
231 Mailer::send($customer->email, $subject, $emailBody, $headers);
232 }
233 }
234
235 public function customerReplied($response, $ticket, $customer)
236 {
237 $mailbox = $ticket->mailbox;
238 if (!$mailbox) {
239 return;
240 }
241
242 // Let's send welcome email to customer if enabled
243 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_replied_by_customer_email_to_admin');
244 if ($this->shouldSendEmail($emailSettings, 'ticket_replied_by_customer_email_to_admin', $ticket, $customer)) {
245
246 $ticket->load('agent');
247 $agent = $ticket->agent;
248
249 $emailTo = $mailbox->settings['admin_email_address'];
250 if ($agent) {
251 $emailTo = $agent->email;
252 }
253
254 if (!$emailTo || !is_email($emailTo)) {
255 return;
256 }
257
258 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
259 'customer' => $customer,
260 'business' => $mailbox,
261 'ticket' => $ticket,
262 'response' => $response,
263 'agent' => $agent
264 ]);
265
266 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_replied_by_customer_email_to_admin');
267
268 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
269 'customer' => $customer,
270 'business' => $mailbox,
271 'ticket' => $ticket,
272 'response' => $response,
273 'agent' => $agent,
274 'email_type' => 'ticket_replied_by_customer_email_to_admin'
275 ]);
276
277 $headers = $mailbox->getMailerHeader();
278 $attachments = [];
279
280 if ($emailSettings['send_attachments'] == 'yes' && ($files = $response->attachments)) {
281 foreach ($files as $file) {
282 if ($file->driver == 'local') {
283 $filePath = $file->file_path;
284 } else {
285 $filePath = Arr::get($file->settings, 'local_temp_path');
286 }
287 if (file_exists($filePath)) {
288 $attachments[] = $filePath;
289 }
290 }
291 }
292
293 Mailer::send($emailTo, $subject, $emailBody, $headers, $attachments);
294 }
295 }
296
297 public function onAgentAssign($agent, $ticket, $assigner)
298 {
299 $currentUser = Helper::getAgentByUserId();
300
301 if ($currentUser && $currentUser->user_id == $agent->user_id) {
302 return;
303 }
304
305 $mailbox = $ticket->mailbox;
306
307 if (!$mailbox) {
308 return;
309 }
310
311 // Let's send notification email to agent if enabled
312 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_agent_on_change');
313
314 if ($this->shouldSendEmail($emailSettings, 'ticket_agent_on_change', $ticket, $agent)) {
315
316 if ($agent) {
317 $emailTo = $agent->email;
318 }
319
320 if (!$emailTo || !is_email($emailTo)) {
321 return;
322 }
323
324 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
325 'business' => $mailbox,
326 'ticket' => $ticket,
327 'agent' => $agent,
328 'assigner' => $assigner
329 ]);
330
331 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_agent_on_change');
332
333 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
334 'business' => $mailbox,
335 'ticket' => $ticket,
336 'agent' => $agent,
337 'assigner' => $assigner,
338 'email_type' => 'ticket_agent_on_change'
339 ]);
340
341 $headers = $mailbox->getMailerHeader();
342
343 Mailer::send($emailTo, $subject, $emailBody, $headers);
344 }
345 }
346
347 public function ticketCreatedByAgent($ticket, $customer, $agent)
348 {
349 $mailbox = $ticket->mailbox;
350
351 if (!$mailbox) {
352 return;
353 }
354
355 // Let's send welcome email to customer if enabled
356 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_by_agent_email_to_customer');
357 if ($this->shouldSendEmail($emailSettings, 'ticket_created_by_agent_email_to_customer', $ticket, $customer)) {
358 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
359 'customer' => $customer,
360 'agent' => $agent,
361 'business' => $mailbox,
362 'ticket' => $ticket
363 ]);
364
365 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_created_by_agent_email_to_customer');
366
367 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
368 'customer' => $customer,
369 'business' => $mailbox,
370 'agent' => $agent,
371 'ticket' => $ticket,
372 'email_type' => 'ticket_created_by_agent_email_to_customer'
373 ]);
374
375 $headers = $mailbox->getMailerHeader();
376
377 $attachments = [];
378
379 if ($emailSettings['send_attachments'] == 'yes' && ($files = $ticket->attachments)) {
380 foreach ($files as $file) {
381 if ($file->driver == 'local') {
382 $filePath = $file->file_path;
383 } else {
384 $filePath = Arr::get($file->settings, 'local_temp_path');
385 }
386 if (file_exists($filePath)) {
387 $attachments[] = $filePath;
388 }
389 }
390 }
391
392 Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments);
393 }
394 }
395
396 public function ticketCreatedByAgentOnBehalf($response, $ticket, $agent)
397 {
398 $mailbox = $ticket->mailbox;
399
400 if (!$mailbox) {
401 return;
402 }
403
404 $emailSettings = (new Settings())->getBoxEmailSettings($mailbox, 'ticket_created_by_agent_on_behalf_email_to_customer');
405 if ($this->shouldSendEmail($emailSettings, 'ticket_created_by_agent_on_behalf_email_to_customer', $ticket)) {
406
407 $ticket->load('customer');
408 $customer = $ticket->customer;
409
410 if ($customer->status == 'inactive') {
411 return false;
412 }
413
414 $subject = apply_filters('fluent_support/parse_smartcode_data', $emailSettings['email_subject'], [
415 'customer' => $customer,
416 'agent' => $agent,
417 'business' => $mailbox,
418 'ticket' => $ticket,
419 'response' => $response
420 ]);
421
422 $subject = apply_filters('fluent_support/ticket_email_subject', $subject, $ticket, 'ticket_created_by_agent_on_behalf_email_to_customer');
423
424 $emailBody = $this->parseEmailBody($emailSettings['email_body'], [
425 'customer' => $customer,
426 'business' => $mailbox,
427 'agent' => $agent,
428 'ticket' => $ticket,
429 'response' => $response,
430 'email_type' => 'ticket_created_by_agent_on_behalf_email_to_customer'
431 ]);
432
433 $headers = $mailbox->getMailerHeader();
434
435 $attachments = [];
436
437 if ($emailSettings['send_attachments'] == 'yes') {
438 $files = $response->attachments;
439
440 // Agent-initiated ticket uploads are stored on the ticket, while this email
441 // is rendered from the synthetic first response created during ticket creation.
442 if ((!$files || $files->isEmpty()) && $ticket->attachments) {
443 $files = $ticket->attachments;
444 }
445
446 foreach ($files as $file) {
447 if ($file->driver == 'local') {
448 $filePath = $file->file_path;
449 } else {
450 $filePath = Arr::get($file->settings, 'local_temp_path');
451 }
452 if (file_exists($filePath)) {
453 $attachments[] = $filePath;
454 }
455 }
456 }
457
458 Mailer::send($customer->email, $subject, $emailBody, $headers, $attachments);
459 }
460 }
461
462 private function emailTemplateCss()
463 {
464 $app = App::getInstance();
465 return $app->view->make('emails.styles');
466 }
467
468 protected function parseEmailBody($emailBody, $data)
469 {
470 $data['email_body'] = apply_filters('fluent_support/parse_smartcode_data', $emailBody, $data);
471
472 $app = App::getInstance();
473
474 if (isset($data['business'])) {
475 $businessName = $data['business']->name;
476 } else {
477 $businessName = get_bloginfo('name');
478 }
479
480 $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>.');
481
482 $data['email_footer'] = $footerText;
483
484 $emailTypeForCustomerFooter = ['ticket_created_email_to_customer', 'ticket_replied_by_agent_email_to_customer', 'ticket_closed_by_agent_email_to_customer'];
485
486 if (defined('FLUENTSUPPORTPRO') && in_array($data['email_type'], $emailTypeForCustomerFooter)
487 && !is_null($data['business']->email_footer)) {
488 $data['email_footer'] = apply_filters('fluent_support/parse_smartcode_data', $data['business']->email_footer, $data);
489 }
490
491 $emailBody = $app->view->make('emails.ticket_template', $data);
492 $emogrifier = new Emogrifier($emailBody, $this->emailTemplateCss());
493 $emogrifier->disableInvisibleNodeRemoval();
494 return $emogrifier->emogrify();
495 }
496
497 protected function shouldSendEmail($emailSettings, $emailType, $ticket, $person = null)
498 {
499 if (!$emailSettings || $emailSettings['status'] != 'yes') {
500 return false;
501 }
502
503 return apply_filters('fluent_support/should_send_notification', true, 'email', $emailType, $ticket, $person);
504 }
505
506 public function getMailerHeaderWithCc($headers, $info)
507 {
508 if (isset($info['cc_email'])) {
509 $CcHeaders = !empty($info['cc_email']) && is_array($info['cc_email']) ? implode(', ', $info['cc_email']) : '';
510 $headers[] = $CcHeaders ? "Cc: $CcHeaders" : '';
511 }
512
513 return $headers;
514 }
515
516 }
517