<?php

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

require_once 'phpmailer/Exception.php';
require_once 'phpmailer/PHPMailer.php';
require_once 'phpmailer/SMTP.php';

class mail_tools {

	function __construct($base_object)
	{
		$this->base_object = $base_object;
	}

	function send_mail($subject, $message, $recipient = false, $logo = false, $recipients_from_db = false, $reply_to = false, $from_mail = false, $from_name = false, $attachments = false)
	{
		$mail = new PHPMailer(true);

		date_default_timezone_set("Europe/Berlin");
		$subject = utf8_decode($subject);
		$from_name = ($from_name) ?  mb_encode_mimeheader(utf8_decode($from_name), "UTF-8", "Q") : false;

		$recipients['to'] = ($recipient) ? $recipient : $global_mail_settings['mail_to']['global'];

		$email = $this->base_object->config->shopConfiguration['system_email'];
		$from_mail = $this->base_object->config->shopConfiguration['system_email'];
		$host = $this->base_object->config->shopConfiguration['system_email_host'];
		$user = $this->base_object->config->shopConfiguration['system_email_user'];
		$pass = $this->base_object->config->shopConfiguration['system_email_password'];
		$from_name = $this->base_object->config->shopConfiguration['system_email_name'];
		$reply_to = $this->base_object->config->shopConfiguration['system_email_reply'];

		// read css file
		$file_name = (isset($this->base_object->layout) && $this->base_object->layout) ? $this->base_object->layout->template_dir.'/../media/css/mail.css' : '';
		$css = '';
		if (file_exists($file_name)) {
			$handle = fopen($file_name, "r");
			while (!feof($handle)) {
				$css .= fgets($handle,2);
			};
			fclose($handle);
		}

		try {
			//Server settings
			/*$mail->SMTPDebug = SMTP::DEBUG_SERVER; // OUTPUT DEBUG
			$mail->SMTPDebug = 3;*/
			$mail->isSMTP();
			$mail->Host = $host;
			$mail->SMTPAuth = true;
			$mail->Username = $user;
			$mail->Password = $pass;
			$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
			$mail->SMTPOptions = array('ssl' => array(
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => false
				));
			$mail->Port = 587;
			//Recipients
			$mail->setFrom($from_mail, $from_name);
			$mail->addAddress($recipients['to']);
			if ($reply_to)
				$mail->addReplyTo($reply_to, $from_name);
			if (isset($recipients['cc']) && $recipients['cc'])
				$mail->addCC($recipients['cc']);
			if (isset($recipients['bcc']) && $recipients['bcc'])
				$mail->addBCC($recipients['bcc']);
			//Attachments
			if ($attachments != false)
				foreach ($attachments as $att)
					$mail->addStringAttachment($att->content, $att->file_name);
			//Content
			$mail->isHTML(true);
			$mail->Subject = $subject;
			$inline_message = $inline_message ?? '';
		$inline_logo = $inline_logo ?? '';
		$body = '<html><head><style type="text/css">'.$css.'</style></head><body>'.utf8_decode($message).'<br /><br />'.utf8_decode($inline_message).'<br />'.$inline_logo.'</body></html>';
			$mail->Body = $body;
			//$mail->AltBody = ;

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

	} // end send_mail

	public function checkmail($email)
	{
		$mail_addr = explode(';', $email);
		$ready_mail = $mail_addr[0];

		$mail_parts = explode('@', $ready_mail);
		$domain = $mail_parts[1];

		if (!$domain) {
			return false;
		}

		if (function_exists('checkdnsrr')) {
			if (checkdnsrr($domain, "MX")) {
				if (function_exists('getmxrr')) {
					if (!getmxrr($domain, $mxhosts)) {
						return false;
					}
				}
			} else {
				return false;
			}
		}
		return $ready_mail;
	} // end checkmail
}
?>