PHP问题mail()

时间:2017-08-22 19:24:12

标签: php email

这是我第一次在php工作,我遇到了让mail()工作的问题。我正在使用MAMP Pro进行测试(通常有一个真正的电子邮件地址要发送到)。当我点击发送按钮时,它会清除表单,但不会显示感谢信息(它也不会显示底部的两个分隔符,只是条纹),我还没有收到成功的电子邮件。任何帮助表示赞赏!

<!doctype html>
<html lang="en-us">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!--[if IE]><meta http-equiv="cleartype" content="on" /><![endif]-->
    <title>Twin City Grill</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="styles/normalize.css">
    <link rel='stylesheet' type="text/css" href="styles/main.css">
</head>
<body>
    <nav class="navbar">
        <a href="index.php">
        <img class="logo" src="assets/Twin-City-Grill-white.png" alt="Twin City Grill, white blocky serif font">
        <h1 class="screen-reader-only">Twin City Grill</h1>
        </a>
        <div class="links-container">
            <a href="index.php" class="navbar-link">Contact</a>
        </div>
    </nav>

    <div class="wrapper">
        <div class="divider"></div>
        <div class="divider"></div>
        <div class="contact-form">

    <?php
    $action=$_REQUEST['action'];
    if ($action=="")
        {
        ?>
            <form  action="" method="POST" enctype="multipart/form-data" role="form" id="contact-form">
                <input type="hidden" name="action" value="submit">
                <h2>Contact Us</h2>
                <p class="form-instructions"><em>All fields are required.</em></p>
                <div class="form-group">
                    <label for="name" class="screen-reader-only">Name:</label>
                    <input type="text" name="name" id="name" placeholder="Name" required="required">
                </div>
                <div class="form-group">
                    <label for="phone" class="screen-reader-only">Phone Number:</label>
                    <input type="tel" name="phone" id="phone" placeholder="Phone Number" required="required">
                </div>
                <div class="form-group">
                    <label for="email" class="screen-reader-only">Email Address:</label>
                    <input type="email" name="email" id="email" placeholder="Email Address" required="required">
                </div>
                <div class="form-group">
                    <label for="message" class="screen-reader-only">Message:</label>
                    <textarea name="message" id="message" cols="30" rows="10" placeholder="Your Message" required="required"></textarea>
                </div>
                <div class="form-group">
                    <input id="submit" name="submit" type="submit" value="Send" >
                </div>
            </form>
        <?php
        }
    else
        {
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $phone=$_REQUEST['phone'];
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($message=="")||($phone==""))
            {
            echo "All fields are required, please fill <a href=\"\">the form</a> again.";
            }
        else{
            $from="From: $name<$email>\r\nReturn-path: $email";
            $subject="Message sent using your contact form";
            $email_message .= "Name: ".clean_string($name)."\n";
            $email_message .= "Email: ".clean_string($email)."\n";
            $email_message .= "Telephone: ".clean_string($phone)."\n";
            $email_message .= "Message: ".clean_string($message)."\n";
            mail("testemail@gmail.com", $subject, $email_message, $from);
            ?>
            <h2>Thanks!</h2>
            <p>We will get back to you as soon as possible. Have a great day!</p>
            <?php
            }
        }
    ?>
        </div>

        <div class="divider"></div>
        <div class="divider"></div>
    </div>

<footer>

</footer>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

看起来mail()函数因任何原因无声地失败。尝试这样的方法来获得实际的错误:

$success = mail("testemail@gmail.com", $subject, $email_message, $from);
if (!$success) {
    echo error_get_last()['message'];
}

答案 1 :(得分:0)

而不是$from变量,请尝试此

$from = 'From: from@example.com' . "\r\n" .
    'Reply-To: from@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

同时检查您的垃圾邮件