<?php
class classMail
{
//private $to, $subject, $message, $headers;
private $sep, $server;

function __construct($server=array())
	{
	$this->sep = PHP_EOL;
	$this->server = $server;
	}

private function fileToBase64($str)
	{
	return chunk_split(base64_encode($str), 76, $this->sep);
//	return  wordwrap(base64_encode($data['content']), 76, $this->sep, true);
	}

private function createMessage($data)
	{
	if (!isset($data['content']) || $data['content']=='' || !isset($data['type']))
		{
		return;
		}
	$out = array();
	switch ($data['type'])
		{
		case 'text':	// array('type'=>'text', 'content'=>'a\nb\nc')
		case 'text_file':	// array('type'=>'text', 'content'=>file_get_contents('path')}
			{
			$out[] = 'Content-type: text/plain; charset=utf-8';	//'Content-type: text/html; charset=iso-8859-1',
			$out[] = 'Content-Transfer-Encoding: quoted-printable';
			$out[] = '';
			if ($data['type']=='text')
				{
				$out[] = $data['content'];
				break;
				}
			$out[] = $this->fileToBase64($data['content']);
			break;
			}
		case 'html':
		case 'html_file':
			{
			$out[] = 'Content-type: text/html; charset=utf-8';
			$out[] = '';
			if ($data['type']=='html')
				{
				$out[] = $data['content'];
				break;
				}
			$out[] = $this->filetoBase64($data['content']);
			}
		case 'file':
			$out[] = 'Content-Type: application/octet-stream; name="'.$data['filename'].'"';
			$out[] = 'Content-Transfer-Encoding: base64';
			$out[] = 'Content-Disposition: attachment; filename="'.$data['filename'].'"';
			$out[] = '';
			$out[] = $this->fileToBase64($data['content']);
			break;
		default: break;
		}
	return $out;
	}
	
public function send($form, $server=array())	//$from, $to, $subject, $message, $domena
	{
	$keys = array(
		'from',
		'to',
		'notify',
		'subject',
		'message',
		'att'
		);
	foreach ($keys as $value)
		{
		$$value = isset($form[$value]) ? $form[$value] : '';
		}
	if (!isset($from) || $from==""
		|| !isset($to) || $to==""
		|| !isset($subject) || $subject==""
		)
		{
		return;
		}
	//if ($domena=="localhost ") {return false;} // upraveno
	$from_text    = $from;
	$to_text      = $to;
	$subject_text = $subject;
	$from      = "=?utf-8?B?".base64_encode($from)   ."?=";
	$to        = "=?utf-8?B?".base64_encode($to)     ."?=";
	$subject   = "=?utf-8?B?".base64_encode($subject)."?=";
	
	$uid       = substr(sha1(uniqid(time())),0,20);
	$uid       = 'NextPart uid=' . $uid;
//	$part_sep  = '--NextPart uid=' . $uid;
//	$uid_sep = '--'.$uid;

	// ??? RFC 821, RFC 2821, rfc1341
	$headers   = array();
	$headers[] = "From: $from";
	$headers[] = "Reply-To: $from";
	$headers[] = "Return-To: $from";
	if ($form['notify']==true)
		{
		$headers[] = "Disposition-Notification-To: $from";
		$headers[] = "Return-Receipt-To: $from";
		}
	$headers[] = 'MIME-Version: 1.0';
	$headers[] = 'X-Mailer: PHP/' . phpversion();
	$headers[] = 'Content-Type: multipart/mixed; boundary="' . $uid . '"';
//	$headers[] = '';	// pro slu.cz musi byt zakomentovano
	$headers[] = 'This is a multi-part message in MIME format.';
	$headers   = implode($this->sep, $headers);

	$messages   = array();
	$messages[] = 'test zprava';
	if (isset($message) && trim($message)!=='')	// $form['message']
		{
		array_unshift($att, $this->createMessage(array('type'=>'text', 'content'=>$message)));
		}
	if (count($att)>0)
		{
		foreach($att as $item)
			{
			$messages[] = '--' . $uid;
			$messages[] = implode($this->sep, $this->createMessage($item));
//			$messages[] = '';
			$messages[] = '';
			}
		}
	$messages[] = '--' . $uid . '--';
	$messages   = implode($this->sep, $messages);
	
	$server  = count($server)>0 ? $server : $this->server;
	$key_all = array('SMTP', 'smtp_port', 'sendmail_from');
	foreach($key_all as $key)
		{
		if (isset($server[$key]))
			{
			ini_set($key, $server[$key]);
			}
		}
//echo '<hr><pre>'; var_dump(array($to_text, $subject, $messages, $headers)); echo '</pre>'; 
	return mail($to_text, $subject, $messages, $headers);
	}
}
//			$this->createMessage($messages, $item);
//			$this->createMessage($messages, array('type'=>'file', 'content'=>file_get_contents($item['path'])));
///	$message = array();
//	foreach ($messages as $value)
///		{
//		}
?>