447 lines
15 KiB
PHP
Executable File
447 lines
15 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
|
|
{
|
|
public static function send_email_smtp($params)
|
|
{
|
|
$myLogger = \Config\Services::mylogger();
|
|
$emaill = $params['mail'];
|
|
$subject = $params['subject'];
|
|
$message = $params['message'];
|
|
if (isset($params['bcc'])) {
|
|
$bcc = $params['bcc'];
|
|
} else {
|
|
$bcc = '';
|
|
}
|
|
$from_address = getenv('email.fromEmail');
|
|
|
|
|
|
try {
|
|
$curl = curl_init();
|
|
$postData = [
|
|
'from' => [
|
|
'address' => $from_address
|
|
],
|
|
'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();
|
|
|
|
$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 = getenv('email.fromEmail');
|
|
try {
|
|
$curl = curl_init();
|
|
|
|
$postData = [
|
|
'from' => [
|
|
'address' => $from_address
|
|
],
|
|
'to' => [
|
|
[
|
|
'email_address' => [
|
|
'address' => $emaill
|
|
]
|
|
]
|
|
],
|
|
'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);
|
|
}
|
|
//
|
|
|
|
// 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...' . $emaill,
|
|
'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...' . $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();
|
|
$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);
|
|
}
|
|
}
|
|
}
|
|
?>
|