duanfu3884 2017-09-29 05:46
浏览 75
已采纳

联系表格:无法发送电话号码

I got this script online and it seems to work. However, the form doesn't seem to process name, phone1, phone2.

I don't see any process method where name, phone1, phone2 is being processed and send either.

Could someone help to get the name, phone1 and phone2 to be sent as well in the mail?

The form code has the correct fields and Chrome console shows that data is sent. What do you think is missing?

Here's the PHP script:

$recipient_email    = "myemail@gmail.com"; //recepient
$from_email         = "myemail@gmail.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
    $country_code   = filter_var($_POST["phone1"], FILTER_SANITIZE_NUMBER_INT);
    $phone_number   = filter_var($_POST["phone2"], FILTER_SANITIZE_NUMBER_INT);
    $subject        = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
    $message        = filter_var($_POST["message"], 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($country_code, FILTER_VALIDATE_INT)){ //check for valid numbers in country code field
        $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in country code'));
        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($subject)<3){ //check emtpy subject
        print json_encode(array('type'=>'error', 'text' => 'Subject is required'));
        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("sanwebe.com"); 

    if($file_count > 0){ //if attachment exists
        //header
        $headers = "MIME-Version: 1.0
"; 
        $headers .= "From:".$from_email."
"; 
        $headers .= "Reply-To: ".$sender_email."" . "
";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary

"; 

        //message text
        $body = "--$boundary
";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1
";
        $body .= "Content-Transfer-Encoding: base64

"; 
        $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
";
                $body .="Content-Type: $file_type; name=".$file_name."
";
                $body .="Content-Disposition: attachment; filename=".$file_name."
";
                $body .="Content-Transfer-Encoding: base64
";
                $body .="X-Attachment-Id: ".rand(1000,99999)."

"; 
                $body .= $encoded_content; 
            }
        }

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

    $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;
    }
}

PS: The developer who wrote this open-source script isn't responding.

  • 写回答

2条回答 默认 最新

  • douxuanjie2692 2017-09-29 06:30
    关注

    Yes, you're right, apparently the author is not sending those fields. If you want them to appear in your email body, you can just concatenate them in the $body variable just before the line where the email is sent...

    $body = "Name: " . $sender_name . "
    " .
            "Phone: (" . $country_code . ") " . $phone_number . "
    " .
            $body;
    
    $sentMail = mail($recipient_email, $subject, $body, $headers);
    

    I hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