diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 1b73489f..ba8c0f25 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -400,6 +400,7 @@ $routes->get("driveListFiles", "GoogleDriveController::listFiles"); $routes->post('dmsSearch', 'PolicyTransactionController::dmsSearch', ['filter' => 'authMVC']); $routes->get('dmsSearch', 'PolicyTransactionController::dmsSearch', ['filter' => 'authMVC']); $routes->get('downloadGdriveFile', 'GoogleDriveController::downloadGdriveFile', ['filter' => 'authMVC']); +$routes->get('cli/sendZeptoMail', 'MasterController::testZeptoSMTP'); $routes->cli('cli/processjob', 'JobWorker::processJob'); $routes->cli('cli/processjobs', 'JobWorker::processJobs'); @@ -407,6 +408,7 @@ $routes->cli('cli/processjobs', 'JobWorker::processJobs'); $routes->get("processjob", "JobWorker::processJob"); $routes->cli('cli/new_gmail_token', 'MasterController::generateNewGmailAPIToken'); $routes->cli('cli/send_mail_cli', 'MasterController::testGmailAPIViaCLI'); +$routes->cli('cli/sendZeptoMail', 'MasterController::testZeptoSMTP'); $routes->cli('cli/check_bounce_mail_cli', 'MasterController::testCheckBounceMails'); $routes->cli('cli/app_check_list', 'MasterController::appCheckList'); $routes->cli('cli/new_gdrive_token', 'GoogleDriveController::generateNewGoogleDriveAccessToken'); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 6a81ebf5..d643952b 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -1641,7 +1641,35 @@ class MasterController extends AdminController print_r($res); } - + + public function testZeptoSMTP() { + $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 = 'srinivas.saravanan@venbainfotech.com'; + $common = ['mail_type'=>'test_mail_cli']; + // Send email and get response + $result = MailHelper::send_email(['mail' => $email_id, 'subject' => 'Mail Via CLI', 'message' => $message,'attachments' => $attachments,'common'=>$common]); + + + log_message('error',json_encode($result)); + // Format the response for display/debugging + $response = [ + 'success' => $result['success'] ?? false, + 'smtp_data' => [ + 'last_reply' => $result['smtp_data']['last_reply'] ?? '', + 'error_info' => $result['smtp_data']['error_info'] ?? '' + ], + 'message_id' => $result['message_id'] ?? '', + 'timestamp' => $result['timestamp'] ?? '' + ]; + + // Return JSON response + return $this->response->setJSON($result); + } public function testCheckBounceMails() { $gmailapi = \Config\Services::gmailapi(); diff --git a/app/Helpers/ExcelMergeHelper.php b/app/Helpers/ExcelMergeHelper.php index fb593ec5..36306692 100644 --- a/app/Helpers/ExcelMergeHelper.php +++ b/app/Helpers/ExcelMergeHelper.php @@ -119,9 +119,9 @@ class ExcelMergeHelper { $newName = $sheetName; $counter = 1; - while ($mergedSpreadsheet->sheetNameExists($newName)) { - $newName = $sheetName . '_' . $counter++; - log_message('debug', "Sheet name already exists. Generating new name: {$newName}"); + while (in_array($newName, $mergedSpreadsheet->getSheetNames())) { + $newName = $sheetName . "_" . $counter++; + log_message('debug', "Sheet name already exists. Trying new name: {$newName}"); } // Clone the worksheet and set the new name diff --git a/app/Helpers/GmailResponseHandler.php b/app/Helpers/GmailResponseHandler.php index f2cb07f7..4a45f65c 100644 --- a/app/Helpers/GmailResponseHandler.php +++ b/app/Helpers/GmailResponseHandler.php @@ -77,17 +77,16 @@ class GmailResponseHandler{ } } } - if(isset($params['gmail_api']['status']) && $params['gmail_api']['status'] == 1 ){ - $data['gmail_api_status'] = isset($params['gmail_api']['status']) ? $params['gmail_api']['status'] : null; - $data['gmail_api_id'] = isset($params['gmail_api']['data']['id']) ? $params['gmail_api']['data']['id'] : null; - $data['received_message'] = isset($params['gmail_api']['data']['labelIds']) ? json_encode($params['gmail_api']['data']['labelIds']) : null; + if(isset($params['zepto_api']['data']) && $params['zepto_api']['message'] == 'OK' ){ + $data['gmail_api_status'] = isset($params['zepto_api']['data']) ? $params['zepto_api']['data'][0]['code'] : null; + $data['gmail_api_id'] = isset($params['zepto_api']['request_id']) ? $params['zepto_api']['request_id'] : null; + $data['received_message'] = isset($params['zepto_api']['data'][0]['message']) ? json_encode($params['zepto_api']['data'][0]['message']) : null; } - elseif(isset($params['gmail_api']['status']) && $params['gmail_api']['status'] == false ){ - $data['gmail_api_status'] = $params['gmail_api']['status']; - if(isset($params['gmail_api']['data'])){ - $data['received_message'] = json_encode($params['gmail_api']['data']); - } + elseif(isset($params['zepto_api']['error'])){ + $data['gmail_api_status'] = isset($params['zepto_api']['error']['code'])?$params['zepto_api']['error']['code']:null; + $data['received_message'] = json_encode($params['zepto_api']); + $data['gmail_api_id'] = isset($params['zepto_api']['error']['request_id']) ? $params['zepto_api']['error']['request_id'] : null; } $gmailModel = new GmailSentHistoryModel(); @@ -96,7 +95,9 @@ class GmailResponseHandler{ public function gmail_response_logger($params) { - $this->myLogger->logme('error',json_encode($params)); + if(isset($params['zepto_api']['error'])){ + $this->myLogger->logme('error',json_encode($params)); + } } public function client_id_flag_setter($params) @@ -119,7 +120,6 @@ class GmailResponseHandler{ $error = $e->getMessage(); $this->myLogger->logme('error','An Error Occured - '.$error); } - } - +} } \ No newline at end of file diff --git a/app/Helpers/MailHelper.php b/app/Helpers/MailHelper.php index 085c510f..91e955b0 100755 --- a/app/Helpers/MailHelper.php +++ b/app/Helpers/MailHelper.php @@ -1,177 +1,422 @@ setMailType('html'); +// $email->setFrom('bbone@venbait.in', 'Nhance'); + +// $email->setTo($emaill); + +// if (!empty($bcc)) { +// // Convert the comma-separated string into an array +// $bccList = explode(',', $bcc); +// // Trim whitespace from each email ID +// $bccList = array_map('trim', $bccList); +// // Set BCC recipients +// $email->setBCC($bccList); +// } + +// $email->setSubject($subject); + +// $email->setMessage($message); + +// if ($email->send()) { + +// return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...','data' => $emaill,],200); +// } else { +// $myLogger->logme('error', "mail sent failed - $emaill"); +// return json_encode(['status' => 'failed','code' => 404,'message'=>'Email Sent Failed...','data' => $emaill ],404); +// } + +// } catch (Exception $e) { +// $msg = $e->getMessage(); +// $myLogger->logme('error', "mail sent failed - $msg"); +// return json_encode(['status' => 'failed','code' => 500,'data' => $emaill],500); +// } +// } + +// public static function bulk_mail_smtp(array $mails = []) +// { +// // print_r();die; +// $model = new JobModel(); +// $start = microtime(true); +// $runtime= 0; +// try { +// foreach ( $mails as $index=>$mail) { +// $runtime = microtime(true) - $start; +// $send_mail = self::send_email($mail); +// } + +// return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); + +// } catch (\Throwable $th) { +// return json_encode(['status' => 'failed','code' => 500],500); +// } +// } + +// //using GmailAPI +// public static function send_email($params) +// { + +// // print_r($params);die; +// $myLogger = \Config\Services::mylogger(); +// $gmailapi = \Config\Services::gmailapi(); + +// $emaill = $params['mail']; +// $subject = $params['subject']; +// $message = $params['message']; +// $attachments = isset($params['attachments']) && count($params['attachments']) ? $params['attachments'] : []; +// $common = isset($params['common'])?$params['common']:''; +// //check BCC mail +// if (isset($params['bcc'])) { +// $bcc = $params['bcc']; +// if(!is_array($bcc)){ +// $bcc = explode(',', $bcc); +// } +// }else{ +// $bcc = []; +// } + +// //check CC mail +// if (isset($params['cc'])) { +// $cc = $params['cc']; +// if(!is_array($cc)){ +// $cc = explode(',', $cc); +// } +// }else{ +// $cc = []; +// } + +// //check REPLY TO mail +// $reply_to = ""; +// if(isset($params['reply_to']) && !empty($params['reply_to'])){ +// $reply_to = $params['reply_to']; +// } +// $MailDataHelper = new GmailResponseHandler(); + +// $res['params'] = $params; +// try { + +// $res['gmail_api'] = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc,$attachments); +// if($res['gmail_api']['status']) +// { +// $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); +// } +// else +// { +// $myLogger->logme('error', "mail sent failed - $emaill"); +// $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); +// } + +// } catch (Exception $e) { +// $msg['error'] = $e->getMessage(); +// $msg['params'] = $params; +// $myLogger->logme('error', "mail sent failed - $msg"); +// $response = $msg; +// $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 = []) +// { +// // print_r($mails);die; +// $model = new JobModel(); +// $start = microtime(true); +// $runtime= 0; +// try { +// foreach ( $mails as $index=>$mail) { +// $runtime = microtime(true) - $start; +// $send_mail = self::send_email($mail); +// } + +// return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); + +// } catch (\Throwable $th) { +// $error = $th->getMessage(); +// log_message('error',$error); +// return json_encode(['status' => 'failed','code' => 500,'error'=>$error],500); +// } +// } + +// } + + namespace App\Helpers; use Psr\Log\LoggerInterface; - use App\Controllers\BaseController; - -use PHPMailer\PHPMailer\PHPMailer; -use PHPMailer\PHPMailer\SMTP; -use PHPMailer\PHPMailer\Exception; - use App\Models\JobModel; class MailHelper { public static function send_email_smtp($params) { - - // print_r($params);die; $myLogger = \Config\Services::mylogger(); $emaill = $params['mail']; $subject = $params['subject']; $message = $params['message']; if (isset($params['bcc'])) { $bcc = $params['bcc']; - }else{ - $bcc= ''; + } else { + $bcc = ''; } + $from_address = getenv('email.fromEmail'); + + try { - - $email = \Config\Services::email(); - $email->setMailType('html'); - $email->setFrom('bbone@venbait.in', 'Nhance'); - - $email->setTo($emaill); + $curl = curl_init(); + $postData = [ + 'from' => [ + 'address' => $from_address + ], + 'to' => [ + [ + 'email_address' => [ + 'address' => $emaill + ] + ] + ], + 'subject' => $subject, + 'htmlbody' => $message + ]; + // Add BCC if present if (!empty($bcc)) { - // Convert the comma-separated string into an array $bccList = explode(',', $bcc); - // Trim whitespace from each email ID $bccList = array_map('trim', $bccList); - // Set BCC recipients - $email->setBCC($bccList); + $postData['bcc'] = array_map(function($email) { + return [ + 'email_address' => [ + 'address' => $email + ] + ]; + }, $bccList); } - $email->setSubject($subject); + 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", + ], + ]); - $email->setMessage($message); + $response = curl_exec($curl); + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $err = curl_error($curl); - if ($email->send()) { + curl_close($curl); + if ($err) { + $myLogger->logme('error', "mail sent failed - $emaill"); + return json_encode(['status' => 'failed', 'code' => 404, 'message' => 'Email Sent Failed...', 'data' => $emaill], 404); + } - return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...','data' => $emaill,],200); + $apiResponse = json_decode($response, true); + + if ($httpCode === 200) { + return json_encode(['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...', 'data' => $emaill], 200); } else { $myLogger->logme('error', "mail sent failed - $emaill"); - return json_encode(['status' => 'failed','code' => 404,'message'=>'Email Sent Failed...','data' => $emaill ],404); + return json_encode(['status' => 'failed', 'code' => 404, 'message' => 'Email Sent Failed...', 'data' => $emaill], 404); } - - } catch (Exception $e) { + + } catch (\Exception $e) { $msg = $e->getMessage(); $myLogger->logme('error', "mail sent failed - $msg"); - return json_encode(['status' => 'failed','code' => 500,'data' => $emaill],500); - } + return json_encode(['status' => 'failed', 'code' => 500, 'data' => $emaill], 500); + } } public static function bulk_mail_smtp(array $mails = []) { - // print_r();die; $model = new JobModel(); $start = microtime(true); - $runtime= 0; - try { - foreach ( $mails as $index=>$mail) { + $runtime = 0; + try { + foreach ($mails as $index => $mail) { $runtime = microtime(true) - $start; $send_mail = self::send_email($mail); - } - - return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); + } + + return json_encode(['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...'], 200); - } catch (\Throwable $th) { - return json_encode(['status' => 'failed','code' => 500],500); - } + } catch (\Throwable $th) { + return json_encode(['status' => 'failed', 'code' => 500], 500); + } } - //using GmailAPI - public static function send_email($params) + public static function send_email($params) { - - // print_r($params);die; $myLogger = \Config\Services::mylogger(); - $gmailapi = \Config\Services::gmailapi(); - + $emaill = $params['mail']; $subject = $params['subject']; $message = $params['message']; - $attachments = isset($params['attachments']) && count($params['attachments']) ? $params['attachments'] : []; - $common = isset($params['common'])?$params['common']:''; - //check BCC mail - if (isset($params['bcc'])) { - $bcc = $params['bcc']; - if(!is_array($bcc)){ - $bcc = explode(',', $bcc); - } - }else{ - $bcc = []; - } + $attachments = isset($params['attachments']) ? $params['attachments'] : []; + $common = isset($params['common']) ? $params['common'] : ''; - //check CC mail - if (isset($params['cc'])) { - $cc = $params['cc']; - if(!is_array($cc)){ - $cc = explode(',', $cc); - } - }else{ - $cc = []; - } - - //check REPLY TO mail - $reply_to = ""; - if(isset($params['reply_to']) && !empty($params['reply_to'])){ - $reply_to = $params['reply_to']; - } - $MailDataHelper = new GmailResponseHandler(); - - $res['params'] = $params; + $from_address = getenv('email.fromEmail'); try { - - $res['gmail_api'] = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc,$attachments); - if($res['gmail_api']['status']) - { - $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); - } - else - { - $myLogger->logme('error', "mail sent failed - $emaill"); - $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); - } - - } catch (Exception $e) { + $curl = curl_init(); + + $postData = [ + 'from' => [ + 'address' => $from_address + ], + 'to' => [ + [ + 'email_address' => [ + 'address' => $emaill + ] + ] + ], + 'subject' => $subject, + 'htmlbody' => $message + ]; + + // 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, // Add mime_type field + ]; + } + } + } + + + 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); + // log_message('error','inside mail helper'); + // log_message('error', $response.' error->'.$err);die(); + $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...' . $emaill, + 'data' => $res + ], 404); + } + + // $apiResponse = json_decode($response, true); + + // Check if request_id exists in response (indicates success) + 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; - $myLogger->logme('error', "mail sent failed - $msg"); - $response = $msg; + $response = $res; $MailDataHelper->receive_and_distribute_param_to_functions($response); - return json_encode(['status' => 'failed','code' => 500,'data' => $msg],500); + return json_encode([ + 'status' => 'failed', + 'code' => 500, + 'data' => $msg + ], 500); } - - } - public static function bulk_mail(array $mails = []) { - // print_r($mails);die; $model = new JobModel(); $start = microtime(true); - $runtime= 0; - try { - foreach ( $mails as $index=>$mail) { + $runtime = 0; + try { + foreach ($mails as $index => $mail) { $runtime = microtime(true) - $start; $send_mail = self::send_email($mail); + } + + return json_encode(['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...'], 200); + + } catch (\Throwable $th) { + $error = $th->getMessage(); + log_message('error', $error); + return json_encode(['status' => 'failed', 'code' => 500, 'error' => $error], 500); } - - return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); - - } catch (\Throwable $th) { - $error = $th->getMessage(); - log_message('error',$error); - return json_encode(['status' => 'failed','code' => 500,'error'=>$error],500); - } } - } ?> \ No newline at end of file diff --git a/composer.json b/composer.json index 73453d9c..c456241c 100755 --- a/composer.json +++ b/composer.json @@ -17,15 +17,15 @@ "dompdf/dompdf": "^2.0", "firebase/php-jwt": "^6.10", "google/apiclient": "^2.15.0", + "kreait/firebase-php": "^7.0", "laminas/laminas-escaper": "^2.9", "php-amqplib/php-amqplib": "^2.8", "phpmailer/phpmailer": "^6.9", "phpoffice/phpspreadsheet": "^2.1", + "psr/http-message": "^1.0", "psr/log": "^1.1", "slim/slim": "^4.13", - "zircote/swagger-php": "^4.8", - "psr/http-message": "^1.0", - "kreait/firebase-php":"^7.0" + "zircote/swagger-php": "^4.8" }, "require-dev": { "codeigniter/coding-standard": "^1.7",