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