<?php
// upload recommendation file section
$target_dir = "wp-content/uploads/rec_file/";
$target_file = $target_dir . basename($_FILES["RC_file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["RC_file"]["tmp_name"]);
    if($check !== false) {
        $error_msg = "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        $error_msg = "File is not an image.";
        $uploadOk = 0;
    }
// Check if file already exists
if (file_exists($target_file)) {
    $error_msg = "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["RC_file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    $error_msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    $error_msg = "your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["RC_file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["RC_file"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file. ". $error_msg ;
    }
}
// upload driven licence file section
$DL_target_dir = "wp-content/uploads/rec_file/";
$DL_target_file = $DL_target_dir . basename($_FILES["DL_file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($DL_target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["DL_file"]["tmp_name"]);
    if($check !== false) {
        $error_msg = "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        $error_msg = "File is not an image.";
        $uploadOk = 0;
    }
// Check if file already exists
if (file_exists($DL_target_file)) {
    $error_msg = "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["DL_file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    $error_msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    $error_msg = "your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["DL_file"]["tmp_name"], $DL_target_file)) {
        echo "The file ". basename( $_FILES["DL_file"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file. ". $error_msg ;
    }
}
    // array with filenames to be sent as attachment
    $files = array($target_file,$DL_target_file);
    // email fields: to, from, subject, and so on
    $to = "tkdilankumara@gmail.com";
    $from = "help@soosti.com"; 
    $subject ="Member Registration"; 
    $headers = "From: $from";
    $eol = "\r\n";
    $message .= "Member Registration Information.".$eol;
    $message .= $username.$eol;
    $message .= $email.$eol;
    $message .= $Fname.$eol;
    $message .= $Lname.$eol;
    $message .= $address.$eol;
        // 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" . " boundary=\"{$mime_boundary}\"";
    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";
        // preparing attachments
    for($x=0;$x<count($files);$x++){
        //$file = @fopen($files[$x],"rb");
        //$data = @fread($file,filesize($files[$x]));
        //@fclose($file);
        $content =file_get_contents($files[$x],"rb");
        $content = chunk_split(base64_encode($content));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }
// send
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
}
?>