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

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

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(&$messages, $data)
	{
	if ($data['content']=='')
		{
		return;
		}
	if (isset($data['type']))
		{
		switch ($data['type'])
			{
			case 'text':	// array('type'=>'text', 'content'=>'a\nb\nc')
			case 'text_file':	// array('type'=>'text', 'content'=>file_get_contents('path')}
				{
				$messages[] = 'Content-type: text/plain; charset=utf-8';	//'Content-type: text/html; charset=iso-8859-1',
				$messages[] = 'Content-Transfer-Encoding: quoted-printable';
				$messages[] = '';
				if ($data['type']=='text')
					{
					$messages[] = $data['content'];
					break;
					}
				$messages[] = $this->fileToBase64($data['content']);
				break;
				}
			case 'html':
			case 'html_file':
				{
				$messages[] = 'Content-type: text/html; charset=utf-8';
				$messages[] = '';
				if ($data['type']=='html')
					{
					$messages[] = $data['content'];
					break;
					}
				$messages[] = $this->filetoBase64($data['content']);
				}
			case 'file':
				$messages[] = 'Content-Type: application/octet-stream; name="'.$data['filename'].'"';
				$messages[] = 'Content-Transfer-Encoding: base64';
				$messages[] = 'Content-Disposition: attachment; filename="'.$data['filename'].'"';
				$messages[] = '';
				$messages[] = $this->fileToBase64($data['content']);
				break;
			default: break;
			}
		}
	//return $messages;
	}
	
public function create($form)	//$from, $to, $subject, $message, $domena
	{
	$keys = array(
		'from',
		'to',
		'notify',
		'subject',
		'message',
		'att'
		);
	foreach ($keys as $value)
		{
		$$value = isset($form[$value]) ? $form[$value] : '';
		}
	if ($from="" || $to="")
		{
		return;
		}
	//if ($domena=="localhost ") {return false;} // upraveno
	$from      = "=?utf-8?B?".base64_encode($from)   ."?=";
	$to        = "=?utf-8?B?".base64_encode($to)     ."?=";
	$subject   = "=?utf-8?B?".base64_encode($subject)."?=";
	
	$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[] = '';
	$headers[] = 'This is a multi-part message in MIME format.';
	$headers   = implode($this->sep, $headers);

	$uid  = '--NextPart uid='.substr(sha1(uniqid(time())),0,20);
	$uid_sep = '--'.$uid;

	$messages = array();
	if (trim($message)!=='')
		{
		$this->createMessage($messages, array('type'=>'text', 'content'=>$message));
		}
	if (count($att)>0)
		{
		foreach($att as $item)
			{
			$this->createMessage($messages, $item);
//			$this->createMessage($messages, array('type'=>'file', 'content'=>file_get_contents($item['path'])));
			}
		}
	
	return mail($to, $subject, $message, $headers);
	}
}


