Я пробовал, казалось бы, каждый StackOverflow, который я мог найти по этой конкретной теме (и их много), но большинство забывают enctype
в теге формы или что-то в этом роде. Я по-прежнему получаю отправленные электронные письма, но без прикрепленных файлов.
Мой массив $_FILES
не заполняется при отправке формы. Приведенный ниже код отправляет электронное письмо html с данными отправленных полей, но без вложений.
Это самая последняя ошибка в журнале, когда форма отправляется с входными данными, но без вложений:
[24-Jan-2020 22:06:33 UTC] PHP Warning: count(): Parameter must be an array or an object that implements Countable in *******/public_html/rtform/contact.php on line 89
Я указал строку 89 в приведенном ниже коде.
Вот начало и конец моей формы, с моим вводом файла внизу (остальные формы слишком длинная, чтобы включить все здесь:
<form id="contact-form" method="POST" action="contact.php" role="form" enctype="multipart/form-data">
<div class="controls">
<!-- the middle of the form here -->
<div class="row">
<h3 class="form_title">Upload Additional Supporting Documents</h3>
<div class="col-lg-12">
<label for="attachments[]">Select one or more files:
<input name="attachments[]" type="file" multiple="multiple">
</label>
</div>
</div>
<hr/>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="**************************************" data-callback="verifyRecaptchaCallback" data-expired-callback="expiredRecaptchaCallback"></div>
<input class="form-control d-none" data-recaptcha="true" required data-error="Please complete the Captcha">
<div class="help-block with-errors"></div>
</div>
<p><span class="red">Fields marked with * denotes a required field.</span></p>
<input type="submit" class="btn btn-success btn-send" value="Submit Order">
<div class="messages"></div>
</div><!-- end .controls -->
</form>
Мой файл обработки формы PHP файл длинный, но, вероятно, необходимо включить все, поэтому:
<?php
// error_reporting(E_ALL); ini_set('display_errors', 1);
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'includes/phpmailer/src/Exception.php';
require 'includes/phpmailer/src/PHPMailer.php';
// Require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// ReCaptch Secret
$recaptchaSecret = *******************************;
/*******************************************************
*
* Here I have a long array with all of the input names and variables which I use for
* the logic below for my html email template(which works great). Shouldn't be necessary
* for this problem, so I'm excluding it.
*
*******************************************************/
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
if ( !empty($_POST) ) {
$msg = ''; // To be used for dynamic messaging
// ReCaptcha
// Validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
$requestorEmail = "";
//Make sure the address they provided is valid before trying to use it
if (array_key_exists('requestoremail', $_POST) && PHPMailer::validateAddress($_POST['requestoremail'])) {
$requestorEmail = $_POST['requestoremail'];
} else {
$msg = 'Error: invalid requestor email address provided';
throw new \Exception('Requestor email, ' . $requestorEmail . ', is not a valid email address.');
}
//Recipients
$mail->setFrom('senderemail@email.com', 'Sender');
$mail->addAddress('recipientemail@email.com', 'Recipient'); // Add a recipient
$mail->addAddress($requestorEmail, 'Requestor'); // Name is optional
$mail->addReplyTo('replytoemail@email.com', 'Reply To Email');
/*************************************
*
* The attachment-related stuff:
*
*************************************/
$maxSize = 2 * 1024 * 1024; // 2 MB
$fileTypes = array('text/plain', 'application/pdf', 'application/msword', 'application/rtf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'); // allowed mime-types
// Passing these variables to my jQuery ajax request at the bottom of this file to try to get some helpful info
$theFiles = $_FILES;
$filesCount = count($_FILES['attachments']['tmp_name']);
if (isset($_FILES)) {
//Attach multiple files one by one
//******************** The line below is line 89 ********************/
for ($ct = 0; $ct < count($_FILES['attachments']['tmp_name']); $ct++) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachments']['name'][$ct]));
$filename = $_FILES['attachments']['name'][$ct];
$filesize = $_FILES['attachments']['size'][$ct];
$filemime = mime_content_type($uploadfile);
// Check $_FILES['upfile']['error'] value.
switch ($_FILES['attachments']['error'][$ct]) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
try {
if (in_array($filemime, $fileTypes)){
if ($filesize <= $maxSize) {
if (move_uploaded_file($_FILES['attachments']['tmp_name'][$ct], $uploadfile)) {
$mail->addAttachment($uploadfile, $filename);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
throw new \Exception('Failed to move file to ' . $uploadfile);
}
}
else {
$msg = "File[s] are too large. They must each be less than or equal to 2MB.";
throw new \Exception('File[s] are too large. They must each be less than or equal to 2MB.');
}
}
else {
$msg = "File[s] must only be of these types: .txt, .rtf, .pdf, .doc, .docx. Please try again.";
throw new \Exception('File[s] must only be of these types: .txt, .rtf, .pdf, .doc, .docx. Please try again.');
}
}
catch (Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $msg); // or $e->getMessage()
}
}
}
else {
$msg = "No file added.";
throw new \Exception('No file added.');
}
// Get template file contents
$template = file_get_contents("email-template.html");
/*************************************************************************
*
* This is constructing my email html template with input data
*
**************************************************************************/
// Loop $_POST content, compare to $fields array, and include/replace if not empty
foreach($_POST as $key => $value) {
// Create a generic row to use for each
$value_row = "<li>$fields[$key]: $value</li>";
if ( !empty( $value ) ) {
// Check each group's first member and insert/replace appropriate title string
if( $key == "casenumber" ) {
$value_row = "<h2>General Information</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "client-fullname" ) {
$value_row = "</ul><h2>Client/Applicant</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "employer-fullname" ) {
$value_row = "</ul><h2>Employer/Insured</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "requestor-attorney" ) {
$value_row = "</ul><h2>Requestor</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "billingcarrier" ) {
$value_row = "</ul><h2>Billing Information</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "caseplantiff" ) {
$value_row = "</ul><h2>Case Caption</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "oclattorneyname" ) {
$value_row = "</ul><h2>Opposing Counsel</h2>\n<ul style='list-style:none;'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location1facility" ) {
$value_row = "</ul><h2>Delivery Instructions</h2><h3>Location 1</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location2facility" ) {
$value_row = "</ul><h3>Location 2</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location3facility" ) {
$value_row = "</ul><h3>Location 3</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location4facility" ) {
$value_row = "</ul><h3>Location 4</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location5facility" ) {
$value_row = "</ul><h3>Location 5</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location6facility" ) {
$value_row = "</ul><h3>Location 6</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location7facility" ) {
$value_row = "</ul><h3>Location 7</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location8facility" ) {
$value_row = "</ul><h3>Location 8</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location9facility" ) {
$value_row = "</ul><h3>Location 9</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
elseif( $key == "location10facility" ) {
$value_row = "</ul><h3>Location 10</h3>\n<ul style='list-style:none;' class='location-list'><li>$fields[$key]: $value</li>";
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
else {
$template = str_replace('{{ '.$key.' }}', $value_row, $template);
}
}
else {
$template = str_replace('{{ '.$key.' }}', '', $template);
}
}
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Order Form Submission from Real Time Records Website';
$mail->Body = $template;
$mail->send();
// Generic success message
$msg = 'Your order has been received successfully. We will contact you shortly. Please contact us if you have any questions.';
$responseArray = array('type' => 'success', 'message' => $msg, 'files' => json_encode($theFiles), 'number-of-files' => "$filesCount");
} // end if( !empty( $_POST ) )
} catch (Exception $e) {
// Generic error message
$msg = 'There was an error while submitting the form. Please try again later';
$responseArray = array('type' => 'danger', 'message' => $msg); // or $e->getMessage()
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
// This encodes the output message(with some of those attachment details) for the jQuery ajax request to pickup
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
Мой jQuery на самом деле просто отображает переданное сообщение об ошибке или успехе, хотя я передал ему некоторую информацию из $_FILES
, чтобы посмотреть, заполняется ли она, а это не так.
Когда ajax запрос сделан, полученные данные, которые я вижу в ответе, связанном с вложениями:
files = '[]', 'number-of-files' = '0'
Может кто-нибудь с некоторым опытом PHPMailer увидеть, что я делаю неправильно?
РЕДАКТИРОВАТЬ x2
Вот мой ты pdated jQuery, согласно ответу Synchro ниже. Теперь он получает файлы из формы и правильно добавляет несколько файлов в объект FormData, но объект php $_FILES
учитывает / показывает только один файл (если их два) и не прикрепляет их к отправленной электронной почте. :
$(function () {
window.verifyRecaptchaCallback = function (response) {
$('input[data-recaptcha]').val(response).trigger('change');
}
window.expiredRecaptchaCallback = function () {
$('input[data-recaptcha]').val("").trigger('change');
}
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
const theForm = document.getElementById('contact-form');
var fd = new FormData(theForm);
var files = $("#file_attachments").prop('files');
for (var i = 0; i < files.length; i++) {
fd.append("attachments", files[i]);
console.log("File #"+(i+1)+" attached: "+files[i]);
}
$.ajax({
type: "POST",
url: url,
data: fd,
processData: false,
contentType: false,
success: function(data) {
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var messageFiles = data.files;
var messageFilesCount = data.numberOfFiles
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div><div>Number of Files: ' + messageFilesCount + ' - ' + messageFiles + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
grecaptcha.reset();
}
},
error: function(data) {
//this is going to happen when you send something different from a 200 OK HTTP
alert('Ooops, something happened: ' + data.message);
}
});
return false;
}
})
});