From 70721df23c3e2a346e3dd98f17a099db9070963d Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Tue, 22 Apr 2025 16:31:32 +0530 Subject: [PATCH] CHANGE_MULTIPL_TO_MAIL : RV --- app/Config/Routes.php | 1 + app/Controllers/MasterController.php | 18 ++ app/Controllers/TicketController.php | 67 +++---- app/Helpers/GmailResponseHandler.php | 6 +- app/Helpers/MailHelper.php | 268 ++++++++++++++------------- 5 files changed, 197 insertions(+), 163 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index a6e3eb12..b9d8b131 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -32,6 +32,7 @@ $routes->get("sendextraparam", "ClientController::sendextraparam"); $routes->get("updateRenewalData", "ClientController::updateRenewalData"); $routes->get("updateRenewalDataNotExistingClient", "ClientController::updateRenewalDataNotExistingClient"); $routes->get("updateRenewalInsurerData", "ClientController::updateRenewalInsurerData"); +$routes->get("sendMutipleToEmails", "MasterController::sendMutipleToEmails"); // $routes->post("iAgreeForAddOn", "EmployeeRestController::iAgreeForAddOn"); // $routes->get("sendCroneRemainderMail", "DashboardController::sendCroneRemainderMail"); // $routes->post("employeeUpload", "EmployeeRestController::employeeUpload"); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 02409c2c..fa094bb8 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -1829,4 +1829,22 @@ class MasterController extends AdminController echo "################################################################\n\n"; } + public function sendMutipleToEmails() + { + + $message = '

Request for Quotation (RFQ)

Dear {{RECIPIENT_NAME}},

We are reaching out to request a quotation for the following insurance coverage. Please review the details below and provide your quote at your earliest convenience.

RFQ Details

Client name{{CLIENT_NAME}}
Coverage Type{{POLICY_LONG_NAME}}
Policy Start Date{{POLICY_START_DATE}}
Policy Duration{{DURATION}}
Please note: Additional terms and details are included in the attachment for your reference.

Please feel free to reach out if you require any further information to prepare the quote. We look forward to receiving your proposal.

Best regards,

Nhance India Pvt Ltd

