<?php

require_once ('PHPMailer_5.2.0/class.phpmailer.php');

$position = $_POST["position"];
$name = $_POST["full-name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$message = $_POST["message"];

$resume = $_POST["resume"];

$resume_file = $_FILES["resume-file"];
$resume_name  = $resume_file['name'];
ob_start(); ?>
<html>
    <head>
        <title>Apply</title>
    </head>
    <body>
         <table>
            <tr>
                <td colspan='2'><b><?php echo $position; ?> </b></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><?php echo $name; ?></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td><?php echo $phone; ?></td>
            </tr>
        
            <tr>
                <td>Message:</td>
                <td><?php echo $message;  ?></td>
            </tr>          
            
        </table> 
    </body>
</html>
<?php
$msg = ob_get_clean(); 


$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP

$mail->SMTPAuth = true;   
$mail->SMTPSecure = "tls";               // enable SMTP authentication
$mail->Host = "mail.dilig.net";
$mail->Port = 587;
$mail->Username  = "contactus@dilig.net"; // SMTP username
$mail->Password  = "contactus#dlgnt";     // SMTP password
$mail->SetFrom($email, 'Nikola Ignjatovic');
$data = substr($resume, strpos($resume, ","));
$filename = $resume_name; 
$encoding = "base64"; 
//$type = "application/vnd.ms-excel";
$type =  substr( $resume , 6 , strpos($resume, ";") - 5);
$mail->AddStringAttachment(base64_decode($data), $resume_name, $encoding, $type);       

$mail->addAttachment( $resume);

$mail->Subject = "Apply for job " . $position;

$mail->Body = $msg;

$mail->AddAddress('hr@dilig.net', "HR Diligent");
$mail->IsHtml(true);

$mail->send();


echo  json_encode($msg);
?>