156 lines
6.4 KiB
PHP
156 lines
6.4 KiB
PHP
<?php
|
|
|
|
// app/Helpers/NotificationHelper.php
|
|
|
|
namespace App\Helpers;
|
|
|
|
class NotificationHelper
|
|
{
|
|
protected $email;
|
|
protected $logger;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->email = service('email');
|
|
$this->logger = service('logger');
|
|
}
|
|
|
|
public function sendEmail($records)
|
|
{
|
|
$this->logger->info('Helper : Email Request Data type = '.gettype($records));
|
|
|
|
$cc = isset($records['carboncopy']) ? $records['carboncopy'] : '';
|
|
$bcc = isset($records['blindcarboncopy']) ? $records['blindcarboncopy'] : '';
|
|
|
|
try {
|
|
$this->email->clear();
|
|
if($records['recipient_email'] == "" && $records['subject'] == ""){
|
|
throw new \Exception("Email and Subject are Must Please Try again..");
|
|
}
|
|
$this->email->setTo($records['recipient_email']);
|
|
$this->logger->info('Helper : Email recipient = '.$records['recipient_email']);
|
|
$this->email->setFrom('bbone@venbait.in', "DONOR");
|
|
$this->email->setSubject($records['subject']);
|
|
$this->logger->info('Helper : Email subject = '.$records['subject']);
|
|
$this->email->setMessage($records['description']);
|
|
|
|
// if($records['template_name'] != ""){
|
|
// $this->logger->info('Helper : Email Template = '.$records['template_name']);
|
|
// $this->logger->info("Helper : Email Request data = ".json_encode($records));
|
|
// $emailContent = view($records['template_name'],$records);
|
|
// $this->email->setMessage($emailContent);
|
|
// }else{
|
|
// $this->logger->info('Helper : Email Dynamic Template');
|
|
// $this->email->setMessage($records['description']);
|
|
// }
|
|
|
|
|
|
if ($cc !== '') {
|
|
$ccRecipients = explode(',', trim($cc));
|
|
$this->logger->info('Helper : Email carbon copy CC recipient = ' . implode(',', $ccRecipients));
|
|
if (!empty($ccRecipients)) {
|
|
if (getenv('CI_ENVIRONMENT') === "production") {
|
|
$this->email->setCC('contact@vijayabharathambooks.com');
|
|
}
|
|
$this->email->setCC($ccRecipients);
|
|
}
|
|
} else {
|
|
if (getenv('CI_ENVIRONMENT') === "production") {
|
|
$this->email->setCC('contact@vijayabharathambooks.com');
|
|
}
|
|
}
|
|
|
|
if ($bcc !== '') {
|
|
$bccRecipients = explode(',', trim($bcc));
|
|
$this->logger->info('Helper : Email blindcarboncopy BCC recipient = ' . implode(',', $bccRecipients));
|
|
if(!empty($bccRecipients)) {
|
|
$this->email->setBCC($bccRecipients);
|
|
}
|
|
}
|
|
|
|
if ($this->email->send()) {
|
|
$message['success'] = 'Email sent successfully';
|
|
$this->logger->info('Helper : Email Status = sent successfully');
|
|
} else {
|
|
throw new \Exception('Email sending failed.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Helper : Email Error = ' . $e->getMessage());
|
|
$message['error'] = 'Email Exception Occur : ' . $e->getMessage();
|
|
}
|
|
return $message;
|
|
}
|
|
|
|
public function sendWhatsAppMessage($url, $method, $data)
|
|
{
|
|
$final_result = []; // Initialize outside try-catch block
|
|
|
|
try {
|
|
$this->logger->info('Helper : Whatsapp');
|
|
$curl = curl_init();
|
|
$headers = array('Content-Type:application/json');
|
|
$jsonencoded = json_encode($data);
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
$this->logger->info('Helper : Whatsapp url = ' . $url);
|
|
|
|
switch ($method) {
|
|
case "POST":
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
if ($data) {
|
|
$this->logger->info('Helper : Whatsapp jsonencoded = ' . $jsonencoded);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonencoded);
|
|
}
|
|
break;
|
|
case "PUT":
|
|
curl_setopt($curl, CURLOPT_PUT, 1);
|
|
break;
|
|
default:
|
|
if ($data) {
|
|
$url = sprintf("%s?%s", $url, http_build_query((array)$data));
|
|
}
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
$curl_exec = curl_exec($curl);
|
|
$this->logger->info('Helper : Whatsapp Reponse = ' . $curl_exec);
|
|
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
if ($http_status === 200) {
|
|
$result = json_decode($curl_exec);
|
|
|
|
if ($result->status === "error") {
|
|
$final_result['message'] = $result->message;
|
|
$final_result['receiving_status'] = 0 ;
|
|
$this->logger->error('Helper : Whatsapp Err occur = ' . $result->message);
|
|
throw new \Exception($result->message);
|
|
}
|
|
|
|
if ($result->status === "success") {
|
|
$final_result['message'] = isset($result->message) ? (
|
|
(strtolower(gettype($result->message)) === 'string') ?
|
|
$result->message : "Sended Success"
|
|
) : " Message Not Sent. ";
|
|
$final_result['receiving_status'] = isset($result->message) ? ((strtolower(gettype($result->message)) === 'string') ? 0 : 1 ) : 0 ;
|
|
}
|
|
} else {
|
|
$curl_errno = curl_errno($curl);
|
|
$this->logger->error('Helper : Whatsapp curl error occur = ' . $curl_errno);
|
|
throw new \Exception(" Errno returned " . $curl_errno);
|
|
}
|
|
|
|
curl_close($curl);
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Helper : Whatsapp Exception Occur = ' . $e->getMessage());
|
|
$final_result['message'] = "Exception Errno returned " . $e->getMessage() . " <br/>";
|
|
$final_result['receiving_status'] = 0 ;
|
|
}
|
|
$final_result['reponse'] = json_decode($curl_exec);
|
|
return $final_result;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|