'; + $attachments = [ + ["filePath" => ROOTPATH."public/sample_excel/sample_addition.xls","fileName" => "sample_addition.xls"], + ["filePath" => ROOTPATH."public/sample_excel/sample_inception.xls","fileName" => "sample_inception.xls"], + ["filePath" => ROOTPATH."public/assets/images/login_bg.jpg","fileName" => "login_bg.jpg"] + ]; + $email_id = 'venkateshraman786@gmail.com'; + $params = ['mail'=>$email_id]; + $multi_mails = ['venkateshraman786@gmail.com', 'gowtham.manavalan.la@gmail.com', 'venkatesh.r@venbainfotech.com']; + $common = ['mail_type'=>'test_mail_cli']; + $bcc = "vitvelz@gmail.com,velmurugan.s@venbainfotech.com,srinivas.saravanan@venbainfotech.com"; + $result = MailHelper::send_email(['mail' => $multi_mails, 'bcc'=>$bcc, 'params'=>$params,'subject' => 'Send Multiple " To " emails','message' => $message,'attachments' => $attachments,'common'=>$common]); + dd($result); + } + } \ No newline at end of file diff --git a/app/Controllers/TicketController.php b/app/Controllers/TicketController.php index b9d62ff5..ab5745fa 100644 --- a/app/Controllers/TicketController.php +++ b/app/Controllers/TicketController.php @@ -878,24 +878,24 @@ class TicketController extends BaseController } // Construct Mail Data - $mailData[] = [ - 'mail' => $ticket_data['emp_mail'], + $mailData = [ + 'mail' => [$ticket_data['emp_mail'], $ticket_data['emp_personal_mail'] ?? ""], 'subject' => $subject, 'message' => $message, 'cc' => $ticket_data['common_mails'] ?? '', 'attachments' => [] ]; - // if the employee personal mail is not empty then send the mail to the employee personal mail - if(!empty($ticket_data['emp_personal_mail'])){ - $mailData[] = [ - 'mail' => $ticket_data['emp_personal_mail'], - 'subject' => $subject, - 'message' => $message, - 'cc' => $ticket_data['common_mails'] ?? '', - 'attachments' => [] - ]; - } + // // if the employee personal mail is not empty then send the mail to the employee personal mail + // if(!empty($ticket_data['emp_personal_mail'])){ + // $mailData[] = [ + // 'mail' => [$ticket_data['emp_mail'], $ticket_data['emp_personal_mail'] ?? ""], + // 'subject' => $subject, + // 'message' => $message, + // 'cc' => $ticket_data['common_mails'] ?? '', + // 'attachments' => [] + // ]; + // } // print_r($mailData); die; @@ -986,28 +986,28 @@ class TicketController extends BaseController $this->myLogger->logme('error', "Fetched ACM emails"); - if(!empty($ticket_data['emp_personal_mail'])){ + // if(!empty($ticket_data['emp_personal_mail'])){ - $this->myLogger->logme('error', "Send employee personal mail start"); + // $this->myLogger->logme('error', "Send employee personal mail start"); - $emailPersonalData = [ - 'mail' => $ticket_data['emp_personal_mail'], - 'subject' => $mail_data['mail_subject'], - 'message' => $mail_data['mail_content'], - 'common' => [], - 'cc' => $acm_mails['common_mails'], - 'attachments' => [] - ]; + // $emailPersonalData = [ + // 'mail' => $ticket_data['emp_personal_mail'], + // 'subject' => $mail_data['mail_subject'], + // 'message' => $mail_data['mail_content'], + // 'common' => [], + // 'cc' => $acm_mails['common_mails'], + // 'attachments' => [] + // ]; - $this->sendTrigger($emailPersonalData); + // $this->sendTrigger($emailPersonalData); - $this->myLogger->logme('error', "Send employee personal mail successfully"); - }else{ - $this->myLogger->logme('error', "Send employee personal mail is empty"); - } + // $this->myLogger->logme('error', "Send employee personal mail successfully"); + // }else{ + // $this->myLogger->logme('error', "Send employee personal mail is empty"); + // } $emailData = [ - 'mail' => $mail_data['emp_mail'], + 'mail' => [$mail_data['emp_mail'], $ticket_data['emp_personal_mail'] ?? ""], 'subject' => $mail_data['mail_subject'], 'message' => $mail_data['mail_content'], 'common' => [], @@ -1029,10 +1029,11 @@ class TicketController extends BaseController if (!empty($auto_mail_enable) && $auto_mail_enable['is_auto_mail'] == 1) { $mail_content = $this->constructMailContent($ticket_id); // print_r($mail_content); die; - foreach ($mail_content as $key => $value) { - $mail_responce = $this->sendTrigger($value); - $this->myLogger->logme('error', '{data} - Auto Mail Sent, Successfully', ['data' => $key + 1]); - } + // foreach ($mail_content as $key => $value) { + $mail_responce = $this->sendTrigger($mail_content); + // $this->myLogger->logme('error', '{data} - Auto Mail Sent, Successfully', ['data' => $key + 1]); + $this->myLogger->logme('error', 'Auto Mail Sent, Successfully'); + // } } elseif (!empty($auto_mail_enable) && $auto_mail_enable['is_auto_mail'] == 0) { $this->myLogger->logme('error', 'Auto Mail Not Sent, Reason: Automail Not Enabled'); } else { @@ -1543,7 +1544,7 @@ class TicketController extends BaseController $sent_message_data['ticket_id'] = $ticket_id; $sent_message_data['sender'] = 'staff'; - $sent_message_data['emp_mail'] = $mail_sent_status->data->params->mail; + $sent_message_data['emp_mail'] = isset($mail_sent_status->data->params->mail[0]) ? $mail_sent_status->data->params->mail[0] ?? null : $mail_sent_status->data->params->mail ?? null; $sent_message_data['mail_subject'] = $mail_sent_status->data->params->subject; $sent_message_data['mail_content'] = $mail_sent_status->data->params->message; diff --git a/app/Helpers/GmailResponseHandler.php b/app/Helpers/GmailResponseHandler.php index 8ff0ed35..beb731a7 100644 --- a/app/Helpers/GmailResponseHandler.php +++ b/app/Helpers/GmailResponseHandler.php @@ -74,7 +74,11 @@ class GmailResponseHandler{ if(in_array($key,$arr)){ $data[$key] = isset($value)?json_encode($value):null; }else{ - $data[$key] = isset($value)?$value:null; + if($key == "mail" && is_array($value)){ + $data[$key] = isset($value)?json_encode($value):null; + }else{ + $data[$key] = isset($value)?$value:null; + } } } } diff --git a/app/Helpers/MailHelper.php b/app/Helpers/MailHelper.php index 8fc4884b..cce74a9a 100755 --- a/app/Helpers/MailHelper.php +++ b/app/Helpers/MailHelper.php @@ -292,154 +292,164 @@ class MailHelper } } - public static function send_email($params) -{ - $myLogger = \Config\Services::mylogger(); - - $emaill = $params['mail']; - $subject = $params['subject']; - $message = $params['message']; - $attachments = isset($params['attachments']) ? $params['attachments'] : []; - $common = isset($params['common']) ? $params['common'] : ''; - $bcc = isset($params['bcc']) ? $params['bcc'] : ''; - $cc = isset($params['cc']) ? $params['cc'] : ''; - - $from_address = isset($params['from_mail']) && !empty($params['from_mail']) ? $params['from_mail'] : getenv('email.fromEmail'); - // $from_address = "claims@nhanceindia.in"; - try { - $curl = curl_init(); - - $postData = [ - 'from' => [ - 'address' => $from_address - ], - 'to' => [ - [ + public static function send_email($params) + { + $myLogger = \Config\Services::mylogger(); + if (is_array($params['mail'])) { + $emails = []; + + foreach ($params['mail'] as $key => $emaill) { + $emails[] = [ 'email_address' => [ 'address' => $emaill ] + ]; + } + } else { + $emails[] = [ + 'email_address' => [ + 'address' => $params['mail'] ] - ], - 'subject' => $subject, - 'htmlbody' => $message - ]; - - // Add CC recipients if provided - if (!empty($cc)) { - $ccList = explode(',', $cc); - $ccList = array_map('trim', $ccList); - $postData['cc'] = array_map(function($email) { - return [ - 'email_address' => [ - 'address' => $email - ] - ]; - }, $ccList); - } - // Add BCC if present - if (!empty($bcc)) { - $bccList = explode(',', $bcc); - $bccList = array_map('trim', $bccList); - $postData['bcc'] = array_map(function($email) { - return [ - 'email_address' => [ - 'address' => $email - ] - ]; - }, $bccList); + ]; } - // Add BCC recipients if provided - // if (!empty($bcc)) { - // $postData['bcc'] = []; - // // Handle both string and array inputs for BCC - // $bccEmails = is_array($bcc) ? $bcc : [$bcc]; - // foreach ($bccEmails as $bccEmail) { - // $postData['bcc'][] = [ - // 'email_address' => [ - // 'address' => $bccEmail - // ] - // ]; - // } - // } + $subject = $params['subject']; + $message = $params['message']; + $attachments = isset($params['attachments']) ? $params['attachments'] : []; + $common = isset($params['common']) ? $params['common'] : ''; + $bcc = isset($params['bcc']) ? $params['bcc'] : ''; + $cc = isset($params['cc']) ? $params['cc'] : ''; - // Handle attachments - if (!empty($attachments)) { - $postData['attachments'] = []; - foreach ($attachments as $attachment) { - if (isset($attachment['filePath']) && file_exists($attachment['filePath'])) { - // Determine MIME type dynamically - $fileMimeType = mime_content_type($attachment['filePath']); - - $postData['attachments'][] = [ - 'content' => base64_encode(file_get_contents($attachment['filePath'])), - 'name' => $attachment['fileName'], - 'mime_type' => $fileMimeType, + $from_address = isset($params['from_mail']) && !empty($params['from_mail']) ? $params['from_mail'] : getenv('email.fromEmail'); + // $from_address = "claims@nhanceindia.in"; + try { + $curl = curl_init(); + + $postData = [ + 'from' => [ + 'address' => $from_address + ], + 'to' => $emails, + 'subject' => $subject, + 'htmlbody' => $message + ]; + + // Add CC recipients if provided + if (!empty($cc)) { + $ccList = explode(',', $cc); + $ccList = array_map('trim', $ccList); + $postData['cc'] = array_map(function ($email) { + return [ + 'email_address' => [ + 'address' => $email + ] ]; + }, $ccList); + } + // Add BCC if present + if (!empty($bcc)) { + $bccList = explode(',', $bcc); + $bccList = array_map('trim', $bccList); + $postData['bcc'] = array_map(function ($email) { + return [ + 'email_address' => [ + 'address' => $email + ] + ]; + }, $bccList); + } + + // Add BCC recipients if provided + // if (!empty($bcc)) { + // $postData['bcc'] = []; + // // Handle both string and array inputs for BCC + // $bccEmails = is_array($bcc) ? $bcc : [$bcc]; + // foreach ($bccEmails as $bccEmail) { + // $postData['bcc'][] = [ + // 'email_address' => [ + // 'address' => $bccEmail + // ] + // ]; + // } + // } + + // Handle attachments + if (!empty($attachments)) { + $postData['attachments'] = []; + foreach ($attachments as $attachment) { + if (isset($attachment['filePath']) && file_exists($attachment['filePath'])) { + // Determine MIME type dynamically + $fileMimeType = mime_content_type($attachment['filePath']); + + $postData['attachments'][] = [ + 'content' => base64_encode(file_get_contents($attachment['filePath'])), + 'name' => $attachment['fileName'], + 'mime_type' => $fileMimeType, + ]; + } } } - } - curl_setopt_array($curl, [ - CURLOPT_URL => "https://api.zeptomail.in/v1.1/email", - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => "POST", - CURLOPT_POSTFIELDS => json_encode($postData), - CURLOPT_HTTPHEADER => [ - "accept: application/json", - "authorization: Zoho-enczapikey " . getenv('ZEPTO_API_KEY'), - "cache-control: no-cache", - "content-type: application/json", - ], - ]); + curl_setopt_array($curl, [ + CURLOPT_URL => "https://api.zeptomail.in/v1.1/email", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => json_encode($postData), + CURLOPT_HTTPHEADER => [ + "accept: application/json", + "authorization: Zoho-enczapikey " . getenv('ZEPTO_API_KEY'), + "cache-control: no-cache", + "content-type: application/json", + ], + ]); - $mail_result = curl_exec($curl); - $err = curl_error($curl); - curl_close($curl); + $mail_result = curl_exec($curl); + $err = curl_error($curl); + curl_close($curl); - $res['params'] = $params; - $MailDataHelper = new GmailResponseHandler(); - $apiResponse = json_decode($mail_result, true); - $res['zepto_api'] = $apiResponse; - - if (isset($apiResponse['error'])) { + $res['params'] = $params; + $MailDataHelper = new GmailResponseHandler(); + $apiResponse = json_decode($mail_result, true); + $res['zepto_api'] = $apiResponse; + + if (isset($apiResponse['error'])) { + $response = $res; + $MailDataHelper->receive_and_distribute_param_to_functions($response); + return json_encode([ + 'status' => 'failed', + 'code' => 404, + 'message' => 'Email Sent Failed...' . json_encode($params['mail']), + 'data' => $res + ], 404); + } + + if (isset($apiResponse['data'])) { + $response = $res; + $MailDataHelper->receive_and_distribute_param_to_functions($response); + return json_encode([ + 'status' => 'success', + 'code' => 200, + 'message' => 'Email Sent Successfully...' . json_encode($params['mail']), + 'data' => $res + ], 200); + } + } catch (\Exception $e) { + $msg['error'] = $e->getMessage(); + $msg['params'] = $params; $response = $res; $MailDataHelper->receive_and_distribute_param_to_functions($response); return json_encode([ 'status' => 'failed', - 'code' => 404, - 'message' => 'Email Sent Failed...' . $emaill, - 'data' => $res - ], 404); + 'code' => 500, + 'data' => $msg + ], 500); } - - if (isset($apiResponse['data'])) { - $response = $res; - $MailDataHelper->receive_and_distribute_param_to_functions($response); - return json_encode([ - 'status' => 'success', - 'code' => 200, - 'message' => 'Email Sent Successfully...' . $emaill, - 'data' => $res - ], 200); - } - - } catch (\Exception $e) { - $msg['error'] = $e->getMessage(); - $msg['params'] = $params; - $response = $res; - $MailDataHelper->receive_and_distribute_param_to_functions($response); - return json_encode([ - 'status' => 'failed', - 'code' => 500, - 'data' => $msg - ], 500); } -} + public static function bulk_mail(array $mails = []) { $model = new JobModel();