<div class="r" id="contact_index_form">

                    <div class="bs-callout bs-callout-warning hidden">
                        <h4>Niestety, nie wszystko jest wypełnione poprawnie!</h4>
                    </div>

                    <div class="bs-callout bs-callout-info hidden">
                        <h4>Świetnie, wszystko ok!</h4>
                    </div>
    <?php
    
    // if you want only logged in users to access this function use this hook
    add_action('wp_ajax_email_form_submit', 'send_email_form');

    // if you want none logged in users to access this function use this hook
    add_action('wp_ajax_nopriv_email_form_submit', 'send_email_form');

    // if you want both logged in and anonymous users to get the emails, use both hooks above

    
    function send_email_form() {
        
    $form_errors = array();

    if( isset($_POST['submit_form']) )  {

    $name = $_POST['fullname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $file = $_POST['file_field'];

    $subject = 'Z formularza na stronie';

    $content = "<html>
                    <head>
                        <style>
                            h3 {
                                text-transform: uppercase;
                                padding: 0 0 15px 0;
                                margin: 0;
                                font-size: 16px;
                            }
                            h4 {
                                font-size: 12px;
                                line-height: 16px;
                                padding: 0;
                                margin: 10px 0 0 0;
                                font-weight: 800;
                            }
                            p {
                                font-size: 12px;
                                line-height: 16px;
                                padding: 0;
                                margin: 0;
                            }
                        </style>
                    </head>
                        <body>
                                <h4>WIADOMOŚĆ Z FORMULARZA NA STRONIE</h4>
                                <h4>NADAWCA: </h4>
                                <p>$name, $email</p>
                                <h4>TREŚĆ: </h4>
                                <p class='message'>$message</p></br>
                        </body>
                </html>";
    
    foreach ($fields as $field) {
        if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
    }
            
    $errors = array_filter($form_errors);

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

        $uploadedfile = $_FILES['attachmentFile'];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

        if ( $movefile && ! isset( $movefile['error'] ) ) {         
            $movefile['url'];
        }

        $to = get_option('admin_email');
        $attachments = array($movefile['file']);

        $headers = array('Content-Type: text/html; charset=UTF-8',
                    'From: '.$name.' <'.$email.'>',
                    'Reply-To: '.$name.' <'.$email.'>');

        $success = wp_mail($to , $subject , $content , $headers , $attachments);
        return wp_send_json($success);
        
       
        
    }
} 
        
        die();
    }
    
    
?>
    
    <div id="message_ok">Y</div>
    <div id="message_error">X</div>
    
        <form id="index_form" action="email_form_submit" method="post" enctype="multipart/form-data">

        <span class="field_50">
            <label>Twoje imię i nazwisko</label>
            <input type="text" 
                name="fullname"
                value="<?php isset( $_POST["fullname"] ) ? esc_attr( $_POST["fullname"] ) : '' ?>"
                required>
        </span>

                        
        <span class="field_50">
            <label>Twój adres e-mail</label>
            <input type="email" 
                name="email"
                value="<?php isset( $_POST["email"] ) ? esc_attr( $_POST["email"] ) : '' ?>"
                required>
        </span>

         <span class="field_100"> 
            <label>Twoja wiadomość</label>       
            <textarea name="message"
                required><?php isset( $_POST["message"] ) ? esc_attr( $_POST["message"] ) : '' ?></textarea>
        </span>

        <div class="send_file">
            <p>Jeżeli chcesz, możesz dołączyć dodatkowe pliki spakowane w jedno archiwum *.zip (max. 20mb).</p>
            <div class="choose_file">

            <label for="file-upload_1" class="custom-file-upload">wskaż plik </label>

            <input
                id="file-upload_1"
                class="file-upload"
                type="file"
                name="attachmentFile"
                value="<?php isset( $_POST["file_field"] ) ? esc_attr( $_POST["file_field"] ) : '' ?>"
                accept="application/zip"
                data-parsley-max-file-size="20480">

            <button id="btn-file-reset_1" class="btn-file-reset" type="button"></button>
        </div>

        <b><label id="file-name_1" class="file-name"></label></b>
        <script>
            // wypisuje nazwę pliku
            $("#file-upload_1").change(function() {
                $("#file-name_1").text(this.files[0].name);
            });
            // resetuje wybrany plik
            $('#btn-file-reset_1').on('click', function(e) {
                var $el = $('#file-upload_1');
                $el.wrap('<form>').closest('form').get(0).reset();
                $el.unwrap();
                $("#file-name_1").text("");
            });

        </script>
        
        </div>
        
        <span class="submit_field">
            <input
                id="submit"
                name="submit_form"
                type="submit"
                value="Wyślij formularz">
            </span>


        </form>
</div>

        <script>
            $(function() {
            $('#index_form').parsley().on('field:validated', function() {
                var ok = $('.parsley-error').length === 0;
                $('.bs-callout-info').toggleClass('hidden', !ok);
                $('.bs-callout-warning').toggleClass('hidden', ok);
            })
                .on('form:submit', function() {
                    return true;
                });
            });

            window.Parsley.addValidator('maxFileSize', {
            validateString: function(_value, maxSize, parsleyInstance) {
                if (!window.FormData) {
                    alert('You are making all developpers in the world cringe. Upgrade your browser!');
                    return true;
                    }
                var files = parsleyInstance.$element[0].files;
                    return files.length != 1 || files[0].size <= maxSize * 1024;
                },
                    requirementType: 'integer',
                    messages: {
                        pl: 'Wybierz plik nie większy niż 20mb.'
                    }
                });
            
            

               $(document).on('submit', '#index_form', function (event) {

                event.preventDefault();
                var formData = $('#index_form').serialize();
                    $.ajax({
                        url: $('#index_form').attr('action'),
                        type: 'POST',
                        data: {
                            action: 'email_form_submit',
                        },
                        success: function() { 
                            $('#message_ok').slideDown(400);
                        },
                        error: function() {
                            $('#message_error').slideDown(400);

                        }
                    });

                });

            
            
        </script>