nhance/app/Helpers/MailHelper.php
2026-06-02 14:26:20 +05:30

491 lines
17 KiB
PHP
Executable File

<?php
// 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= '';
// }
// try {
// $email = \Config\Services::email();
// $email->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 App\Models\JobModel;
class MailHelper
{
private const SYSTEM_EMAIL_FOOTER = '<div style="
text-align:center;
font-size:9px;
color:#999999;
border-top:1px solid #eeeeee;
margin-top:2px;
padding-top:1px;
line-height:1;
">
This is a system-generated email. Please do not reply.
</div>';
public static function send_email_smtp($params)
{
$myLogger = \Config\Services::mylogger();
$emaill = $params['mail'];
$subject = $params['subject'];
$message = $params['message'];
$message .= self::SYSTEM_EMAIL_FOOTER;
if (isset($params['bcc'])) {
$bcc = $params['bcc'];
} else {
$bcc = '';
}
if(isset($params['cc'])){
$cc = $params['cc'];
}else{
$cc = '';
}
$from_address = getenv('email.fromEmail');
try {
$curl = curl_init();
$postData = [
'from' => [
'address' => $from_address,
'name' => 'Nhance India Insurance'
],
'to' => [
[
'email_address' => [
'address' => $emaill
]
]
],
'subject' => $subject,
'htmlbody' => $message
];
// 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);
}
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",
],
]);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$err = curl_error($curl);
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);
}
$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);
}
} 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 = [])
{
$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);
}
}
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 = $params['subject'];
$message = $params['message'];
$message .= self::SYSTEM_EMAIL_FOOTER;
// echo $message;die;
$attachments = isset($params['attachments']) ? $params['attachments'] : [];
$common = isset($params['common']) ? $params['common'] : '';
$bcc = isset($params['bcc']) ? $params['bcc'] : '';
$cc = (isset($params['cc']) && !empty($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";
if(isset($params['common'])){ $params['common']['from_mail'] = $from_address; }
try {
$curl = curl_init();
$postData = [
'from' => [
'address' => $from_address,
'name' => "Nhance India Insurance"
],
'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",
],
]);
$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'])) {
$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' => 500,
'data' => $msg
], 500);
}
}
public static function bulk_mail(array $mails = [])
{
$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);
}
}
}
?>