/*
function mailAdd($type='',$path='',$filename='',$message='')
{
$body = array();
if ($type!='')
	{
	switch ($type)
		{
		case 'text':
			{
			$body[] = 'Content-type:text/plain; charset=utf-8';	//'Content-type: text/html; charset=iso-8859-1',
			$body[] = 'Content-Transfer-Encoding: quoted-printable';
			$body[] = '';
			if ($message!='')
				{
				$body[] = $message;
				}
			elseif (file_exists($path))
				{
				$body[] = chunk_split(base64_encode(file_get_contents($path)));
				}
			break;
			}
		case 'html':
			{
			$body[] = 'Content-type:text/html; charset=utf-8';
			$body[] = '';
			if ($message!='')
				{
				$body[] = $message;
				}
			elseif (file_exists($path))
				{
				$body[] = chunk_split(base64_encode(file_get_contents($path)));
				}
			break;
			}
		case 'file':
			if (file_exists($path))
				{
				$body[] = 'Content-Type: application/octet-stream; name="'.$filename.'"';
				$body[] = 'Content-Transfer-Encoding: base64';
				$body[] = 'Content-Disposition: attachment; filename="'.$filename.'"';
				$body[] = '';
				$body[] = chunk_split(base64_encode(file_get_contents($path)));
				}
			break;
		default: break;
		}
	}
return $body;
}


function createMail($schedule_data=array(),$contact_data=array(),$query='')
{
global $CFG,$sql,$url;
//$sep  = "\r\n";	// PHP_EOL
$sep  = PHP_EOL;
$mail = array();
$att  = array();
$id   = $schedule_data['id_schedule'] * 1;
$mail['from'] = $schedule_data['mail_from'];
$msg  = $schedule_data['mail_body'];
$mail['subj'] = "=?UTF-8?B?".base64_encode($schedule_data['mail_subj'])."?=";
$uid  = '--NextPart uid='.substr(sha1(uniqid(time())),0,20);
$uid_sep = '--'.$uid;
$link_view = '<p>Obsah teto zpravy lze zobrazit na <a href="'.$url['home' ].$url['schedule_view'].'?'.'id_schedule'.'='.$id.'" target="_blank">nasi webove strance</a>.</p>';
$link_active = '';
//var_dump(isset($url['home' ]),isset($contact_data['mail']) , isset($contact_data['deactivate_code']));
if (isset($url['home' ]) && isset($url['mail_deactive']) && isset($url['mail_deactive'])
     && isset($contact_data['mail']) && isset($contact_data['deactivate_code']))
	{
	$link_active = '<a href="'.$url['home'].'%1$s'.'&amp;mail'.'='.$contact_data['mail'].'&amp;deactivate_code'.'='.$contact_data['deactivate_code'].'" target="_blank">%2$s</a>';
	$link_active = '<p>Kliknutim na jeden z odkazu muzete '.
		sprintf($link_active,$url['mail_deactive'],'zakazat').' / '.
		sprintf($link_active,$url['mail_active']  ,'povolit').'
		 odber newsletteru.</p>';
	}
$mail['body']    = array();
$mail['headers'] = array(
	'From: '.$mail['from'],
	'Reply-To: '.$mail['from']
	);
if ($schedule_data['mail_notification']==2)
	{
	$mail['headers'][] = 'Disposition-Notification-To: '.$mail['from'];
	$mail['headers'][] = 'Return-Receipt-To: '.$mail['from'];
//	$mail['headers'][] = 'X-pmrqc: 1';
//	$mail['headers'][] = 'X-Confirm-Reading-To: '.$mail['from'];
//	$mail['headers'][] = 'Delivery-Status-Notification: '.$mail['from'];
	}
$mail['headers'][] = 'MIME-Version: 1.0';
$mail['headers'][] = 'X-Mailer: PHP/' . phpversion();
$mail['headers'][] = 'Content-Type: multipart/mixed; boundary="'.$uid.'"';
$mail['headers'][] = '';
$mail['headers'][] = 'This is a multi-part message in MIME format.';

$body_type = $schedule_data['mail_body_type']==2 ? 'html' : 'text';
$msg = $body_type=='text' ? nl2br($msg) : $msg;
$msg = $link_view . $msg . $link_active;
$mail['body'][] = $uid_sep;
$mail['body'][] = implode($sep, mailAdd('html','','',$msg));
$mail['body'][] = '';
$mail['body'][] = '';

//$mail['body'][] = $uid_sep;
//$mail['body'][] = implode($sep, mailAdd('html','','',$link_view));
//$mail['body'][] = '';
//$mail['body'][] = '';
//$mail['body'][] = $uid_sep;
//$mail['body'][] = implode($sep, mailAdd($body_type,'','',$msg));
//$mail['body'][] = '';
//$mail['body'][] = '';
//$mail['body'][] = $uid_sep;
//$m/ail['body'][] = implode($sep, mailAdd('html','','',$link_active));
//$mail['body'][] = '';
//$mail['body'][] = '';

$path = $CFG['upload']['path'].'/'.$schedule_data['name'].'/';
//var_dump($data['id_schedule']);
$query  = sprintf($query,
		$sql->escape($id)
		);
$result = $sql->query($query);
while(($row = $sql->fetch($result))!==false)
	{
	if (isset($row['file']))
		{
		$mail['body'][] = $uid_sep;
		$mail['body'][] = implode($sep, mailAdd('file',$path.$row['file'],$row['file'],''));
		$mail['body'][] = '';
		$mail['body'][] = '';
		}
	}
$mail['body'][] = $uid_sep;
$mail['headers'] = implode($sep, $mail['headers']);
$mail['body']    = implode($sep, $mail['body']);
//var_dump($mail['body']);
return $mail;
}

-------------

function OdesliEmail($from, $to, $subject, $message, $domena)
{
//if ($domena=="localhost ") {return false;} // upraveno

$from      = "=?utf-8?B?".base64_encode($from)   ."?=";
$to        = "=?utf-8?B?".base64_encode($to)     ."?=";
$subject   = "=?utf-8?B?".base64_encode($subject)."?=";

$headers   = array();
$headers[] = "From: $from";
$headers[] = "Reply-To: $from";
$headers[] = "Return-To: $from";
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/html; charset=utf-8";
$headers[] = "Content-Transfer-Encoding: base64";
$headers[] = "X-Mailer: PHP/" . phpversion();
$headers[] = "";
$headers   = implode(PHP_EOL, $headers);        // konverze array na string pridanim PHP_EOL = \r\n nebo \n v nekterych verzich php

$message   = "<html>\n<body>\n$message\n</body>\n</html>";
$message   = wordwrap(base64_encode($message), 76, PHP_EOL, true); // tady by se to asi nemuselo wrapovat; a nejsem si jisty, jestli i body by nemelo projit kodovanim na utf8

return mail($to, $subject, $message, $headers); // pridano return
}

$bool = OdesliEmail("info@rosopida.cz <info@rosopida.cz>", 'test@mail.cz', 'test', 'test', $_SERVER['HTTP_HOST']);
if ($bool) {echo 'odeslano';} else {echo 'chyba pri odesilani';}

?>

    $filename = 'myfile';
    $path = 'your path goes here';
    $file = $path . "/" . $filename;

    $mailto = 'mail@mail.com';
    $subject = 'Subject';
    $message = 'My message';

    $content = file_get_contents($file);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (RFC)
    $eol = "\r\n";

    // main header (multipart mandatory)
    $headers = "From: name <test@test.com>" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

    //SEND Mail
    if (mail($mailto, $subject, $body, $headers)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
        print_r( error_get_last() );
    }
*/
?>