<?PHP
/**
* sends email
*/
class Email extends engine{

	/**
	* send stored email what is saved into "templates" table - uses phpmailer
	* @param String $name - name column value
	* @param Array $data - array('key' => 'value')
	* @param String $language - $_SESSION['language']
	* @param String $to_name
	* @param String $to_email
	* @param String $attachment = '' - file_name with full_path
	*/
	function sendStoredEmail($name, $data, $language, $to_name, $to_email, $attachment = ''){
		$rows = $this->Sql->Query("SELECT * FROM templates WHERE name = '" . $name . "' and type = 'EMAIL' and language = '" .
            $language . "' and is_active=1 ", true);
		if($this->Sql->Numrows == 0){
			return array('error' => 'no such email');
		}

        $content = $this->Replace($rows[0]['content'], $data);
        $plaincontent = $this->Replace($rows[0]['content_source'], $data);
		$subject = $this->Replace($rows[0]['subject'], $data);

		return $this->SendEmail($to_name, $to_email, $rows[0]['from_name'], $rows[0]['from_email'], $subject, $content, $attachment, $plaincontent);
	}

	/**
	* same as sendStoredEmail but without stored email content
	*/
	function SendEmail($to_name, $to_email, $from_name, $from_email, $subject, $content, $attachment = '', $plaintext = ''){

		$ret = $this->phpMailer($to_name, $to_email, $from_name, $from_email, $subject, $content, $attachment, $plaintext);//default
		//$this->regular_mailer($to_name, $to_email, $from_name, $from_email, $subject, $content);

		$this->StoreEmail($to_name, $to_email, $from_name, $from_email, $subject, $content, 1);
		return $ret;
	}

	/**
	* same as sendStoredEmail but do not uses phpmailer
	*/
	function sendStoredRegularEmail($name, $data, $language, $to_name, $to_email){

		$rows = $this->Sql->Query("SELECT * FROM templates WHERE name = '".$name."' and type = 'EMAIL' and language = '".$language."' and is_active=1 ", true);
		$content = $this->Replace($rows[0]['content'], $data);
		$subject = $this->Replace($rows[0]['subject'], $data);
		return $this->regular_mailer($to_name, $to_email, $rows[0]['from_name'], $rows[0]['from_email'], $subject, $content);
	}

	/**
	* same as sendStoredRegularEmail but dont uses saved template
	*/
	function regularMailer($to_name, $to_email, $from_name, $from_email, $subject, $content){

		$Name = $from_name; //senders name
		$email = $from_email; //senders e-mail adress
		$recipient = $to_email; //recipient
		$mail_body = $content; //mail body
		$subject =  $subject; //subject
		$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
		$isSent = true;

		mail($recipient, $subject, $mail_body, $header) or ($isSent = false); //mail command :)
		return array('name' => $Name, 'email' => $email, 'recipient' => $recipient, 'body' => $mail_body, 'subject' => $subject, 'header' => $header, 'isSent' => $isSent);
	}

	private function phpMailer($to_name, $to_email, $from_name, $from_email, $subject, $content, $attachment = '', $plaintext = ''){

		require_once _Classes.'/phpMailer/class.phpmailer.php';

		$mail = new PHPMailer();
		$mail->IsSMTP();
    	$mail->IsHTML(true);
    	$mail->CharSet = 'UTF-8';

        $From        = $from_email;
        $FromName    = $from_name;
        $Hostname    = "localhost";

        $mail->Host = $Hostname;
        $mail->From = $From;
        $mail->FromName = $FromName;

        $content  = $content;

        $mail->AddAddress($to_email, $to_name);
		if($attachment){

			if(is_Array($attachment))
				$mail->AddAttachment($attachment[0], $attachment[1]);
			else
				$mail->AddAttachment($attachment);
		}

        $mail->Subject = "=?utf-8?b?".base64_encode($subject)."?=";
        $mail->Body = $content;
		$content = str_replace('<br/>', '<br>', $content);
        $mail->AltBody = strlen($plaintext) > 0 ? $plaintext : trim(str_replace('    ', '', strip_tags(str_replace('<br>', "\r\n", $content)))); //plain text

		$mail->isSent = false;
		if($mail->Send()) $mail->isSent = true;

		return $mail;
	}

	private function StoreEmail($to_name, $to_email, $from_name, $from_email, $subject, $content, $is_sent = 0){

		$this->Sql->table = 'emails';
		$this->Sql->Query("INSERT INTO emails (client_name, client_email, from_name, from_email, subject, content, is_sent, created_at, sent_at) VALUES ('".addslashes($to_name)."', '".addslashes($to_email)."', '".addslashes($from_name)."', '".addslashes($from_email)."', '".addslashes($subject)."', '".addslashes($content)."', '".$is_sent."', NOW(), ".($is_sent == 1 ? 'NOW()' : '').") ");

		//insert into email_sender
	}
}
