<?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'])
		{
		// pro mail-body
		case 'text':		// array('type'=>'text', 'content'=>'a\nb\nc')
		case 'text_file':	// array('type'=>'text_file', 'content'=>file_get_contents('path')}
			{
			$out[] = 'Content-type: text/plain; charset=utf-8';	//'Content-type: text/html; charset=iso-8859-1',
			if ($data['type']=='text')
				{
				$out[] = 'Content-Transfer-Encoding: quoted-printable';
				$out[] = '';
				$out[] = $data['content'];
				break;
				}
			$out[] = 'Content-Transfer-Encoding: base64';
			$out[] = '';
			$out[] = $this->fileToBase64($data['content']);
			break;
			}
		case 'html':
		case 'html_file':
			{
			$out[] = 'Content-type: text/html; charset=utf-8';
			if ($data['type']=='html')
				{
				$out[] = '';
				$out[] = $data['content'];
				break;
				}
			$out[] = 'Content-Transfer-Encoding: base64';
			$out[] = '';
			$out[] = $this->filetoBase64($data['content']);
			}
		// pro mail-att
		case 'file':
			$out[] = 'Content-type: text/html; charset=utf-8';
			$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;
	}
	
//private function headersCreate()
//	{
//	}

//private function messagesCreate()
//	{
//	}

//rfc2557 ? https://tools.ietf.org/html/rfc2557 | https://www.ietf.org/rfc/rfc2557.txt / 1999
// https://tools.ietf.org/html/rfc7231#section-3.1.4.2
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[] = '';
			}
		}

/*
Nefunguje to, aspon Thunderbird ten obrazek nezobrazi
	$messages[] = '--' . $uid;
	$messages[] = 'Content-Type:Text/html; charset="US-ASCII"';
	$messages[] = '';
	$messages[] = '<html><head><title>Test
          </title></head>
          <body><img src="http://www.dsv.su.se/~yvonne-b/yvonne.jpg"/> yvonne.jpg
          <img src="teckning.gif"/> teckning.gif
<IMG SRC="cid:foo4atfoo1atbar.net" ALT="IETF logo">
          </body></html>';
	$messages[] = '';
	$messages[] = '';

	$messages[] = '--' . $uid;
	$messages[] = 'Content-Type: image/jpg';
	$messages[] = 'Content-Transfer-Encoding: base64';
	$messages[] = 'Content-ID: <foo4atfoo1atbar.net>';
	$messages[] = 'Content-Location: cid:foo4atfoo1atbar.net';
	$messages[] = '';
	$messages[] = 'R0lGODlhPABSAMQAAP///+He3tXU2sjK1bzB0K+3zKOtx5ajw4qavX2QuXCGtGR9r1dyrExppz9gojJW
nSZMmBlClAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAEALAAA
AAA8AFIAAAX/YCCOZGmewbKgbOu+55IkK2zfsEzjfF8KDofAR8QtHAaDo1ZsmgaKBCSSjEASioFze3g4
IAxFJKJgeB+Hre/QeDwW7wCj4WCk6l5GWv0SKB5jD3NDDSoNAQJ0gBBxfC00EAgNUQ4iDQ1zIg5YDQhu
CY4oAw0RaYUKhwGXl5YKC4cHEEyhAQMEAQkODyJzDEIBdUeIggx2AYIKAglDjioDYcYIYDQFCkFZDAkN
kbwNMwfGagVjArGFBnoODQZyuisGdAcLBq8NBQRjjkAIiFh+A0G8bAsCQVEQKH6W4ZpFRIAvXwpGEHjl
YIzFixjrMLgVgMCXgr94CDhy6UsqEQIA/80hViBIAYqYIjxgFqBASTAOIuKgQaecgQQEtIhYoIDANkAY
HxScyKTAswMGBjDaiAPBgnsNAIopJULBlDcJDhQYWwABjUU6';
	$messages[] = '';
	$messages[] = '';
*/
        	
	$messages[] = '--' . $uid . '--';
	$messages   = implode($this->sep, $messages);

	// set mail server, if not set in php.ini
	$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]);
			}
		}
	// send mail
//echo '<hr><pre>'; var_dump(array($to_text, $subject, $messages, $headers)); echo '</pre>'; 
//die();
	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)
///		{
//		}
?>