PHPMailer can't see why this fails

Hi, This is making what hair I have left fall out!

I have used an online demo (https://phppot.com/jquery/jquery-contact-form-with-attachment-using-php/), which I downloaded and works ok, as the basis for an email form. I copied and pasted code from this to create my own form and handler and everything works ok except the attachments.

The html code in the form is:

                    <form id="frmEmail" method = "POST" action = "email_action.php"  enctype="multipart/form-data">
                        <div class="form-group sm row alert alert-success">
                            <label class="col-md-2 control-label" for="email">Subject:</label>  
                            <div class="col-md-8">
                                <input id="subject" required name="subject" class="form-control input-sm" type="text" value="">
                                <input type ="hidden" id ="id" name="id" value="<?php echo $id; ?>">
                                <input type ="hidden" id ="etype" name="etype" value="<?php echo $type; ?>">
                            </div>
                        </div>           
                        <div>
                            <textarea  id="summernote3"></textarea>
                        </div>
                        <br />
                        <div class="form-group sm row alert alert-success">
                            <label class="col-md-2 control-label" for="attachment"><b>Attachment(s)</b></label>
                            <br /> 
                            <div class="input-group">
                                <input type="file" name="files[]" class="AttachmentInputBox" multiple="multiple">
                            </div>
                        </div>
                        <button  id="btnEmail1"  name= "btnEmail1" class="btn btn-primary btn-sm"><span class = "fa fa-at"></span> Email</button>      
                    </form>

The JS code to submit is:

                $("#frmEmail").submit(function (event) {
                    event.preventDefault(); //prevent default action 
                    var subject = document.getElementById('subject').value;
                    var codeHtml = $('#summernote3').summernote('code');
                    if (subject.length < 5 || codeHtml.length < 12) {
                        alert("Please ensure both Subject and Message are entered correctly");
                        return false;
                    }

                    var post_url = $(this).attr("action"); //get form action url
                    var request_method = $(this).attr("method"); //get form GET/POST method
                    var form_data = $(this).serialize() + "&codeHtml=" + codeHtml;
                    // error trap - remove next 2 lines
                    var message = "About to fire redirect\r\nURL: " + post_url + "\r\nMethod: " + request_method + "\r\nData: " + form_data;
                    //alert(message);
                    $.post(post_url, form_data, function (data) {
                       window.location.href = "<?php echo $page ?>";
                    }).fail(function () {
                        alert("Sending email failed.");
                        window.location.href = "<?php echo $page ?>";
                    });

                    // to prevent refreshing the whole page page
                    return false;
                });

and in the email action code:

// if there are attachments....
for($ct=0;$ct<count($_FILES['files']['tmp_name']);$ct++){
    $mail->AddAttachment($_FILES['files']['tmp_name'][$ct],$_FILES['files']['name'][$ct]);
}

As stated the email is sent ok, but with no attachment(s).
The PHP log shows th following error
PHP Notice: Undefined index: files in C:\wamp64\www\AGC\email_action.php on line 35
Line 35 is the above “for” line.

Probably something very simple, but I just cannot see it,

Any help much appreciated.

The key point that makes the linked to code include the type=‘file’ field data is the use of the new FormData(this) call. The jquery .serialize() method call that you are using doesn’t include the type=‘file’ field data.

Many thanks - didn’t realise this. Now works fine

Sponsor our Newsletter | Privacy Policy | Terms of Service