fix email send issue

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

View File

@@ -314,133 +314,131 @@
</div>
</form>
<?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";
$apiKey = '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';
$apiKey = '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'; // better use env
$data = [
"to" => [
[
"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 '<script>window.location.href = "/edit-lead/?id='.$_GET['id'].'"</script>';
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
// Close the file pointer
fclose($fp);
echo '<script>window.location.href = "/edit-lead/?id='.$_GET['id'].'"</script>';
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 "<script>window.location.href = '/edit-lead/?id=" . $_GET['id'] . "';</script>";
}
/* ===============================
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 '<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>';
$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 "<script>window.location.href = '/edit-lead/?id=" . $_GET['id'] . "';</script>";
}
?>
<div class="flex flex-col div-custom-margin" style="width: 100%;" >
<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;">
@@ -710,7 +708,7 @@
<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;" />
</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">
<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;" />