我有一个插件,我正在使用发送电子邮件的PHP表单。表单中的所有内容都是发送我需要发送的值,但是有一个文件附件区域,用户可以在其中附加多个文件。
html表单(通过插件中的echo>)
function html_form_code() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<div class="row">';
echo '<div class="col-xs-12 col-md-4">';
echo '<input class="form-control" type="text" placeholder="Name" label="Name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '<div class="col-xs-12 col-md-4">';
echo '<input class="form-control" type="text" placeholder="company" label="company" name="cf-company" value="' . ( isset( $_POST["cf-company"] ) ? esc_attr( $_POST["cf-company"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '<div class="col-xs-12 col-md-4">';
echo '<input class="form-control" type="text" placeholder="phone" label="phone" name="cf-phone" pattern="[0-9 ]+" value="' . ( isset( $_POST["cf-phone"] ) ? esc_attr( $_POST["cf-phone"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';
echo '<div class="row">';
echo '<div class="col-xs-12">';
echo '<input class="form-control" type="text" placeholder="email" label="email" name="cf-email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';
echo '<div class="row">';
echo '<div class="col-xs-12">';
echo '<label for="formDeliverables[]">Select the countries that you have visited:</label><br>';
echo '<select name="formDeliverables[]">';
echo '<option value="New Website">New Website</option>';
echo '<option value="New Web Features">New Web Features</option>';
echo '<option value="Website Stabilization">Website Stabilization</option>';
echo '<option value="3rd Party Email Integration">3rd Party Email Integration</option>';
echo '<option value="Web Virus Removal">Web Virus Removal</option>';
echo '<option value="Web Security Review">Web Security Review</option>';
echo '</select>';
echo '</div>';
echo '</div>';
echo '<div class="row checkboxes">';
echo '<div class="col-sm-12 col-md-6">';
echo '<label>Copy Writing:</label><br>';
echo '<input type="checkbox" name="copy[]" value="Copy provided" size="40" /><label for="Copy provided" name="check" class="checks">I will provide copy.</label>';
echo '<input type="checkbox" name="copy[]" value="Copy needed" size="40" /><label for="Copy needed" name="check" class="checks">I need copy.</label>';
echo '<input type="checkbox" name="copy[]" value="Not sure about copy" size="40" /><label for="Not sure about copy" name="check" class="checks">I\'m not sure.</label>';
echo '</div>';
echo '<div class="col-sm-12 col-md-6">';
echo '<label>Design Services:</label><br>';
echo '<input type="checkbox" name="design[]" value="Design provided" size="40" /><label for="Design provided" name="check" class="checks">I will need design services.</label>';
echo '<input type="checkbox" name="design[]" value="Design needed" size="40" /><label for="Design needed" name="check" class="checks">I will not need design services.</label>';
echo '<input type="checkbox" name="design[]" value="Not sure about design" size="40" /><label for="Not sure about design" name="check" class="checks">I\'m not sure.</label>';
echo '</div>';
echo '<div class="col-sm-12 col-md-6">';
echo '<label>Illustration:</label><br>';
echo '<input type="checkbox" name="illustration[]" value="Illustration needed" size="40" /><label for="Illustration needed" name="check" class="checks">I need illustration services.</label>';
echo '<input type="checkbox" name="illustration[]" value="Illustration not needed" size="40" /><label for="Illustration not needed" name="check" class="checks">I will not need illustration services.</label>';
echo '<input type="checkbox" name="illustration[]" value="Not sure about illustration" size="40" /><label for="Not sure about illustration" name="check" class="checks">I\'m not sure.</label>';
echo '</div>';
echo '<div class="col-sm-12 col-md-6">';
echo '<label>Photography:</label><br>';
echo '<input type="checkbox" name="photography[]" value="photography needed" size="40" /><label for="photography needed" name="check" class="checks">I need photography services.</label>';
echo '<input type="checkbox" name="photography[]" value="photography not needed" size="40" /><label for="photography not needed" name="check" class="checks">I will not need photography services.</label>';
echo '<input type="checkbox" name="photography[]" value="Not sure about photography" size="40" /><label for="Not sure about photography" name="check" class="checks">I\'m not sure.</label>';
echo '</div>';
echo '</div>';
echo '<div class="container">';
echo '<div class="row">';
echo '<label class="control-label">Timelines:</label>';
echo '</div>';
echo '<div class="row">';
echo '<div class="col-sm-6">';
echo '<input class="form-control datepicker" id="date" name="date_from" placeholder="Select the start date" type="text"/>';
echo '</div>';
echo '<div class="col-sm-6">';
echo '<input class="form-control datepicker" id="date_end" name="date_end" placeholder="Select the end date" type="text"/>';
echo '</div>';
echo '</div>';
echo '<div class="container">';
echo '<div class="row budget">';
echo '<div class="col-xs-12">';
echo '<label for="price">Budget:</label>';
echo '<div class="double-point-slide">';
echo '<div id="slider-3"></div>';
echo '<input type="text" id="price" name="price" style="border:0; color:#000; font-weight:bold;">';
echo '<input type="text" id="price-two" name="price-two" style="border:0; color:#000; font-weight:bold;">';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="container table">';
echo '<div class="row description">';
echo '<div class="col-xs"> ';
echo '<textarea id="projectDescription" placeholder="Describe Your Project" name="cf-message"></textarea>';
echo '</div>';
echo '</div>';
echo '<div class="row add-attachments">';
echo '<div class="col-xs text-right" >';
echo '<label><input id="file" type="file" name="file[]" multiple="true"><span>+ Add Inspiration Image</span></label>';
echo '</div>';
echo '</div>';
echo '<div class="row attachments" style="display:none">';
echo '<div class="col-xs">';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col-xs-12">';
echo '<input type="submit" class="final" name="cf-submitted" value="Submit"/>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</form>';
}
这是发送邮件功能。
function deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$phone = "Phone: " . sanitize_text_field( $_POST["cf-phone"] );
$company = "Company: " . sanitize_text_field( $_POST["cf-company"] );
$deliverablesArr = $_POST["formDeliverables"];
$deliverables = "Deliverables: " . implode( $deliverablesArr);
$message = "Message: " . esc_textarea( $_POST["cf-message"] );
$date_from = $_POST['date_from'];
$date_end = $_POST['date_end'];
$date_span = "Timeline: " . $date_from . " - " . $date_end;
$price = $_POST['price'];
$price_two = $_POST['price-two'];
$budget = $price . " - " . $price_two;
$subject = "Someone needs " . $deliverables . "from a-three.cc!";
$copy = 'None';
if(isset($_POST['copy']) && is_array($_POST['copy']) && count($_POST['copy']) > 0){
$copy = implode(', ', $_POST['copy']);
}
$design = 'None';
if(isset($_POST['design']) && is_array($_POST['design']) && count($_POST['design']) > 0){
$design = implode(', ', $_POST['design']);
}
$illustration = 'None';
if(isset($_POST['illustration']) && is_array($_POST['illustration']) && count($_POST['illustration']) > 0){
$illustration = implode(', ', $_POST['illustration']);
}
$photography = 'None';
if(isset($_POST['photography']) && is_array($_POST['photography']) && count($_POST['photography']) > 0){
$photography = implode(', ', $_POST['photography']);
}
// get the blog administrator's email address
$to = get_option( 'admin_email' );
$body = '<html><body>';
$body .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$body .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $name . "</td></tr>";
$body .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
$body .= "<tr><td><strong>Company:</strong> </td><td>" . $company . "</td></tr>";
$body .= "<tr><td><strong>Phone:</strong> </td><td>" . $phone . "</td></tr>";
$body .= "<tr><td><strong>Deliverables:</strong> </td><td>" . $deliverables . "</td></tr>";
$body .= "<tr><td style='font-size: 16px'><strong>Services Needed</strong> </td><td></td></tr>";
$body .= "<tr><td><strong>Copy:</strong> </td><td>" . $copy . "</td></tr>";
$body .= "<tr><td><strong>Design:</strong> </td><td>" . $design . "</td></tr>";
$body .= "<tr><td><strong>Illustration:</strong> </td><td>" . $illustration . "</td></tr>";
$body .= "<tr><td><strong>Photography:</strong> </td><td>" . $photography . "</td></tr>";
$body .= "<tr><td style='font-size: 16px'><strong>Timeline</strong> </td><td></td></tr>";
$body .= "<tr><td><strong>Timeline:</strong> </td><td>" . $date_span . "</td></tr>";
$body .= "<tr><td style='font-size: 16px'><strong>Budget</strong> </td><td></td></tr>";
$body .= "<tr><td><strong>Budget:</strong> </td><td>" . $budget . "</td></tr>";
$body .= "<tr><td style='font-size: 16px'><strong>Project Description</strong> </td><td></td></tr>";
$body .= "<tr><td><strong>Project Description:</strong> </td><td>" . $message . "</td></tr>";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $body ) ) {
echo '<div>';
echo '<p>Thanks for contacting us, expect a response soon.</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}
}
我已经拥有&#34;文件[]&#34;作为该字段的名称,因为我假设我需要将每个文件添加到数组中,然后以可以解释的方式将其吐出到电子邮件中。我已经尝试了很多东西,但不能使这个工作完成。
如何在wordpress中将多个文件附加到php表单以便通过电子邮件发送?
答案 0 :(得分:0)
你错过了最后一个参数$attachments
,它可以是一个数组。 您指定要使用 file[]
但我在输入名称中看不到它。
$attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );
wp_mail( $to, $subject, $message, $headers, $attachments );
的示例
您可以使用函数move_uploaded_file()
复制服务器上的文件,并在邮件发送后立即unlink()
。
答案 1 :(得分:0)
所以我最终解决了它。感谢@Benoti带领我朝着正确的方向前进。这是在上传过程中添加了安全性的最终产品。
//file upload function
$total = count($_FILES['upload']['name']);
$attachments = "";
$attachmentString = "";
$attachmentSet = false;
//variables for validation
$maxsize = 2097152; //just over 2mb
$whitelist = array(
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
);
if(file_exists($_FILES['upload']['tmp_name'][0]) || is_uploaded_file($_FILES['upload']['tmp_name'][0])) {
//hey look the attachment is set!
$attachmentSet = True;
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
$uploaddir = 'var/www/uploadsfolderpath/'#changed for security
wp_mkdir_p( $uploaddir );
//rename file
$orig_name = $_FILES["upload"]["name"][$i];
$temp = explode(".", $orig_name);
//give name with original name added by user plus hash random value
$newfilename = array_values($temp)[0] . md5(array_values($temp)[0]) . '.' . end($temp);
//variables per each iteration
//Setup our new file path
$newFilePath = $uploaddir . $newfilename; //renamed file added to new file path
$file_type = $_FILES['upload']['type'][$i]; //returns the mimetype
$imageinfo = getimagesize($_FILES['upload']['tmp_name'][$i]);//returns size of image
$fileName = strtolower($_FILES['upload']['name'][$i]);
$upload_extension = explode(".", $fileName);
$upload_extension = end($upload_extension);//gets just the extension
#check for appropriate size with php.ini
$POST_MAX_SIZE = ini_get('post_max_size');
$mul = substr($POST_MAX_SIZE, -1);
$mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
if ($_SERVER['CONTENT_LENGTH'] > $mul*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) $error = true;
$max_file_size_in_bytes = 2140907; // 2MB in bytes
if(count($errors) === 0)
{
#restrict the limit
$file_size = @filesize($tmpFilePath);
if (!$file_size || $file_size > $max_file_size_in_bytes) {
$errors[] = 'File too large. File must be less than 2 GB.';
}
}
else
{
$errors[] = 'File too large. File must be less than 2 GB.';
}
//check if isn't a type and if has an image size
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/png' && $imageinfo['mime'] != 'image/jpeg' && isset($imageinfo))
{
$errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.';
}
//file type verficatio
if(!in_array($file_type, $whitelist) && !empty($_FILES["upload"]["type"])) {
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
//all conditions passed, upload files!
if(count($errors) === 0) {
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
chmod($newFilePath, 0644);
$attachments[$i] = $newFilePath;
//make string for sending to email
$attachmentString[$i] = $newFilePath;
$headers .= "Content-Disposition: attachment; filename = \"" . $attachmentString[$i] . "\"\n\n";
}
}
}
}//end of loop
}
// If email has been process for sending, display a success message
if(count($errors) === 0) {
if ( wp_mail( $to, $subject, $message, $headers, $attachments ) ) {
echo '<div class="success"><p>Success! Thanks for contacting us, expect a response soon.</p></div>';
if ($attachmentSet) {
//clean up your temp files after sending
foreach($attachments as $att) {
@unlink($att);
};
}
}
}
else {
echo '<div class="error">';
foreach($errors as $error) {
echo '<p>' .$error . '</p>';
}
echo '</div>';
}
}
}
这是文件上传部分(仍然是相同的邮件功能)。
FAIL __tests__/components/MyComponent.test.js
● Test suite failed to run
ReferenceError: define is not defined
at Object.<anonymous> (node_modules/private-npm-module/utils.js:1:90)
建议欢迎,因为这是我第一次用php构建这样的东西:)