From e9234d776e21cd510277a841cb527308067b0324 Mon Sep 17 00:00:00 2001 From: Suvo Date: Wed, 24 Dec 2025 10:24:10 +0000 Subject: [PATCH] fix email send issue --- .hta_slug/edit-lead.php | 196 ++++++++++++++++++++-------------------- 1 file changed, 97 insertions(+), 99 deletions(-) diff --git a/.hta_slug/edit-lead.php b/.hta_slug/edit-lead.php index 2458fb5..37d7a13 100644 --- a/.hta_slug/edit-lead.php +++ b/.hta_slug/edit-lead.php @@ -314,133 +314,131 @@ [ [ "email" => $usersEmailForSendMessage, - "name" => $usersNameforSendMessage + "name" => $usersNameforSendMessage ] ], - "templateId" => (int)$_GET['send-email-id'], // Ensure this is an integer + "templateId" => (int) $_GET['send-email-id'], "batchId" => "5c6cfa04-eed9-42c2-8b5c-6d470d978e9d" ]; - $jsonData = json_encode($data); - - $options = [ - 'http' => [ - 'header' => "Content-Type: application/json\r\n" . - "api-key: $apiKey\r\n", - 'method' => 'POST', - 'content' => $jsonData, + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + "Content-Type: application/json", + "api-key: $apiKey" ], - ]; - - $context = stream_context_create($options); - $fp = @fopen($url, 'r', false, $context); // Suppress warnings to handle them manually - - if ($fp === FALSE) { - $error = error_get_last(); - echo 'Error occurred: ' . $error['message']; - } else { - $response = stream_get_contents($fp); - $http_response_header = $http_response_header ?? []; - $response_code = (int)substr($http_response_header[0], 9, 3); - - if ($response === FALSE || $response_code >= 400) { - echo 'Error reading response: ' . $response; - } else { - $templateName = $_GET['send-email-id']; - $messageMethod = 'Email'; - try { - $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); - $stmt->bindParam(':leadid', $_GET['id']); - $stmt->bindParam(':useremail', $_SESSION['email']); - $stmt->bindParam(':username', $_SESSION['name']); - $stmt->bindParam(':templateid', $templateName); - $stmt->bindParam(':method', $messageMethod); - $stmt->execute(); - $sendSuccessMessage = "Email Sent successfully"; - // echo ''; - } catch (PDOException $e) { - echo "Error: " . $e->getMessage(); - } - } - // Close the file pointer - fclose($fp); - echo ''; + CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_TIMEOUT => 30 + ]); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_errno($ch)) { + die("Brevo cURL Error: " . curl_error($ch)); } - } elseif (isset($_GET['send-whatsapp-id']) && $_GET['send-whatsapp-id']) { + + curl_close($ch); + + if ($httpCode >= 400) { + die("Brevo API Error ($httpCode): $response"); + } + + // Log in DB + try { + $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); + $stmt->execute([ + ':leadid' => $_GET['id'], + ':useremail' => $_SESSION['email'], + ':username' => $_SESSION['name'], + ':templateid'=> $_GET['send-email-id'], + ':method' => 'Email' + ]); + } catch (PDOException $e) { + die("DB Error: " . $e->getMessage()); + } + echo ""; + } + + + /* =============================== + SEND WHATSAPP (INTERAKT) + ================================= */ + elseif (!empty($_GET['send-whatsapp-id'])) { + $url = "https://api.interakt.ai/v1/public/message/"; - $apiKey = 'Basic {{OTgyajd4bHFUSXItSW9PN1BTdzhOenNDaS0ya0NQeXByRE0tMnRyQ3FrUTo=}}'; + $apiKey = 'Basic {{OTgyajd4bHFUSXItSW9PN1BTdzhOenNDaS0ya0NQeXByRE0tMnRyQ3FrUTo=}}'; // move to env in production + $data = [ "fullPhoneNumber" => $usersNumberforSendMessage, - "callbackData" => "some text here", + "callbackData" => "lead-message", "type" => "Template", "template" => [ "name" => $_GET['send-whatsapp-id'], "languageCode" => "en", - "headerValues" => [ - "header_variable_value" - ], "bodyValues" => [ $usersNameforSendMessage ] ] ]; - $jsonData = json_encode($data); - $options = [ - 'http' => [ - 'header' => "Content-Type: application/json\r\n" . - "Authorization: $apiKey\r\n" . - "Content-Length: " . strlen($jsonData) . "\r\n", - 'method' => 'POST', - 'content' => $jsonData, + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + "Content-Type: application/json", + "Authorization: $apiKey" ], - ]; + CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_TIMEOUT => 30 + ]); - $context = stream_context_create($options); - $fp = fopen($url, 'r', false, $context); - if ($fp === FALSE) { - // echo 'Error occurred'; - echo $fp; - } else { - $response = stream_get_contents($fp); - - if ($response === FALSE) { - // echo 'Error reading response'; - // echo $response; - } else { - if(isset($_GET['send-whatsapp-id'])){ - $templateName = $_GET['send-whatsapp-id']; - } elseif(isset($_GET['send-email-id'])){ - $templateName = $_GET['send-email-id']; - } - $messageMethod = 'WhatsApp'; - try { - $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); - $stmt->bindParam(':leadid', $_GET['id']); - $stmt->bindParam(':useremail', $_SESSION['email']); - $stmt->bindParam(':username', $_SESSION['name']); - $stmt->bindParam(':templateid', $templateName); - $stmt->bindParam(':method', $messageMethod); - $stmt->execute(); - $sendSuccessMessage = "WhatsApp Sent successfully"; - - } catch(PDOException $e) { - echo "Error: " . $e->getMessage(); - } - // echo ''; - // echo $response; - } - fclose($fp); - echo ''; + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_errno($ch)) { + die("WhatsApp cURL Error: " . curl_error($ch)); } + + curl_close($ch); + + if ($httpCode >= 400) { + die("WhatsApp API Error ($httpCode): $response"); + } + + // Log in DB + try { + $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); + $stmt->execute([ + ':leadid' => $_GET['id'], + ':useremail' => $_SESSION['email'], + ':username' => $_SESSION['name'], + ':templateid'=> $_GET['send-whatsapp-id'], + ':method' => 'WhatsApp' + ]); + } catch (PDOException $e) { + die("DB Error: " . $e->getMessage()); + } + + echo ""; } ?> +
@@ -710,7 +708,7 @@
- +