<?php


class Email{
	//global $config;
	function __construct($from, $to, $subject, $body, $replyTo = $config["OrderEmail"]){
		global $con;
		global $config;
		global $root;
		//$typeText = "Purchase Order";
		//$subject = "New $typeText #" . $this->PurchaseOrderID;

		require_once $_SERVER['DOCUMENT_ROOT'] . '/assets/PHPMailerAutoload.php';

		$results_messages = array();

		$mail = new PHPMailer(true);

		$mail->CharSet = 'utf-8';

		//class phpmailerAppException extends phpmailerException {}

		//try {
		//	if(is_null($to)){
		//		$to = $this->Vendor->OrderEmail;
		//}else{
		//		$to = "";
		//}
		//if(!PHPMailer::validateAddress($to)) {
		//  throw new phpmailerException("Email address " . $to . " is invalid -- aborting!");
		//}
		$mail->isMail();
		$mail->addReplyTo($config['FromEmail']);
		$mail->From       = $config['FromEmail'];
		$mail->FromName   = $config['FromEmailName'];

		$mail->addAddress($this->Vendor->OrderEmail);
		//var_dump($this->Vendor->OrderEmail);
		try{
			$to = explode(",",$this->Vendor->OrderEmail);
			foreach($to as $email){
				$mail->addCC($email);
			}
		} catch (Exception $e ){
			echo $e;
		}

		$mail->addBCC($config['OrderEmail']);

		$mail->Subject  = $subject;
		$title = $config["FromEmailName"];
		$body =
		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
		<html>
		<head>
		  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		  <title>' . $title . '</title>
		</head>
		<body>
		  <div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 16px;">
			<img src="'.$root.'/img/'. $config['SmallLogo'] .'" style="width: 150px;" />
			<p>New ' . $typeText . ' for ' . $this->Vendor->Name . ' <br />
			Our Account Number: ' . $this->Vendor->OurAccountNumber . '<br />
			Notas: ' . $this->Notes . '<br />

			<a href="' . $root . 'mod.php?p=purchasing&a=viewpo&id=' . $this->PurchaseOrderID . '&key=' . $this->getTokenKey() . '">View Purchase Order #' . $this->PurchaseOrderID . '</a>
			</p>
			<div align="center">

			</div>

		  </div>
		</body>
		</html>';

		$mail->WordWrap = 78;
		$mail->msgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images
		//$mail->addAttachment('images/phpmailer_mini.png','phpmailer_mini.png');  // optional name
		//$mail->addAttachment('images/phpmailer.png', 'phpmailer.png');  // optional name

		$privatekeyfile = 'dkim_private.key';
		//Make a new key pair
		//(2048 bits is the recommended minimum key length -
		//gmail won't accept less than 1024 bits)
		$pk = openssl_pkey_new(
			array(
				'private_key_bits' => 2048,
				'private_key_type' => OPENSSL_KEYTYPE_RSA
			)
		);
		openssl_pkey_export_to_file($pk, $privatekeyfile);

		$mail->DKIM_domain = 'eli-tpr.com';
		$mail->DKIM_private = $privatekeyfile;
		$mail->DKIM_selector = 'phpmailer';
		$mail->DKIM_passphrase = ''; //key is not encrypted


		try {
		  ($mail->send());
		  unlink($privatekeyfile);
		  $results_messages[] = "Message has been sent using MAIL";
		  return $results_messages;
		}catch (phpmailerException $e) {
		  throw new phpmailerException('Unable to send to: ' . $to. ': '.$e->getMessage());
		}
		}
		catch (phpmailerException $e) {
		  $results_messages[] = $e->errorMessage();
		}
	}


	}
	function send(){

	}
}


?>
