<?php

include_once "../config.php";
//require_once 'PHPMailer/PHPMailerAutoload.php';



//require_once "PHPMailer/src/PHPMailer.php";
//require_once "PHPMailer/src/SMTP.php";
//require_once "PHPMailer/src/Exception.php";

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

// Load Composer's autoloader
//require_once 'PHPMailer/vendor/autoload.php';
function send_email($email,$name, $subject, $body, $attachments = null, $noLogo = null, $noFrame = 0)
{

    /* SMTP */

    $mail = new PHPMailer(true);

    try {
        //Server settings
      // $mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
        $mail->isSMTP();
        $mail->Host = SMTP_HOST;  // Specify main and backup SMTP servers
       // $mail->Host = 'localhost';
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
      //  $mail->SMTPAuth = false; 
        $mail->Username = SMTP_EMAIL;                 // SMTP username
        $mail->Password = SMTP_PASSWORD;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;   // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
        $mail->Port = SMTP_PORT;
       // $mail->Port = 25;
        $mail->CharSet  = 'UTF-8';

        /*$mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );*/

        $mail->setFrom(SMTP_SET_FROM_EMAIL, SMTP_SET_FROM_NAME);


        if(is_array($email))
        {
            $mail->addAddress(SMTP_SET_FROM_EMAIL, SMTP_SET_FROM_NAME);
            foreach($email AS $each)
            {
                $mail->addBcc($each['email']);
            }
        }
        else
        {
            $mail->addAddress($email, $name);
        }

      
	
        $mail->Subject = $subject;


        $logo = '';
        if($noLogo == null || $noLogo == 0)
        {
            $logo = '<img src="https://zillo.lv/uploads/logo/logo.png?version=1019" style="max-width:150px" />';
        }

        //$bodyEmail = '<!DOCTYPE html><head></head><title></title><body>';
        $bodyEmail = '';
        if($noFrame == 0)
        {
            $bodyEmail.= '<table style="width:800px;max-width:100%;border:1px solid lightgray;text-align:center;margin : 0 auto"><tr><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $logo . '</td></tr><tr><td><b style="font-size:17px;;padding:15px">';
        }

        $bodyEmail.= $body;

        if($noFrame == 0)
        {
            $bodyEmail.= '</b></td></tr><tr><td style="border-top:1px solid lightgray;padding:15px">Paldies, ka izvēlējāties<br/>www.zillo.lv</td></tr></table>';
        }

        $mail->Body    = $bodyEmail;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        
        	$mail->isHTML(true);

        if($attachments != null)
        {
            foreach($attachments AS $attachment)
            {
                $mail->addAttachment($attachment); 
            }
            
        }
        
      

        if(!$mail->send())
        {
            return 0;
        }
        else
        {
            return 1;
        }

    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: " . $mail->ErrorInfo;
}

/*ini_set('sendmail_from', 'noreplay@clusterinfos.com');
ini_set('sendmail_path', '/usr/sbin/sendmail -t -i -f noreplay@clusterinfos.com');
$headers = "From: noreplay@clusterinfos.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
                        //$headers .= "Reply-To: shayasmk@gmail.com\r\n";
						$headers .= "MIME-Version: 1.0\r\n";
						$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
						$headers .= "X-Mailer: PHP/".phpversion();

						return mail('shayasmk@gmail.com',$subject, $body, $headers); */

}

