<?php
require_once '../config.php';
require_once '../../../vendor/autoload.php';

date_default_timezone_set('Asia/Kolkata');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

function loi_mail($data)
{
  list($to, $ccMails, $subject, $mail_body) = $data;

  $mail_body = str_replace('₹', '&#8377;', $mail_body);

  // $in_hr = mysqli_fetch_assoc(mysqli_query($GLOBALS['db'],"SELECT name,email from users where id='$_SESSION[uid]' "));
  // $in_hr_name = $in_hr['name'];
  // $in_hr_email = $in_hr['email'];

  $mail = new PHPMailer(true);
  try {
    $mail->SMTPDebug = 0;
    $mail->isSMTP();

    include __DIR__."/../../mail_send_credential.php";

    $mail->addAddress(trim($to));
    $mail->addReplyTo('recruitment@jobaaj.com', 'Jobaaj Recruitment');
    $mail->addCC('recruitment@jobaaj.com', 'Jobaaj Recruitment');

    if (!empty($ccMails)) {
      $ccMails = explode(',', $ccMails);
      foreach ($ccMails as $ccEmail) {
        $mail->addCC(trim($ccEmail));
      }
    }

    // $mail->addCC('finance@nishtyainfotech.com', 'Finance Department');
    // $mail->addCC('gaurav.garg@jobaaj.com', 'Gaurav Garg');
    // $mail->addCC('hr@jobaaj.com', 'HR Department');


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


    $mail->Body    = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
          <html xmlns='http://www.w3.org/1999/xhtml' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office'>
          <head>
            <!--[if gte mso 9]>
            <xml>
              <o:OfficeDocumentSettings>
              <o:AllowPNG/>
              <o:PixelsPerInch>96</o:PixelsPerInch>
              </o:OfficeDocumentSettings>
            </xml>
            <![endif]-->
            <meta http-equiv='Content-type' content='text/html; charset=utf8' />
            <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1' />
            <meta http-equiv='X-UA-Compatible' content='IE=edge' />
            <meta name='format-detection' content='date=no' />
            <meta name='format-detection' content='address=no' />
            <meta name='format-detection' content='telephone=no' />
            <meta name='x-apple-disable-message-reformatting' />
            <!--[if !mso]><!-->
            <link href='https://fonts.googleapis.com/css?family=PT+Sans:400,400i,700,700i&display=swap' rel='stylesheet' />
            <!--<![endif]-->
            <title>Email Template</title>
            <!--[if gte mso 9]>
            <style type='text/css' media='all'>
              sup { font-size: 100% !important; }
            </style>
            <![endif]-->
          </head>
          <body class='body'>
            <center>
             $mail_body
            </center>
          </body>
          </html>  
        ";
    $mail->AltBody = 'Body in plain text for non-HTML mail clients';
    // $mail->send();
    if ($mail->send()) {
      return 1;
    } else {
      return 2;
    }
    //echo "Mail has been sent successfully!";
  } catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
}
