PHP邮件表单 - 只有一个变量正在提交

时间:2017-08-01 19:28:51

标签: php jquery ajax email

我正在尝试设置PHP邮件表单。我从this guide开始,一直在修改内容。目前,我收到要提交的表单,我收到了电子邮件,但只有文本区域中的注释填充了电子邮件正文。

我的PHP代码:

<?php
$recipient_email    = "my@email.com"; //recepient
$from_email         = "from@email.com"; //from email using site domain.


if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
	die('Sorry, request must be Ajax POST'); //exit script
}

if($_POST){
    
    $sender_name 	= filter_var($_POST["name"], FILTER_SANITIZE_STRING); //capture sender name
    $sender_email 	= filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
    $phone_number   = filter_var($_POST["telephone"], FILTER_SANITIZE_NUMBER_INT);
    $subject        = "Contact Form Entry";
    $message 		= filter_var($_POST["comments"], FILTER_SANITIZE_STRING); //capture message

    $attachments = $_FILES['file_attach'];
	
    //php validation
    if(strlen($sender_name)<4){ // If length is less than 4 it will output JSON error.
        print json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
        exit;
    }
    if(!filter_var($sender_email, FILTER_VALIDATE_EMAIL)){ //email validation
        print json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
        exit;
    }
    if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
        print json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
        exit;
    }
    if(strlen($message)<3){ //check emtpy message
        print json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
        exit;
    }

    
    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5("whatever"); 
            
    if($file_count > 0){ //if attachment exists
        //header
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from_email."\r\n"; 
        $headers .= "Reply-To: ".$sender_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
        
        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($message)); 

        //attachments
        for ($x = 0; $x < $file_count; $x++){       
            if(!empty($attachments['name'][$x])){
                
                if($attachments['error'][$x]>0) //exit script and output error if we encounter any
                {
                    $mymsg = array( 
                    1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
                    2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
                    3=>"The uploaded file was only partially uploaded", 
                    4=>"No file was uploaded", 
                    6=>"Missing a temporary folder" ); 
                    print  json_encode( array('type'=>'error',$mymsg[$attachments['error'][$x]]) ); 
					exit;
                }
                
                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];
                
                //read file 
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
                
                $body .= "--$boundary\r\n";
                $body .="Content-Type: $file_type; name=".$file_name."\r\n";
                $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
                $body .="Content-Transfer-Encoding: base64\r\n";
                $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
                $body .= $encoded_content; 
            }
        }

    }else{ //send plain email otherwise
       $headers = "From:".$from_email."\r\n".
        "Reply-To: ".$sender_email. "\n" .
        "X-Mailer: PHP/" . phpversion();

        $body = "Name: " . $sender_name . "\r\n";
        $body .= "Email: " . $sender_email . "\r\n";
        $body .= "Comments: " . $message . "\r\n";
        $body .= "Telephone: ".$phone_number . "\r\n";
    }
        
    $sentMail = mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {       
        print json_encode(array('type'=>'done', 'text' => 'Thank you for your email'));
		exit;
    }else{
        print json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));  
		exit;
    }
}
?>

我一直在测试它,大多没有图像附件,因此代码应该跳转到} else {//发送普通电子邮件部分。

$body = "Name: " . $sender_name . "\r\n";
$body .= "Email: " . $sender_email . "\r\n";
$body .= "Comments: " . $message . "\r\n";
$body .= "Telephone: ".$phone_number . "\r\n";

包含此代码后,我想知道为什么所有这些行都没有连接到body变量,并且在mail()函数被触发时发送所有这些信息。我收到的电子邮件只包含表格中textarea写的内容。有什么见解吗?

1 个答案:

答案 0 :(得分:0)

由于循环中的“附加附件”代码正在检查空文件名,因此在$file_count语句中也可以安全地执行此操作。

更改

$file_count = count($attachments['name']);

$file_count = count(array_filter($attachments['name']));

因此只计算非空文件名。