<?php

class mailer {
	
	private $PHPMailer;
	
	public function __construct() {
		global $settings;
		$this->PHPMailer = new PHPMailer\PHPMailer\PHPMailer();
		$this->PHPMailer->isHTML(true);
		$this->PHPMailer->setFrom($settings->get("email"), $settings->get("bname"));
	}
	
	public function __set($key, $value) {
		$this->PHPMailer->{$key} = $value;
	}
	
	public function __get($key) {
		return ( $this->PHPMailer->{$key} ?? null );
	}
	
	private function create() {
		global $settings;
		require EMAIL_DIR . '/header.php';
		require EMAIL_DIR . '/body.php';
		require EMAIL_DIR . '/footer.php';
	}
	
	public function __call($method, $args) {
		if( $method != 'send' ) {
			call_user_func_array(array($this->PHPMailer, $method), $args);
		} else return $this->send();
	}
	
	public function table(array $data) {
		$style = 'border-collapse: collapse; border-spacing: 0; margin: 0; padding: 8px; width: 50%; font-size: 16px; font-weight: 400; line-height: 160%; font-family: sans-serif; border: 1px solid lightgray; ';
		$table = "<table>
			<tbody>";
		$i = 1;
		foreach( $data as $array ) {
			$restyle = ($i % 2) ? ($style . "background-color: #f7f7f7; ") : $style;
			$table .= "<tr>
				<td style='{$restyle}'>{$array[0]}</td>
				<td style='{$restyle}'>{$array[1]}</td>
			</tr>";
			$i++;
		};
		$table .= "</tbody>
			</table>";
		return $table;
	}
	
	private function send() {
		ob_start();
		$this->create();
		$this->PHPMailer->Body = ob_get_clean();
		$sent = $this->PHPMailer->send();
		return $sent;
	}

}


/*

MAIL USAGE SAMPLE;

$mailer = new mailer();

$mailer->Subject = "Account Details";

$mailer->Headline = "Your email is arrived";
$mailer->Footline = "This mail was sent automatically. Do not reply";

$TABLE = $mailer->table(array(
	["Wow", "Yea"],
	["No", "Wo"],
	["No", "Wo"]
));

$mailer->Body = "
	<section>
		More than 50%&nbsp;of&nbsp;total email opens occurred on&nbsp;a&nbsp;mobile device&nbsp;— a&nbsp;mobile-friendly design is&nbsp;a&nbsp;must for&nbsp;email campaigns.
	</section>
	<hr>
	<section>
		{$TABLE}
	</section>
";

$mailer->addAddress("user@email.com", "Client Name");
$mailer->send();
			
*/