【发布时间】:2016-11-21 20:08:21
【问题描述】:
我有一个插件,我正在使用一个发送电子邮件的 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';
}
}
}
我已经将“file[]”作为该字段的名称,因为我假设我需要将每个文件添加到一个数组中,然后以一种可以解释的方式将其发送到电子邮件中。我已经尝试了很多东西,但不能完全做到这一点。
如何将多个文件附加到 wordpress 中的 php 表单以发送电子邮件?
【问题讨论】:
-
你能澄清一下问题是什么吗?
-
希望我澄清一下。