jobb_form.php

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    try {
        $fulltNavn = $_POST['fullt_navn'];
        $epost = $_POST['email'];
        $telefon = $_POST['telefonnummer'];
        $linkedin = $_POST['linkedin'];
        $cv = $_FILES['cv']['name'];
        $tekst = $_POST['tekst_soknad'];

        // Legg e-postadressen du ønsker å motta skjemaet på
        $to = "dummyuser34@gmail.com";

        // Lag e-postmelding
        $subject = "Jobbsøknad fra $fulltNavn";
        $message = "Fullt navn: $fulltNavn\n\n";
        $message .= "Epostadresse: $epost\n\n";
        $message .= "Telefonnummer: $telefon\n\n";
        $message .= "Linkedin konto: $linkedin\n\n";
        $message .= "CV: $cv\n\n";
        $message .= "Tekst: $tekst\n\n";

        // Legg ved CV hvis den ble lastet opp
        if (!empty($_FILES['cv']['tmp_name']) && file_exists($_FILES['cv']['tmp_name'])) {
            $file_path = $_FILES['cv']['tmp_name'];
            $file_type = $_FILES['cv']['type'];
            $file_name = $_FILES['cv']['name'];
            $file_size = $_FILES['cv']['size'];
            $file_error = $_FILES['cv']['error'];

            if ($file_error === UPLOAD_ERR_OK) {
                $attachment = chunk_split(base64_encode(file_get_contents($file_path)));
                $boundary = md5(time());
                $headers = "From: $epost\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
                $message = "This is a multi-part message in MIME format.\r\n";
                $message .= "--$boundary\r\n";
                $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
                $message .= "Content-Transfer-Encoding: 7bit\r\n\n";
                $message .= $message . "\r\n";
                $message .= "--$boundary\r\n";
                $message .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
                $message .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
                $message .= "Content-Transfer-Encoding: base64\r\n\n";
                $message .= $attachment . "\r\n";
                $message .= "--$boundary--";
            }
        }

        // Send e-post
        $headers = "From: $epost\r\n";
        $headers .= "Reply-To: $epost\r\n";

        if (mail($to, $subject, $message, $headers)) {
            echo json_encode(array('success' => true));
        } else {
            echo json_encode(array('success' => false, 'message' => 'Noe gikk galt ved sending av e-post.'));
        }
    } catch (Exception $e) {
        // Fang opp eventuelle unntak og logg feilmeldingen
        error_log("Feil ved behandling av skjema: " . $e->getMessage());
        echo json_encode(array('success' => false, 'message' => 'Noe gikk galt under behandling av skjemaet.'));
    }
}
?>