<?php
// for use PHP Mailer without composer : 

// ex. Create a folder in root/PHPMAILER
// Put on this folder this 3 files find in "src" 
// folder of the distribution : 
// PHPMailer.php , SMTP.php , Exception.php

// include PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include dirname(__DIR__) . '/PHPMailer/src/PHPMailer.php';
include dirname(__DIR__) . '/PHPMailer/src/SMTP.php';
include dirname(__DIR__) . '/PHPMailer/src/Exception.php';
 


// i made a function 

/*
    *
    * Function send_mail_by_PHPMailer($to, $from, $subject, $message);
    * send a mail by PHPMailer method
    * @Param $to -> mail to send
    * @Param $from -> sender of mail
    * @Param $subject -> suject of mail
    * @Param $message -> html content with datas
    * @Return true if success / Json encoded error message if error 
    * !! need -> classes/Exception.php - classes/PHPMailer.php - classes/SMTP.php
    *
    */
function send_mail_by_PHPMailer() 
{
    $db = getConnection();
    $to = mysqli_real_escape_string($db, $_POST['email']);
    $subject = mysqli_real_escape_string($db, $_POST['subject']);
    $message = mysqli_real_escape_string($db, $_POST['emailMessage']);
    $path=null;

    // SEND MAIL by PHP MAILER
    $from = 'bensnjo@gmail.com'; 
    $mail = new PHPMailer(true);
    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true; 
    $mail->Username = 'bensnjo@gmail.';
    $mail->Password = "gtcltgocdbxollxl"; 
    $mail->SMTPSecure = 'ssl'; 
    $mail->Port = '465'; 
    $mail->setFrom($from,"Dejavu Technologies"); 
    $mail->addAddress($to);  
    $mail->isHTML(true); 
    $mail->Subject = $subject;
    $mail->Body = $message;
    if ($path!=null){
    $mail->addAttachment($path); 
    }
    
    // SEND
    if (!$mail->send()) {

        // render error if it is
        $tab = array('error' => 'Mailer Error: ' . $mail->ErrorInfo);
        echo json_encode($tab);
        exit;
    } else {
        // return true if message is sent
        $mail->smtpClose();
        return true;
    }
}
    /*
    *
    * END send_mail_by_PHPMailer($to, $from, $subject, $message)
    * send a mail by PHPMailer method
    *
    */
  
    // use function :
     //$to = "wycliffmuriithi@gmail.com";
     //$from ="test@wintec.co.ke";
    //$subject = "Mail test";
     //$message = "test 1";
    //send_mail_by_PHPMailer($to, $subject, $message);
