简体   繁体   English

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

[英]Contact Form: Unable to send phone number

I got this script online and it seems to work. 我在网上得到这个脚本,它似乎工作。 However, the form doesn't seem to process name, phone1, phone2. 但是,表单似乎没有处理名称,phone1,phone2。

I don't see any process method where name, phone1, phone2 is being processed and send either. 我没有看到任何处理方法,其中名称,phone1,phone2正在处理和发送。

Could someone help to get the name, phone1 and phone2 to be sent as well in the mail? 有人可以帮助获取名称,phone1和phone2也将在邮件中发送?

The form code has the correct fields and Chrome console shows that data is sent. 表单代码具有正确的字段,Chrome console显示数据已发送。 What do you think is missing? 您认为缺少什么?

Here's the PHP script: 这是PHP脚本:

$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\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 = $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. PS:编写此开源脚本的开发人员没有响应。

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变量中将它们连接起来...

$body = "Name: " . $sender_name . "\r\n" .
        "Phone: (" . $country_code . ") " . $phone_number . "\r\n" .
        $body;

$sentMail = mail($recipient_email, $subject, $body, $headers);

I hope it helps 我希望它有所帮助

You just want to add phone number to message? 您只想在邮件中添加电话号码? If yes, I think below code is enough: 如果是,我认为以下代码就足够了:

$body = $body . "\r\nPhone: " . $country_code . "-" . $phone_number;
$sentMail = mail($recipient_email, $subject, $body, $headers);

If line-breaker didn't work, try: 如果断路器不起作用,请尝试:

$body = $body . "

Phone: ". $country_code . "-" . $phone_number;

$sentMail = mail($recipient_email, $subject, $body, $headers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM