December 24, 20205 yr Hi there, I have created a webform, which when submitted sends to email. I have included ajax to handle the notifications. But it's not quite working when a file is attached. It sends to email fine when it's just text If I include the form handler contactForm.php directly in the html page with the form, it sends the form attachment fine. If I include a console.log(file) just above the ajax, it does display the file data Below is the code in question: <form enctype="multipart/form-data" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <input class="file-input" id="fileUpload" name="attachment" type="file"> $('form').submit(function(e) { e.preventDefault(); var formData = new FormData(this); if($('#fileUpload').prop('files').length > 0) { file =$('#fileUpload').prop('files')[0]; formData.append("attachment", file); } console.log(file); $.ajax({ type: "POST", url: '/_partial/contactForm.php', data: formData, success: function(response){ var jsonData = JSON.parse(response); if (jsonData.success == "1") { $('.notification').fadeIn(function(){ $(this).addClass("is-success"); $(this).text("Thank you for your interest. We will get back to you soon."); $(this).delay(5000).fadeOut(); }); } else { $('.notification').fadeIn(function(){ $(this).addClass("is-danger"); $(this).text("Something went wrong. Please try again later"); $(this).delay(5000).fadeOut(); }); } }, cache: false, processData: false, contentType: false, }); $(this).trigger("reset"); }); Please let me know if anything looks suspect!
December 24, 20205 yr Author Below is the php form handler if this helps at all: <?php $name = $phone = $package = $beatQuote = $uploadedFile = $message = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $phone = test_input($_POST["phone"]); $package = test_input($_POST["package"]); $quote = $_POST["quote"]; $message = $_POST["message"]; $uploadStatus = 1; if(!empty($_FILES["attachment"]["name"])){ $targetDir = "uploads/"; $fileName = basename($_FILES["attachment"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); $allowTypes = array('pdf', 'doc', 'docx'); if(in_array($fileType, $allowTypes)){ if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){ $uploadedFile = $targetFilePath; }else{ $uploadStatus = 0; echo json_encode(array('success' => 0)); } }else{ $uploadStatus = 0; echo json_encode(array('success' => 0)); } } if ($uploadStatus = 1) { $toEmail = 'email@address.co.uk'; $emailSubject = 'Contact Page Enquiry'; $headers = 'From: Contact Page <noreply>'; $bodyParagraphs = '<h2>Contact Request Submitted</h2> <p><b>Name:</b> '.$name.'</p> <p><b>Phone:</b> '.$phone.'</p> <p><b>Package Of Interest:</b> '.$package.'</p> <p><b>Customer Ticked Beat Quote Box:</b> '.$beatQuote.'</p> <p>'.$message.'</p>'; if (!empty($uploadedFile) && file_exists($uploadedFile)){ $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $bodyParagraphs . "\n\n"; if(is_file($uploadedFile)){ $message .= "--{$mime_boundary}\n"; $fp = @fopen($uploadedFile,"rb"); $data = @fread($fp,filesize($uploadedFile)); @fclose($fp); $data = chunk_split(base64_encode($data)); $message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" . "Content-Description: ".basename($uploadedFile)."\n" . "Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; } $message .= "--{$mime_boundary}--"; $returnpath = "-f" . $email; $mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath); @unlink($uploadedFile); } else { $headers .= "\r\n". "MIME-Version: 1.0"; $headers .= "\r\n". "Content-type:text/html;charset=UTF-8"; $mail = mail($toEmail, $emailSubject, $bodyParagraphs, $headers); } if ($mail) { echo json_encode(array('success' => 1)); } else { echo json_encode(array('success' => 0)); } } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
Create an account or sign in to comment