<?php
/**
 * class mail
 *
 * Description for class mail
 *
 * @author:
*/
namespace core;

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

class mail {
	public static function static_send($subject, $message, $recipment) {
		global $__config;

		$mail = new PHPMailer(true);

		try {
			$mail->isSMTP();
			$mail->CharSet = "UTF-8";
			$mail->Host = $__config["server_smtp"]["host"];
			$mail->SMTPAuth = (bool)$__config["server_smtp"]["auth"];
			$mail->Username = $__config["server_smtp"]["user"];
			$mail->Password = $__config["server_smtp"]["password"];
			$mail->SMTPSecure = $__config["server_smtp"]["secure"];
			$mail->Port = (int)$__config["server_smtp"]["port"];

			$mail->setFrom($__config["server_smtp"]["email"], $__config["server_smtp"]["email_name"]);
			$mail->addReplyTo($__config["server_smtp"]["email"], $__config["server_smtp"]["email_name"]);
			
			if(!is_array($recipment)) {
				$recipment = explode(";", $recipment);
			}

			$recipment = array_filter($recipment);
			if(count($recipment) > 0) {
				foreach($recipment as $item) {
					$mail->addAddress($item);
				}
			}

			//$mail->addCC("cc@example.com");
			//$mail->addBCC("bcc@example.com");
			//$mail->addAttachment("/tmp/image.jpg", "new.jpg");

			$mail->isHTML(true);
			$mail->Subject = $subject;
			$mail->Body = $message;
			$mail->AltBody = strip_tags($message);

			return $mail->send();
		} catch (Exception $e) {
			
		}

		return false;
	}

	public static function render_message($view, $model, $layout = "layout_mail") {
		global $__template;

		$__template->setVariable("_MODEL", $model);
		return $__template->render("_shared/{$layout}.tpl", "_shared/mail/{$view}.tpl");
	}
}
?>