javascript - No file data showing for attachment with Ajax -
so i'm having bit of issue when trying send email myself attachment. i'm not using standard php mail function, i'm using phpmailer because of convenience. i'm running post through ajax call , i've done extensive research make sure formats correctly.
what happening when submitting form data sends through correctly. i'm getting image name, nothing else it. content type set correctly, , else set correctly, i"m confused going on , use little insight.
all code sectioned off below
html form index.php
<form id='requestartform' action='' method='post'> <div class='row'> <section class='form-group col-sm-6 col-xs-12'> <label class='control-label req'>name</label> <input type='text' class='form-control' id='name' data-validator='notempty'/> </section> <section class='clearfix'></section> <section class='form-group col-sm-6 col-xs-12'> <label class='control-label req'>e-mail address</label> <input type='email' class='form-control' id='email_address' data-validator='notempty|isemail' /> </section> <section class='clearfix'></section> <section class='form-group col-sm-6 col-xs-12'> <label class='control-label'>phone number</label> <input type='tel' class='form-control' id='phone' data-validator='isphonenumber' /> </section> <section class='clearfix'></section> <section class='form-group col-sm-6 col-xs-12'> <label class='control-label'>upload image</label> <input type='file' name='images[]' multiple/> </section> <section class='clearfix'></section> <section class='form-group col-xs-12'> <label class='control-label req'>additional comments</label> <textarea class='form-control' rows='5' id='comments' data-validator='notempty'></textarea> </section> <section class='form-group col-xs-12'> <button type='submit' class='btn btn-primary pull-right'>send message</button> </section> </div> </form>
javascript main_scripts.php
$('#requestartform').submit(function(e){ e.preventdefault(); $('body').spin('large'); var formdata = new formdata(); formdata.append('callback', 'requestdrawing'); formdata.append('parameters[]', $('#requestartform #name').val()); formdata.append('parameters[]', $('#requestartform #email_address').val()); formdata.append('parameters[]', $('#requestartform #phone').val()); formdata.append('parameters[]', $('#requestartform #comments').val()); $.each($('[name="images[]"]')[0].files, function(i, file){ formdata.append('images[]', file); }); if(validator($(this))){ $.ajax({ url : "<?=app_base?>assets/server/callbacks.php", type : 'post', data : formdata, datatype : 'json', contenttype : 'multipart/form-data', processdata : false, success : function(data){ $('body').spin(false); if(!data.errors){ }else{ } } }); }else{ $('body').spin(false); } });
php callbacks.php
$enabledfunctions = array(); $mail = new phpmailer(); function enablefunction($function){ global $enabledfunctions; array_push($enabledfunctions, $function); } function requestdrawing($name, $email_address, $phone, $comments){ global $mail; $response = array(); if(empty($name) or empty($email_address)){ $response['errors'] = true; $response['message'] = "please make sure required fields filled out. fields marked asterisk required."; }else{ $body = "you have new message $name\n\n"; $body .= "name: $name\n"; $body .= "email address: $email_address\n"; $body .= "phone number: $phone\n"; $body .= "comments: $comments\n\n"; $mail->from = "$email_address"; $mail->fromname = "$name"; $mail->addaddress("mark@neartist.com", "mark hill"); $mail->addreplyto("$email_address", "$name"); if (isset($_files['images']) && $_files['images']['error'] == upload_err_ok) { $mail->addattachment($_files['images']['tmp_name'], $_files['images']['name']); } $mail->subject = "new message $name"; $mail->body = $body; if(!$mail->send()){ $response['errors'] = true; $response['success'] = "there error when trying send message, please try again later."; }else{ $response['success'] = "your message has been sent!"; } } return $response; } enablefunction('requestdrawing'); $function = $_post['callback']; $parameters = isset($_post['parameters']) ? array_values($_post['parameters']) : ""; if(empty($parameters) && in_array($function, $enabledfunctions)){ echo json_encode(call_user_func($function)); }elseif(in_array($function, $enabledfunctions)){ echo json_encode(call_user_func_array($function, $parameters)); }
i thoroughly followed phpmailer documentation , double checked have written out here, nothing seems giving me proper solution.
edit:
setting content-type : 'multipart/form-data'
provides php error of undefined index: callback
. setting enctype on form provides identical error. setting false sends message have no attachment.
also, when looking through devloper tools, when check response tab, when run print_r($_files['images']
array ( [name] => 20150625_140017.jpg [type] => [tmp_name] => [error] => 1 [size] => 0 )
take @ the example provided phpmailer. you're missing standard stuff handling file uploads in php, it's done correctly in example.
setting submitter's address address fail spf checks - put own address in , submitter's in reply-to.
it looks you've based code on obsolete example, make sure have latest phpmailer too.
Comments
Post a Comment