So I'm having a bit of an issue when trying to send an email to myself with an attachment. I'm not using the standard php mail function, I'm using PHPMailer because of convenience. I'm running the post through an Ajax call and I've done extensive research to make sure that it formats correctly.
What is happening is that when I am submitting my form all the data sends through correctly. I'm getting the image name, but nothing else with it. My content type is set correctly, and everything else is set correctly, so I"m just confused as to what is going on and could use a little insight.
All my code is 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 an 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 all the required fields are filled out. Fields marked with an asterisk are required.";
}else{
$body = "You have a new message from $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 from $name";
$MAIL->Body = $body;
if(!$MAIL->Send()){
$response['errors'] = true;
$response['success'] = "There was an error when trying to send you 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 the PHPMailer documentation and double checked what I have written out here, but nothing seems to be giving me the proper solution.
Edit:
Setting the content-Type : 'multipart/form-data'
provides a PHP error of Undefined index: callback
. Setting enctype on the form provides the identical error. Setting it to false sends the message but I have no attachment.
Also, when looking through the devloper tools, when I check the response tab, I get this when I run print_r($_FILES['images']
Array
(
[name] => 20150625_140017.jpg
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)