From 5b71c709b0c4a3962b63488091b03c7145c06b0b Mon Sep 17 00:00:00 2001 From: Suvodip Date: Fri, 24 May 2024 20:00:24 +0530 Subject: [PATCH] implement email send --- .hta_slug/edit-lead.php | 214 +++++++++++++++++++++++++--------------- 1 file changed, 135 insertions(+), 79 deletions(-) diff --git a/.hta_slug/edit-lead.php b/.hta_slug/edit-lead.php index acc6f9e..f9ea7bd 100644 --- a/.hta_slug/edit-lead.php +++ b/.hta_slug/edit-lead.php @@ -51,6 +51,7 @@ $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // var_dump($rows); foreach($rows as $row){ + $usersEmailForSendMessage = $row['email']; $usersNameforSendMessage = $row['name']; $usersNumberforSendMessage = $row['phone']; $nameParts = explode(" ", $row['name']); @@ -272,6 +273,130 @@ + [ + [ + "email" => $usersEmailForSendMessage, + "name" => $usersNameforSendMessage + ] + ], + "templateId" => (int)$_GET['send-email-id'], // Ensure this is an integer + "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, + ], + ]; + + $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); + } + } elseif (isset($_GET['send-whatsapp-id']) && $_GET['send-whatsapp-id']) { + $url = "https://api.interakt.ai/v1/public/message/"; + $apiKey = 'Basic {{OTgyajd4bHFUSXItSW9PN1BTdzhOenNDaS0ya0NQeXByRE0tMnRyQ3FrUTo=}}'; + $data = [ + "fullPhoneNumber" => $usersNumberforSendMessage, + "callbackData" => "some text here", + "type" => "Template", + "template" => [ + "name" => "boost_conversion", + "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, + ], + ]; + + $context = stream_context_create($options); + $fp = fopen($url, 'r', false, $context); + if ($fp === FALSE) { + echo 'Error occurred'; + } else { + $response = stream_get_contents($fp); + + if ($response === FALSE) { + echo 'Error reading 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"; + echo ''; + } catch(PDOException $e) { + echo "Error: " . $e->getMessage(); + } + // echo ''; + // echo $response; + } + fclose($fp); + } + } + ?>
@@ -495,13 +620,18 @@ foreach ($appt_data as $appt) { // Assuming 'comment' and 'date' fields are in the appointments table $apptDate = explode(' ', $appt['time']); // Split date and time + if (is_numeric($appt['templateid'])) { + $templateNameForPrint = 'Email Template ID ' . $appt['templateid']; + } else { + $templateNameForPrint = $appt['templateid']; + } ?>
-

+

-

Mail Send by

+

Send by

@@ -543,81 +673,7 @@
- $usersNumberforSendMessagex, - "callbackData" => "some text here", - "type" => "Template", - "template" => [ - "name" => "boost_conversion", - "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, - ], - ]; - - $context = stream_context_create($options); - $fp = fopen($url, 'r', false, $context); - - // Check for errors - if ($fp === FALSE) { - echo 'Error occurred'; - } else { - // Read the response - $response = stream_get_contents($fp); - - if ($response === FALSE) { - echo 'Error reading response'; - } else { - // Print the response - if(isset($_GET['send-whatsapp-id'])){ - $templateName = $_GET['send-whatsapp-id']; - } elseif(isset($_GET['send-email-id'])){ - $templateName = $_GET['send-email-id']; - } - try { - $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid) VALUES (:leadid, :useremail, :username, :templateid)"); - $stmt->bindParam(':leadid', $_GET['id']); - $stmt->bindParam(':useremail', $_SESSION['email']); - $stmt->bindParam(':username', $_SESSION['name']); - $stmt->bindParam(':templateid', $templateName); - $stmt->execute(); - echo "Message Sent successfully"; - } catch(PDOException $e) { - echo "Error: " . $e->getMessage(); - } - // echo ''; - // echo $response; - } - // Close the file pointer - fclose($fp); - } - } -?> - +