<?php


@session_start();
date_default_timezone_set('Asia/Dhaka');


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

$host   = $config2['hostname'];
$port   = $config2['port'];
$user   = $config2['username'];
$pass   = $config2['password'];
$db     = $config2['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';
// require_once "../../../controllers/routing/default_values.php";
// require_once SERVER_AUTH."db_con_live_static.php";

function mailer_before_login($to, $subject, $body) {

    global $conn;
    $to = mysqli_real_escape_string($conn, $to);
    $subject = mysqli_real_escape_string($conn, $subject);
    // $body = mysqli_real_escape_string($conn, $body);
    $reason = 'N/A';
    $mail = new PHPMailer(true);                              
    try {
        $mail->SMTPDebug = 0;                            
        $mail->isSMTP();                               
        // $mail->Host = '10.101.254.146'; 
		$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');
        $mail->addAddress($to);    
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $body;

        $mail->send();
        $rfq_no=$_SESSION['rfq_no'];
        
        $rfq_no = mysqli_real_escape_string($conn, $_SESSION['rfq_no']);
        $sql_mail_history = "INSERT INTO mail_logs (sender,rfq_no, recipients, subject, status, reason, sent_at) 
        VALUES ('robi','$rfq_no', '$to', '$subject', 'Success', '$reason', NOW())";
        $insert= mysqli_query($conn,$sql_mail_history);
    } catch (Exception $e) {
        $reason=$mail->ErrorInfo;

        $sql_mail_history = "INSERT INTO mail_logs (sender,rfq_no, recipients, subject, status, reason, sent_at) 
        VALUES ('robi','$rfq_no', '$to', '$subject', 'Failed', '$reason', NOW())";
        $insert= mysqli_query($conn,$sql_mail_history);
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
}

// $subject = 'Test Email from Axiata Group of Companies';
// $body = '
//     <html>
//     <body>
//         <p>Dear Fahim,</p>
//         <p>I hope this email finds you well.</p>
//         <p>We are writing to inform you that we are currently conducting a test of our email server to ensure its efficiency and reliability. As part of this process, you have received this email.</p>
    
  
//         <p>Thank you for your attention, and we apologize for any inconvenience this test may have caused.</p>
//         <p>Best regards,</p>
//         <p>Axiata Group of Companies</p>
//     </body>
//     </html>
// ';

// mailer('fahimfoysal177@gmail.com', $subject, $body);
?>
