Emails with the attachments

JS script

/*************************************************************
 *************  AJAX - Form sending  ****************
 *************************************************************/

function formSubmitTrigger() {

    $('#job-form').on('submit', function (e) {
        e.preventDefault();

        let submit_button = $(this).find('input[type=submit]');

        var data = new FormData(this);

        $.ajax({
            type: "POST",
            dataType: "json",
            url: ajaxurl,
            data: data,
            contentType: false,
            processData: false,
            beforeSend: function () {
                submit_button.attr("disabled", true);
                $(".spinner").show();
            },
            success: function (data) {
                $('.alert-notice').toggle().text(data['data']);
                $('#job-form')[0].reset();
                $(".spinner").hide();
                submit_button.attr("disabled", false);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $('.alert-notice').toggle().text(data['data']);
                console.log(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    })
}

Ajax function:


function contact_form_ajax(){

    $to = strip_tags($_POST['sendto']);

    $attachments = [];

        //upload file
        if (! function_exists('wp_handle_upload')) {
            require_once(ABSPATH . 'wp-admin/includes/file.php');
        }

        if($_FILES['file']) {
            foreach ($_FILES['file']['name'] as $n => $name) {
                $upload_overrides = ['test_form' => false ];
                $temp_file = $_FILES['file']['tmp_name'][$n];
                $file_name = $_FILES['file']['name'][$n];
                $file_path = WP_CONTENT_DIR . '/uploads/formattachments/' . $file_name;
                
                wp_handle_upload($temp_file, $upload_overrides);
                move_uploaded_file($temp_file, $file_path);
                
                $attachments[] .= $file_path;
            } 
        }


    $subject = "Neue Anfrage!";
    $headers = "From: Hochegger <". $to . ">\r\n";
    $headers .= "Reply-To: ". strip_tags($_POST['Email']) . "\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    $message = '<html><body>';
    $message .= '<h2>Neue Anfrage:</h2>';
    foreach ($_POST as $key => $value) {
        if(
            $key != 'action' && 
            $key != 'datenschutz' && 
            $key != 'sendto'
        ) {
         
            $message .= "<strong>" . strip_tags($key) . ": </strong>" . strip_tags($value);
            $message .= "<br/>";
        }
    }
    $message .= "<br/>";
    $message .= "--";
    $message .= "<br/>";
    $message .= "Hochegger Website";
    $message .= "</body></html>";


        //Confirmation email
        $to_confirm = $_POST['Email'];
        $subject_confirm = "Ihre Hochegger Anfrage";
        $headers_confirm = "From: Hochegger <". $to . ">\r\n";
        $headers_confirm .= "MIME-Version: 1.0" . "\r\n";
        $headers_confirm .= "Content-type: text/html; charset=UTF-8" . "\r\n";

        $message_user = '<html><body>';
        $message_user .= '<h2>Ihre Anfrage:</h2>';
        foreach ($_POST as $key => $value) {
            if(
                $key != 'action' && 
                $key != 'datenschutz' && 
                $key != 'sendto'
            ) {
             
                $message_user .= "<strong>" . strip_tags($key) . ": </strong>" . strip_tags($value);
                $message_user .= "<br/>";
            }
        }
        $message_user .= "<br/>";
        $message_user .= "--";
        $message_user .= "<br/>";
        $message_user .= "Hochegger Website";
        $message_user .= "</body></html>";

        wp_mail($to_confirm, $subject_confirm, $message_user, $headers_confirm, $attachments);


    // Here put your Validation and send mail
    $sent = wp_mail($to, $subject, $message, $headers, $attachments);

    if($sent) {
        wp_send_json_success();
        return true;
    }
    else  {
        $message = "Error!";
        $return = array(
            'message' => $message
        );
        wp_send_json_error($return);
    }

    die();
}

add_action('wp_ajax_nopriv_contact_form_ajax', 'contact_form_ajax');
add_action('wp_ajax_contact_form_ajax', 'contact_form_ajax');