Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将联系人表单发送到个人电子邮件时出现问题_Php_Html_Css_Forms_Contact - Fatal编程技术网

Php 将联系人表单发送到个人电子邮件时出现问题

Php 将联系人表单发送到个人电子邮件时出现问题,php,html,css,forms,contact,Php,Html,Css,Forms,Contact,我正在尝试使用HTML和PHP制作一个联系人表单。表单的PHP如下所示: <?php if(isset($_POST['email'])) { // Email to information $email_to ="personalemail@email.com"; $email_subject ="Contact"; $email_from ="Person"; // Error code

我正在尝试使用HTML和PHP制作一个联系人表单。表单的PHP如下所示:

<?php
    if(isset($_POST['email'])) {

        // Email to information
        $email_to ="personalemail@email.com";
        $email_subject ="Contact";
        $email_from ="Person";


        // Error code
        function died($error) {
            echo "We are sorry, but there were error(s) found with the form you submitted.";
            echo "These errors appear below.<br/><br/>";
            echo $error. "<br/><br/>";
            echo "Please go back and fix these errors.<br/>";
            die();
        }

        // Validation
        if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['message'])) {
            died('We are sorry but there appears to be a problem with the form you submitted.');    
        }

        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];
        $message = $_POST['message'];

        $error_message = "";


        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error_message .= 'The email address you entered does not appear to be valid.<br/>';
        }

        $string_exp = "/^[A-Za-z.'-]+$/";
        if(!preg_match($string_exp, $fname)) {
            $error_message .= 'The first name you entered does not appear to be valid.<br/>';
        }

        if(!preg_match($string_exp, $lname)) {
            $error_message .= 'The last name you entered does not appear to be valid.<br/>';
        }

        if(strlen($message) < 2) {
            $error_message .= 'The message you entered does not appear to be valid.<br/>';
        }

        if(strlen($error_message) > 0) {
            died($error_message);
        } 
        $email_message = "Form details below.\n\n";

        function clean_string($string) {
            $bad = array("content-type", "bcc:", "to:", "cc:", "href");
            return str_replace($bad, "", $string);
        }

        $email_message .= "Name:" . clean_string($fname) . clean_string($lname) . "\n";
        $email_message .= "Email:" . clean_string($email) . "\n";
        $email_message .= "Message:" . clean_string($message) . "\n";

        // Create email headers
        $headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion();
        mail($email_to, $email_subject, $email_message, $headers);
        ?>

        Thankyou for contacting me. I will be in contact with you shortly. <br/>
        Please click <a href="index.html">here</a> to go back to the main website
<?php
    }
?>

谢谢你联系我。我很快就会和你联系
请点击返回主网站

问题是,当提交表单时,它从不向我自己的个人电子邮件发送电子邮件。我是否还需要为web托管设置其他内容,或者问题完全在代码中?我做了一些关于适当代码的研究,并尝试了许多不同的选择,但似乎都不起作用

发送电子邮件的表单容易出错,更糟糕的是,如果出现问题,您和最终用户通常都不会收到错误消息。他们只会认为您的客户服务没有响应。出现错误的情况:Web服务器电子邮件服务器关闭或配置错误、接收邮箱已满、垃圾邮件过滤器吃掉电子邮件、更改Web服务器电子邮件服务器的规则并丢弃您的邮件

我建议使用云托管表单,存储表单提交并通过电子邮件通知您,但电子邮件不是主要数据存储

也许像这样的东西有一个免费的计划:

您是否正在本地主机上尝试此操作?您遇到了什么错误?你试着打印错误了吗?我在测试之前把它上传到服务器上,这样PHP就可以正常工作了。我没有收到任何错误,我只是从来没有收到来自表单的电子邮件。你检查过你的垃圾邮件文件夹吗?我不知道这是一个选项,我一定会调查它。你还忘了将链接粘贴到你推荐的网站上。