<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

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) {

		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'];

		include_once 'Mail.php';
		include_once 'Mail/mime.php';

		$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 = '';

		$crlf = "\n";
		$hdrs = array(
			'From' => (($from_name) ? $from_name : $user).' <'.(($from_mail) ? $from_mail : $reply).'>',
			'Sender' => $name.' <'.$email.'>',
			'Reply-To'=> (($reply_to) ? $reply_to : $reply),
			'Subject' => $subject,
			'To' => $recipients['to'],
			'Cc' => $recipients['cc'],
			'Bcc' => $recipients['bcc']
		);

		$mime = new Mail_mime($crlf);

		$inline_message = '';
		/*if ($logo) {
			if ($logo == 'intern') {
				$mime->addHTMLimage(ROOT_DIR.'web/'.SHOP_SYSTEM.'/images/static/logo4mail2.jpg', 'image/jpeg');
				$inline_logo = '<img src="'.ROOT_DIR.'web/'.SHOP_SYSTEM.'images/static/logo4mail2.jpg">';
				$inline_message .= '<b>ELISA</b> - <b>E</b>asyway-Shop <b>L</b>ive <b>I</b>nformation <b>S</b>ervices and <b>A</b>nnouncements';
			} else if ($logo == 'extern') {
				$inline_message .= $global_mail_settings['inline_text'];
				$mime->addHTMLimage(ROOT_DIR.'web/'.SHOP_SYSTEM.'/themes/'.THEME.'/images/logo4mail.jpg', 'image/jpeg');
				$inline_logo = '<img src="'.ROOT_DIR.'web/'.SHOP_SYSTEM.'/themes/'.THEME.'/images/logo4mail.jpg">';
			}
		} else {*/
			$inline_logo = '';
		//}


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

		$mime->setHTMLBody('<html><head><style type="text/css">'.$css.'</style></head><body>'.utf8_decode($message).'<br /><br />'.utf8_decode($inline_message).'<br />'.$inline_logo.'</body></html>');

		// attachments
		if ($attachments) {
			foreach ($attachments as $attachment) {
				if (is_object($attachment)) {
					$mime->addAttachment(
					 	$attachment->content,
					 	'application/octet-stream',
					 	$attachment->file_name,
					 	false,
					 	'base64',
					 	'attachment'
					);
				} else {
					$mime->addAttachment($attachment);
				}
			}
		}

		$body = $mime->get();
		$hdrs = $mime->headers($hdrs);

		$mailx =& Mail::factory('smtp', array('host' => $host, 'auth' => true,'socket_options' => array('ssl' => array('verify_peer_name' => false, 'allow_self_signed' => true)), 'username' => $user, 'password' => $pass));
		$sent = $mailx->send(implode(',', $recipients), $hdrs, $body);

		if (PEAR::isError($sent)) {
			$log_object = Logger::get_instance();
			$log_object->info("Error in /core/mail.class.php->send_mail()", "Cant't send email. Sendstate: ".$sent);
			return false;
		} else {
			$log_object = Logger::get_instance();
			$log_object->info("Error in /core/mail.class.php->send_mail()", "erfolgreich gesendet");
			return true;
		}

		return true;
	} // 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
}