1

我刚开始为一家新公司工作,一位以前的员工使用 PHP 开发了他们的网站。我不确定他正确/错误的是什么。我知道大量的 HTML 和 CSS,对 PHP 有一点了解,但了解不多。但我正在尝试在 W3 学校学习 :)

我的网站上有一个联系表格,但是每当我单击提交时,我的电子邮件地址都没有收到任何内容,尽管它确实说它已提交。

提前感谢您的帮助!

下面是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Anchor Technology | Services</title>
    <?php include('includes/head.php'); ?>
    <style type="text/css">#maintext .error h2 {color: red;</style>
</head>
<body>
<div id="maincontainer">
<?php include('includes/header.php'); ?>
<?php include('includes/navigation.php'); ?>
    <div class="shadow">
        <div id="maintext">
            <h2>Help!</h2>
        <!--Form options start here-->
        <!--Small Form Here-->
            <h2><strong>Existing Client Form</strong></h2>
            <?php
                error_reporting(E_ERROR | E_WARNING | E_PARSE);
                $form_block = "
                <p>Required fileds are marked with an asterick (*)        </p>
                <form action=\"$_SERVER[php_self]\"method=\"POST\">
                <P>*Company Name:</p>
                    <input size=40 name=\"name\" value=\"$_POST[name]\"><br />    <br />
                <p>*Contact Name:</p>
                    <input size=40 name=\"ctname\" value=\"$_POST[ctname]\"><br /><br />    
                <P>*Request Title:</p>
                    <input size=40 name=\"title\" value=\"$_POST[title]\"><br /><br />
                <P>Description:</p>
                  <textarea cols=\"60\"rows=\"10\"name=\"description\">$_POST[description]</textarea><br /><br />
                <P>Computer Name:</p>
                    <input size=20 name=\"cpname\" value=\"$_POST[cpname]\"><br /><br />
                <p>User Name:</p>
                    <input size=40 name=\"uname\" value=\"$_POST[uname]\"><br /><br />  
                <P>Priority Level</p>
                    <select name=\"level\">
                        <option value=\"High\">High</option>
                        <option value=\"Medium\">Medium</option>
                        <option value-\"Low\" selected=\"default\">Low</option>
                    </select><br /><br />   
                <input type=\"hidden\" name=\"op\" value=\"ds\">        
                <input type=\"submit\" name=\"submit\" value=\"Submit\">    
            </form>";
            if ($_POST[op] != "ds") {
                //need to see form
                echo "$form_block";
                //check required fields
            } else if ($_POST[op] == "ds") {
                If (($_POST[name] == "") || ($_POST[ctname] == "") || ($_POST[title] == "")) {
                    $msg_err = "<div class=\"error\"<h2>    <strong>Please fill in all required fields</strong></h2></div>";
                    $send = "no";
                }
                if ($send != "no") {
                    //it's ok to send
                    // create msg variable containing the message that will be sent to the email recipient  
                    $msg .= "Company Name: $_POST[name]\n ";

                    // continue to concatenate the variable adding new pieces of information submitted

                    $msg .= "Company Name: $_POST[name]\n ";
                    $msg .= "Contact Name: $_POST[ctname]\n ";
                    $msg .= "Request Title: $_POST[title]\n ";              
                    $msg .="Description:$_POST[description]\n";             
                    $msg .= "Computer Name: $_POST[cpname]\n ";                 
                    $msg .= "User Name: $_POST[uname]\n ";              
                    $msg .= "Priority Level: $_POST[level]\n ";             

                    //Create variables to be used in the php Mail Function
                    $recipient = "help@anchortechnology.com";
                    $subject = "Help request from $_POST[name]";
                    $mailheaders = "From: $_POST[ctname] \n";
                    $mailheaders .= "Reply-To: $_POST[ctname]";

                    //send mail form
                    //Use mail Function to Send
                    mail($recipient, $subject, $msg, $mailheaders);

                    // show confirmation message
                    // echo statements will be shown when script executes

                    echo "<p>Thank you, $_POST[name]!</p>";
                    echo "<p>Your message was sent!</p>";
                    echo "<a href=\"../index.php\">Return Home</a>";
                } else if ($send == "no") {
                    //print error messages
                    echo "$msg_err";
                    echo "$form_block";
                }
            }
            ?>
            <br /><br />
        </div>
    </div>      
    <div class="shadow">        
        <div id="footer">
            <?php include('includes/footer.php'); ?>
        </div>
    </div>
</div>                  
</body>
</html>
4

2 回答 2

2

First, narrow down the problem. Can you get any mail sent from PHP? Try a simple mail() test script to send a simple message.

If that works, then examine the mail($recipient, $subject, $msg, $mailheaders); part of the code by echoing out or var_dump() the variables before the call to mail.

One problem is that PHP mail() returns boolean true or false based upon success, but the script as posted completely ignores this and just assumes it went ok. This isn't going to cause email not to send, but it WILL cause the script to report it was sent ok even if the mail() function flat-out told you it didn't work. Sadly this is never addressed in the documentation itself, and all the examples show mail without an attempt to check success.

Part of this is because mail() does not give useful error messages of any kind, so it usually just works or it doesn't - and if the function returns true it doesn't mean it really worked. For "mission critical" emails, do no use mail() alone to make sure you are getting important messages or capturing important data.

However, this is a start to finding out what is going wrong. First ensure proper PHP mail() functionality, then go from there.

于 2013-10-22T23:22:37.343 回答
0

好的,我从一位对此进行了调查的朋友那里发现了主要问题。

我们正在使用 IX Webhosting,并且有一个 Linux 和一个 Microsoft 服务器,每个服务器都托管几个站点。PHP 站点托管在 Microsoft 机器上,据我所知,它没有设置为运行 PHP,但 Linux 是。

换了就没有问题了。

感谢你的帮助!

于 2013-11-05T20:11:58.117 回答