<?
// use PHPMailer\PHPMailer\PHPMailer;
// use PHPMailer\PHPMailer\Exception;

// require 'PHPMailer/src/Exception.php';
// require 'PHPMailer/src/PHPMailer.php';
// require 'PHPMailer/src/SMTP.php';
// require 'phpmail_password.php';

// function mailerwithCC($to, $subject, $body) {
//     $mail = new PHPMailer(true);                              
//     try {
//         $mail->SMTPDebug = 0;                            
//         $mail->isSMTP();                               
//         $mail->Host = 'smtp.robi.com.bd';     
        // $mail->SMTPAuth = true;           
        // $mail->Username = 'eSourcing';
        // $mail->Password = 'SmTp@1357';
        // $mail->SMTPSecure = 'STARTLS';
//         $mail->Port = 25;     
//         $mail->setFrom('esourcing@robi.com.bd', 'Robi Group');
//         foreach ($to as $recipient) {
//          if($recipient->action=='Owner'){
//             $mail->addAddress($recipient->email);    
            
//          }else{
//             $mail->addCC($recipient->email,$recipient->fname);
//          }
//         }
//         $mail->isHTML(true);
//         $mail->Subject = $subject;
//         $mail->Body = $body;
 
//         $mail->send();
       
//     } catch (Exception $e) {
//         echo 'Message could not be sent.';
//         echo 'Mailer Error: ' . $mail->ErrorInfo;
//     }
// }


?>
<?php

$config = require_once '../../../controllers/config/db_live_config.php';

$host   = $config['hostname'];
$port   = $config['port'];
$user   = $config['username'];
$pass   = $config['password'];
$db     = $config['database'];


$conn = mysqli_connect($host,$user,$pass,$db,$port);
if(!$conn) {    die("Database connection failed: M" . mysqli_connect_error()); }
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'phpmail_password.php';

function mailerwithCC($to, $subject, $body) {
    global $conn;
    $subject = mysqli_real_escape_string($conn, $subject);
    $mail = new PHPMailer(true);                              
    try {
        $mail->SMTPDebug = 0;                            
        $mail->isSMTP();                               
        $mail->Host = 'smtp.robi.com.bd';     
        $mail->Port = 25;     
        $mail->setFrom('esourcing@robi.com.bd', 'Robi Group');

        $toAddresses = [];
        $ccAddresses = [];

        foreach ($to as $recipient) {
            if ($recipient->action == 'Owner') {
                $mail->addAddress($recipient->email);    
                $toAddresses[] = $recipient->email;
            } else {
                $mail->addCC($recipient->email, $recipient->fname);
                $ccAddresses[] = $recipient->email;
            }
        }

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $body;

        $mail->send();

        // Combine addresses into a single string for logging
        $toList = implode(', ', $toAddresses);
        $ccList = implode(', ', $ccAddresses);
        $ccList=empty($ccList)?'N/A':$ccList;
        $recipientList = $toList . ($ccList ? ", CC: " . $ccList : "");

        $rfq_no = $_SESSION['rfq_no'] ?? 'N/A'; 
        // Default to 'N/A' if rfq_no is not set
        $rfq_no = mysqli_real_escape_string($conn, $rfq_no);
        $reason = 'N/A';

        $sql_mail_history = "INSERT INTO mail_logs (cc,sender, rfq_no, recipients, subject, status, reason, sent_at) 
        VALUES ('$ccList','robi', '$rfq_no', '$toList', '$subject', 'Success', '$reason', NOW())";

        mysqli_query($conn, $sql_mail_history);
    } catch (Exception $e) {
        $rfq_no = $_SESSION['rfq_no'] ?? 'N/A';
        $reason = $mail->ErrorInfo;

        $sql_mail_history = "INSERT INTO mail_logs (sender, rfq_no, recipients, subject, status, reason, sent_at) 
        VALUES ('robi', '$rfq_no', '$recipientList', '$subject', 'Failed', '$reason', NOW())";

        mysqli_query($conn, $sql_mail_history);

        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
}
?>

