PHP:电子邮件提交无法发送

时间:2014-10-14 00:23:11

标签: php email

我目前在提交表单时遇到问题,该表单将用户输入直接发送到我的电子邮箱。一切都通过,它进入成功页面,但它实际上从未发送过。请阅读我的代码,让我知道我在哪里做错了。 (我是PHP的新手,所以可能还有其他问题需要解决。如果你可以跟踪那些,那么你可以获得奖励积分。)

HTML

<form action="contact.php" method="post">
    <table id="info" style="height: 180px">
        <tr>
            <td>Name:</td>
            <td style="text-align: left"><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>E-mail:</td>
            <td style="text-align: left"><input type="text" name="email"></td>
        </tr>
        <tr>
            <td>Topic:</td>
            <td style="text-align: left">
                <select name="topic">
                    <option value="1">General Inquiry</option>
                    <option value="2">Feedback</option>
                    <option value="3">Website</option>
                    <option value="4">Commission</option>
                    <option value="1">Collaboration</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Message:</td>
            <td style="text-align: left"><textarea name="message" style="height: 90px; resize: none; width: 516px;"></textarea></td>
        </tr>
        <tr>
            <td><input type="submit" value="Send Form" style="cursor: pointer";></td>
            <td>&nbsp;</td>
        </tr>
    </table>
</form>

PHP

<?php
if(isset($_POST['email'])) {
    $to = "example@gmail.com";
    $subject = "New Inquiry";
    function died($error) {
        echo "Please fix the following error(s):";
        echo ".$error_message.";
        die();
    }
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
        died('Invalid credentials.');
    }
    $name = $_POST['name'];
    $from = $_POST['email'];
    $topic = $_POST['topic'];
    $message = $_POST['message'];
    $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
    if(!eregi($email_exp,$from)) {
        $error_message .= 'Invalid E-mail.';
    }
    $string_exp = "^[a-z .'-]+$";
    if(!eregi($string_exp,$name)) {
        $error_message .= 'Invalid name.';
    }
    if(strlen($message) < 10) {
        $error_message .= 'Message must contain more than ten characters.';
    }
    if(strlen($error_message) > 0) {
        died($error_message);
    }
    $email_message = "You've received a new inquiry from Example.com!\n\n";
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "E-mail: ".clean_string($from)."\n";
    $email_message .= "Topic: ".$name."\n";
    $email_message .= "Message: ".clean_string($message)."\n";
$headers = 'Inquiry: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $email_message, $headers);  ;}
?>
<html>
<body>
<center>Your message has been sent!</center>
</body>
</html>

0 个答案:

没有答案