fix email send issue

This commit is contained in:
2025-12-24 10:24:10 +00:00
parent 0c6e79426e
commit e9234d776e

View File

@@ -314,9 +314,14 @@
</div> </div>
</form> </form>
<?php <?php
if (isset($_GET['send-email-id']) && $_GET['send-email-id']) { /* ===============================
SEND EMAIL (BREVO)
================================= */
if (!empty($_GET['send-email-id']) && is_numeric($_GET['send-email-id'])) {
$url = "https://api.brevo.com/v3/smtp/email"; $url = "https://api.brevo.com/v3/smtp/email";
$apiKey = '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'; $apiKey = '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'; // better use env
$data = [ $data = [
"to" => [ "to" => [
[ [
@@ -324,123 +329,116 @@
"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" "batchId" => "5c6cfa04-eed9-42c2-8b5c-6d470d978e9d"
]; ];
$jsonData = json_encode($data);
$options = [ $ch = curl_init($url);
'http' => [ curl_setopt_array($ch, [
'header' => "Content-Type: application/json\r\n" . CURLOPT_POST => true,
"api-key: $apiKey\r\n", CURLOPT_RETURNTRANSFER => true,
'method' => 'POST', CURLOPT_HTTPHEADER => [
'content' => $jsonData, "Content-Type: application/json",
"api-key: $apiKey"
], ],
]; CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_TIMEOUT => 30
]);
$context = stream_context_create($options); $response = curl_exec($ch);
$fp = @fopen($url, 'r', false, $context); // Suppress warnings to handle them manually $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($fp === FALSE) { if (curl_errno($ch)) {
$error = error_get_last(); die("Brevo cURL Error: " . curl_error($ch));
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) { curl_close($ch);
echo 'Error reading response: ' . $response;
} else { if ($httpCode >= 400) {
$templateName = $_GET['send-email-id']; die("Brevo API Error ($httpCode): $response");
$messageMethod = 'Email'; }
// Log in DB
try { try {
$stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)");
$stmt->bindParam(':leadid', $_GET['id']); $stmt->execute([
$stmt->bindParam(':useremail', $_SESSION['email']); ':leadid' => $_GET['id'],
$stmt->bindParam(':username', $_SESSION['name']); ':useremail' => $_SESSION['email'],
$stmt->bindParam(':templateid', $templateName); ':username' => $_SESSION['name'],
$stmt->bindParam(':method', $messageMethod); ':templateid'=> $_GET['send-email-id'],
$stmt->execute(); ':method' => 'Email'
$sendSuccessMessage = "Email Sent successfully"; ]);
// echo '<script>window.location.href = "/edit-lead/?id='.$_GET['id'].'"</script>';
} catch (PDOException $e) { } catch (PDOException $e) {
echo "Error: " . $e->getMessage(); die("DB Error: " . $e->getMessage());
} }
echo "<script>window.location.href = '/edit-lead/?id=" . $_GET['id'] . "';</script>";
} }
// Close the file pointer
fclose($fp);
echo '<script>window.location.href = "/edit-lead/?id='.$_GET['id'].'"</script>'; /* ===============================
} SEND WHATSAPP (INTERAKT)
} elseif (isset($_GET['send-whatsapp-id']) && $_GET['send-whatsapp-id']) { ================================= */
elseif (!empty($_GET['send-whatsapp-id'])) {
$url = "https://api.interakt.ai/v1/public/message/"; $url = "https://api.interakt.ai/v1/public/message/";
$apiKey = 'Basic {{OTgyajd4bHFUSXItSW9PN1BTdzhOenNDaS0ya0NQeXByRE0tMnRyQ3FrUTo=}}'; $apiKey = 'Basic {{OTgyajd4bHFUSXItSW9PN1BTdzhOenNDaS0ya0NQeXByRE0tMnRyQ3FrUTo=}}'; // move to env in production
$data = [ $data = [
"fullPhoneNumber" => $usersNumberforSendMessage, "fullPhoneNumber" => $usersNumberforSendMessage,
"callbackData" => "some text here", "callbackData" => "lead-message",
"type" => "Template", "type" => "Template",
"template" => [ "template" => [
"name" => $_GET['send-whatsapp-id'], "name" => $_GET['send-whatsapp-id'],
"languageCode" => "en", "languageCode" => "en",
"headerValues" => [
"header_variable_value"
],
"bodyValues" => [ "bodyValues" => [
$usersNameforSendMessage $usersNameforSendMessage
] ]
] ]
]; ];
$jsonData = json_encode($data);
$options = [ $ch = curl_init($url);
'http' => [ curl_setopt_array($ch, [
'header' => "Content-Type: application/json\r\n" . CURLOPT_POST => true,
"Authorization: $apiKey\r\n" . CURLOPT_RETURNTRANSFER => true,
"Content-Length: " . strlen($jsonData) . "\r\n", CURLOPT_HTTPHEADER => [
'method' => 'POST', "Content-Type: application/json",
'content' => $jsonData, "Authorization: $apiKey"
], ],
]; CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_TIMEOUT => 30
]);
$context = stream_context_create($options); $response = curl_exec($ch);
$fp = fopen($url, 'r', false, $context); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($fp === FALSE) {
// echo 'Error occurred';
echo $fp;
} else {
$response = stream_get_contents($fp);
if ($response === FALSE) { if (curl_errno($ch)) {
// echo 'Error reading response'; die("WhatsApp cURL Error: " . curl_error($ch));
// 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';
curl_close($ch);
if ($httpCode >= 400) {
die("WhatsApp API Error ($httpCode): $response");
}
// Log in DB
try { try {
$stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)"); $stmt = $conn->prepare("INSERT INTO communication (leadid, useremail, username, templateid, method) VALUES (:leadid, :useremail, :username, :templateid, :method)");
$stmt->bindParam(':leadid', $_GET['id']); $stmt->execute([
$stmt->bindParam(':useremail', $_SESSION['email']); ':leadid' => $_GET['id'],
$stmt->bindParam(':username', $_SESSION['name']); ':useremail' => $_SESSION['email'],
$stmt->bindParam(':templateid', $templateName); ':username' => $_SESSION['name'],
$stmt->bindParam(':method', $messageMethod); ':templateid'=> $_GET['send-whatsapp-id'],
$stmt->execute(); ':method' => 'WhatsApp'
$sendSuccessMessage = "WhatsApp Sent successfully"; ]);
} catch (PDOException $e) { } catch (PDOException $e) {
echo "Error: " . $e->getMessage(); die("DB Error: " . $e->getMessage());
}
// echo '<script>window.location.href="/edit-lead/?id='.$_GET['id'].'"</script>';
// echo $response;
}
fclose($fp);
echo '<script>window.location.href = "/edit-lead/?id='.$_GET['id'].'"</script>';
} }
echo "<script>window.location.href = '/edit-lead/?id=" . $_GET['id'] . "';</script>";
} }
?> ?>
<div class="flex flex-col div-custom-margin" style="width: 100%;" > <div class="flex flex-col div-custom-margin" style="width: 100%;" >
<div class="bg-[#F6F6F6] rounded-xl p-3"> <div class="bg-[#F6F6F6] rounded-xl p-3">
<div class="flex-container-x2y place-content-between" style="border-bottom: 2px solid #7E8299; border-style: dashed; padding-bottom: 3px;"> <div class="flex-container-x2y place-content-between" style="border-bottom: 2px solid #7E8299; border-style: dashed; padding-bottom: 3px;">
@@ -710,7 +708,7 @@
<label for="appt_date">Appointment Date</label> <label for="appt_date">Appointment Date</label>
<input type="datetime-local" name="appt_date" id="appt_date" style="border: 1px solid #D9D9D9; border-radius: 5px; padding: 6px;" /> <input type="datetime-local" name="appt_date" id="appt_date" style="border: 1px solid #D9D9D9; border-radius: 5px; padding: 6px;" />
</div> </div>
<input type="text" name="lead_name" id="lead_name" value="<?php echo $usersNameforSendMessage; ?>"> <input type="hidden" name="lead_name" id="lead_name" value="<?php echo $usersNameforSendMessage; ?>">
<div class="flex flex-col"> <div class="flex flex-col">
<label for="appt-comment">Description (if needed)</label> <label for="appt-comment">Description (if needed)</label>
<input type="text" name="comment" id="appt-comment" style="border: 1px solid #D9D9D9; border-radius: 5px; padding: 6px;" /> <input type="text" name="comment" id="appt-comment" style="border: 1px solid #D9D9D9; border-radius: 5px; padding: 6px;" />