<?php

/**
 * @author Ahmet YUCEL
 * @copyright 2016
 */
 
 //require_once "PHPMailer/class.phpmailer.php";

$errors = '';
//<-----Put Your email address here.
if(empty($_POST['S_email'])  || 
   empty($_POST['R_email'])  ||
   empty($_POST['to'])  ||
   empty($_POST['Subject'])  ||
   empty($_POST['textarea'])   
   
   )
   
{
    $errors .= "\n Error: all fields are required";
}

 


require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;	

/* $mail->setFrom($Sender_email, $Sender_name). "\r\n";
$mail->addAddress($To) . "\r\n";// Add a recipient//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$mail->addReplyTo($reply, $reply_me);
$mail->isHTML(true);									// Set email format to HTML
//$mail->WordWrap = 50;
$mail->Subject = $subject;
$mail->Body    = $message;	
	
 */	
 
 if(isset($_FILES) && (bool) $_FILES) {
  
	$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt","xlsx","zip","xls",);
	
	$files = array();
	foreach($_FILES as $name=>$file) {
		$file_name = $file['name']; 
		$temp_name = $file['tmp_name'];
		$path_parts = pathinfo($file_name);
		$ext = $path_parts['extension'];
		if(!in_array($ext,$allowedExtensions)) {
			die("File $file_name has the extensions $ext which is not allowed");
		}
		$server_file = "upload/$path_parts[basename]";
		move_uploaded_file($temp_name,$server_file);
		array_push($files,$server_file);
	}
	
 }
 
 $priority = 1;
 $Sender_email = $_POST['S_email']; 
 $Sender_name = $_POST['S_name']; 
 $To = $_POST['to'];
 $reply = $_POST['Reply_to'];
 $reply_me = $_POST['reply_next'];
 $subject = $_POST['Subject'];
 $message = $_POST['textarea'];



 if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$Sender_email))
{
    $errors .= "\n Error: Invalid email address";
}  
	
	$file_upload = $server_file;
	
	// email fields: to, from, subject, and so on
	/* $to = $To; */
	/* $to = $To;
	$name = $Sender_name;
	$from = $Sender_email; 
	$subject = $subject; 
	$message = $message;
	$headers = "From: $from";
	$headers .= "$name"; */
	
	
	$mail  = new PHPMailer();
	$mail->From      = $Sender_email;
	$mail->FromName  = $Sender_name;
	$mail->Subject   = $subject;
	$mail->Body      = $message;
	$mail->AddAddress ( $To );
	$mail->addReplyTo($reply, $reply_me);
	$mail->addAttachment($file_upload);
	
	$mail->isHTML(true);
	
	
	// boundary 
	/* $semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  */
	 
	// headers for attachment 
	
	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n"; 
	 
	// multipart boundary 
	$message = "This is a multi-part message in MIME format.\n\n"  . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
	/* $message .= "--{$mime_boundary}\n"; */
	 
	// preparing attachments
	foreach($files as $file){
		$aFile = fopen($file,"rb");
		$data = fread($aFile,filesize($file));
		fclose($aFile);
		$data = chunk_split(base64_encode($data));
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
		"Content-Disposition: attachment;\n" . " filename=\"$file\"\n" . 
		"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
		
	}
	// send
	if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//$mail->ClearAllRecipients();
	//header('Location: mymailer.html');
	echo("Message has been sent!");;
}



?>