php - 将联系表发送到个人电子邮件的问题

标签 php html css forms contacts

我正在尝试使用 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
    }
?>

问题是提交表单时,它永远不会向我自己的个人电子邮箱发送电子邮件。我还需要为网络托管设置一些东西吗?还是问题纯粹出在代码中?我对要放入的适当代码进行了一些研究,并尝试了许多不同的选项,但似乎都没有用。

最佳答案

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

我推荐一个云托管表单,它存储表单提交并通过电子邮件通知您,但电子邮件不是主要数据存储。

也许像这样,有一个免费计划: http://www.wufoo.com/features/

关于php - 将联系表发送到个人电子邮件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30011484/

相关文章:

php - 左连接的 MySQL 查询问题

javascript - 如何使用 Javascript 检查视频文件中的音频?

css - Chrome/Firefox 中不一致的 <img> 大小调整行为

javascript - 在使用 JavaScript 函数调用创建动态表的实例中创建日期选择器

javascript - Bootstrap 固定页眉和页脚,带有可折叠侧边栏

css - 如何将裁剪后的图像扩展到盒子中?

javascript - 根据窗口大小动态调整图像缩略图,全出血

php - 使用 MySQL 和 PHP 的数据透视报告

php - 使用php套接字以特定的数据包格式发送数据

php - 在Android中通过文件(使用FileInputStream)将图像发送到PHP并通过Blob保存mysql