donmqryh49993 2015-05-03 07:51
浏览 57

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

I am trying to make a contact form using HTML and PHP. The PHP for the form is below:

<?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.

";

        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) . "
";
        $email_message .= "Email:" . clean_string($email) . "
";
        $email_message .= "Message:" . clean_string($message) . "
";

        // Create email headers
        $headers = 'From: ' .$email_From . "
". 'Reply-To:' . $email. "
" . '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
    }
?>

The issue is that when the form is submitted, it never sends an email to my own personal email. Is there something more I have to set up with the web hosting or is the problem purely in the code? I did some research about the appropriate code to put in and have tried many different options but none seem to work.

  • 写回答

1条回答 默认 最新

  • doucigua0278 2015-05-03 08:20
    关注

    Forms that send email are errorprone, and worse yet, if something goes wrong neither you nor the end user usually get an error message. They will just think your customer service is not responding. Things that do go wrong: webserver email server down or malconfigured, recieving mailbox full, spamfilter eat email, rules on webserver's emailserver change and throws away your mails.

    I recommend a cloud hosted form, that stores the form submissions and notify you by email, but email is not the primary data storage.

    Something like this perhaps, has a free plan: http://www.wufoo.com/features/

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据