File "resend_message.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/email/resend_message.php
File size: 2.26 KiB (2318 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
header('Content-Type: application/json');
require_once '../../../config/config.php';

try {
    // Check if it's a POST request
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
        throw new Exception('Invalid request method');
    }
    
    $log_id = $_POST['log_id'] ?? '';
    if (empty($log_id)) {
        throw new Exception('Log ID is required');
    }
    
    // Get the original WhatsApp message log
    $stmt = $pdo->prepare("SELECT * FROM whatsapp_message_logs WHERE id = ?");
    $stmt->execute([$log_id]);
    $log = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$log) {
        throw new Exception('Message log not found');
    }
    
    // Get booking details to find email address
    $booking_id = $log['booking_id'];
    if (!$booking_id) {
        throw new Exception('No booking ID associated with this message');
    }
    
    $stmt = $pdo->prepare("SELECT consignee_email, consignee_name FROM booking WHERE id = ?");
    $stmt->execute([$booking_id]);
    $booking = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$booking) {
        throw new Exception('Booking not found');
    }
    
    if (empty($booking['consignee_email'])) {
        throw new Exception('No email address found for this booking');
    }
    
    // Include the email sender class
    require_once 'send_message.php';
    $email = new EmailSender($pdo);
    
    // Create email subject and message from WhatsApp content
    $subject = "Booking Information - CN: " . ($log['cn_number'] ?? 'N/A');
    $message = $log['message_content'];
    
    // Try to send email
    $result = $email->sendMessage($booking['consignee_email'], $subject, $message, null, $booking_id);
    
    if ($result['status'] === 'success') {
        echo json_encode([
            'status' => 'success',
            'message' => 'Email sent successfully',
            'email' => $booking['consignee_email']
        ]);
    } else {
        echo json_encode([
            'status' => 'error',
            'message' => $result['message'] ?? 'Failed to send email',
            'debug' => $result['debug'] ?? null
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
}
?> 

PHP File Manager