通过php获取表单数据以写入cvs然后作为附件发送到电子邮件

时间:2014-10-08 06:53:44

标签: php forms email csv wordpress-plugin

嘿所有它已经有一段时间了,因为我一直在这里,但我只是想有人证明阅读此代码。我正在为客户端制作一个wordpress插件。它的目的是从表单中获取数据,然后以CSV格式输入,然后将csv通过电子邮件发送给管理员用户作为附件。

我得到几声尖叫,我一直盯着它看,欢迎一双新鲜的眼睛。

一如既往,提前谢谢。

function html_form_code() {
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<p >   <small style="color:red;" >All Fields Required</small>';
    echo 'Your Name  <br/>';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>'
    echo 'Your Address (I.E. 123 Abc Street) <br/>';
    echo '<input type="text" name="cf-address" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-address"] ) ? esc_attr( $_POST["cf-address"] ) : '' ) . '" size="60" />'; 
    echo '</p>';
    echo '<p>';
    echo '<p>'
    echo 'Your City <br/>';
    echo '<input type="text" name="cf-city" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-city"] ) ? esc_attr( $_POST["cf-city"] ) : '' ) . '" size="60" />';  
    echo '</p>';
    echo '<p>';
    echo 'Your State (I.E.  MO, IL, etc.)  <br/>';
    echo '<input type="text" name="cf-state" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-state"] ) ? esc_attr( $_POST["cf-state"] ) : '' ) . '" size="2" />';   
    echo '</p>';
     echo '<p>';
    echo 'Your Zip Code (I.E. 12345)  <br/>';
    echo '<input type="text" name="cf-zip" pattern="[0-9 ]+" value="' . ( isset( $_POST["cf-zip"] ) ? esc_attr( $_POST["cf-zip"] ) : '' ) . '" size="5" />';    
    echo '</p>';
    echo '<p>';
    echo 'Your Phone (I.E.  MO, IL, etc.)  <br/>';
    echo '<input type="text" name="cf-phone" pattern="[0-9]+" value="' . ( isset( $_POST["cf-phone"] ) ? esc_attr( $_POST["cf-phone"] ) : '' ) . '" size="10" />';  
    echo '</p>';
    echo '<p>';
    echo 'Your Email  <br/>';
    echo '<input type="email" name="cf-email"  value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo '<p>';
    echo ' Vehicle year  <br/>';
    echo '<input type="text" name="cf-year" pattern="[0-9 ]+" value="' . ( isset( $_POST["cf-year"] ) ? esc_attr( $_POST["cf-year"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo 'Make  <br/>';
    echo '<input type="text" name="cf-make" pattern="[a-zA-Z0-9 ]+"value="' . ( isset( $_POST["cf-make"] ) ? esc_attr( $_POST["cf-make"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Model  <br/>';
    echo '<input type="text" name="cf-model" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-model"] ) ? esc_attr( $_POST["cf-model"] ) : '' ) . '" size="40" />';
    echo '</p>';    
    echo '<p>';
    echo 'color  <br/>';
    echo '<input type="text" name="cf-color" pattern="[a-zA-Z]+" value="' . ( isset( $_POST["cf-color"] ) ? esc_attr( $_POST["cf-color"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo 'License Plate<br/>';
    echo '<input type="text" name="cf-license" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-license"] ) ? esc_attr( $_POST["cf-license"] ) : '' ) . '" size="40" />';
    echo '</p>';    
    echo '<p>';
    echo 'Services Needed <br/>';
    echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
    echo '</p>';
    echo '<p><input type="submit" name="cf-submitted" value="Send"></p>';
    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"] );
        $address = sanitize_text_field( $_POST["cf-address"] );
        $city    = sanitize_text_field( $_POST["cf-city"] );
        $state   = sanitize_text_field( $_POST["cf-state"] );
        $zip     = sanitize_text_field( $_POST["cf-zip"] );
        $phone   = sanitize_text_field( $_POST["cf-phone"] );   
        $email   = sanitize_email( $_POST["cf-email"] );
        $year    = sanitize_text_field( $_POST["cf-year"] );
        $make    = sanitize_text_field( $_POST["cf-make"] );
        $model   = sanitize_text_field( $_POST["cf-model"] );
        $color   = sanitize_text_field( $_POST["cf-color"] );
        $licence = sanitize_text_field( $_POST["cf-license"] );     
        $service = sanitize_text_field( $_POST["cf-service"] );     
        $message = esc_textarea( $_POST["cf-message"] );

 //this is where the creating of the csv takes place
$cvsData = $name . "," . $address . "," . $city . "," . $state . "," . 
            $zip  $phone . "," . $email . "," . $year . "," . $make . 
            "," . $model . "," . $color . "," .  $licence . "," .   
            $service  . "," .   $message "\n";

 $file = fopen("dropoffRequests.csv","a"); // $fp is now the file pointer to file $filename

 if($file){
fwrite($file,$cvsData); // Write information to the file
fclose($file); // Close the file
  // get the administrator's email address
        $replyto = get_option( 'admin_email' );
 $content = chunk_split(base64_encode($content));
 $uid = md5(uniqid(time()));
                $header = "From: ".$from_name." <".$from_mail.">\r\n";
                $header .= "Reply-To: ".$replyto."\r\n";
                $header .= "MIME-Version: 1.0\r\n";
                $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                $header .= "This is a multi-part message in MIME format.\r\n";
                $header .= "--".$uid."\r\n";
                $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
                $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
                $header .= $message."\r\n\r\n";
                $header .= "--".$uid."\r\n";
                $header .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n"; 
                // use different content types here
                $header .= "Content-Transfer-Encoding: base64\r\n";
                $header .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
                $header .= $content."\r\n\r\n";
                $header .= "--".$uid."--";       


        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for getting in contact with us! We will get back to you shortly..</p>';
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
        }
    }
}

function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();

    return ob_get_clean();
}

add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );

?>

1 个答案:

答案 0 :(得分:0)

请您描述一下哪些不适合您?如果我们不必先找到问题,我相信我们可以找到解决办法; - )

顺便说一下。这可能会使事情变得复杂,但只是您的deliver_mail函数的替代方案。 (至少它的开头):

    if ( isset( $_POST['cf-submitted'] ) ) {
$wantedIndex = array(
    'cf-name' => sanitize_text_field,
    'cf-address' => sanitize_text_field,
    'cf-city' => sanitize_text_field,
    'cf-state' => sanitize_text_field,
    'cf-zip' => sanitize_text_field,
    'cf-phone' => sanitize_text_field,
    'cf-email' => sanitize_email,
    'cf-year' => sanitize_text_field,
    'cf-make' => sanitize_text_field,
    'cf-model' => sanitize_text_field,
    'cf-color' => sanitize_text_field,
    'cf-license' => sanitize_text_field,
    'cf-service' => sanitize_text_field,
    'cf-message' => esc_textarea
);
$csvArray = array();
foreach( $wantedIndex as $key => $value )
{
    $csvArray[ str_replace( 'cf-', '', $key) ] = '\\"' . $value( $_POST[ $key ] ) . '\\"';
}
$csvData = implode( ',', $csvArray ) . "\n";