0

我正在创建一个网站,并且正在使用 PHP 制作联系表格。它在页面本身上没有出现任何错误,但电子邮件从未出现在我的电子邮件的收件箱或垃圾邮件文件夹中。我的代码是:

        $_NAME = $_POST["name"];
        $_EMAIL = $_POST["reply"];
        $_SUBJECT = $_POST["subject"];
        $_MESSAGE = $_POST["message"];

        $_MAILTO = "myemail@gmail.com";
        $_SUBJECT = "Contact Form";
        $_FORMCONTENT = "From: ".$_NAME." Subject: ".$_SUBJECT." Message: ".$_MESSAGE;
        $_MAILHEADER = "Reply To: ".$_EMAIL;

        mail($_MAILTO, $_SUBJECT, $_FORMCONTENT, $_MAILHEADER);

任何想法是什么问题?

编辑 -

这是 HTML 表单:

<form id="contact" name="contact" action="contact2.php" method="post">
                <input type="text" class="name" name="name" id="name" placeholder="Your name (optional)" onfocus="placeholder=''" onblur="placeholder='Your name (optional)'" /><br><br>
                <input type="text" class="reply" id="reply" name="reply" placeholder="Your email (optional)" onfocus="placeholder=''" onblur="placeholder='Your email (optional)'" /><br><br>
                <input type="text" class="subject" id="subject" name="subject" placeholder="Subject" onfocus="placeholder=''" onblur="placeholder='Subject'" /><br><br>
                <textarea class="message" id="message" name="message" rows="10" cols="50" placeholder="Enter your message" onfocus="placeholder=''" onblur="placeholder='Enter your message'"></textarea><br><br>
                <input type="submit" class="send" id="send" name="send" value="Send Message" />
            </form>
4

2 回答 2

0

第一步是检查邮件函数的返回值,以查看电子邮件是否已成功发送(就 PHP/mail 函数可以确定而言)。

$mail_result = mail($_MAILTO, $_SUBJECT, $_FORMCONTENT, $_MAILHEADER);
if ($mail_result) {
    echo <<<HTML
<div>Mail was successfully sent!</div>
HTML;
} else {
    echo <<<HTML
<div>Sending mail failed!</div>
HTML;
}

其次,您应该检查您的 php.ini 文件中是否正确设置了所有适当的设置,特别是 sendmail_path 设置。您绝对应该查看PHP 手册中的官方文档

作为最后的努力,您可能需要研究一种发送邮件的方法。

于 2013-02-03T00:42:53.973 回答
0

简单示例:

<?php

// Has the form been submitted?
if (isset($_POST['send'])) {
    // Set some variables
    $required_fields = array('name', 'email'); // add fields as needed
    $errors = array();

    $success_message = "Congrats! Your message has been sent successfully!";
    $sendmail_error_message = "Oops! Something has gone wrong, please try later.";

    // Cool the form has been submitted! Let's loop 
    // through the required fields and check
    // if they meet our condition(s)
    foreach ($required_fields as $fieldName) {
        // If the current field in the loop is NOT part of the form submission -OR-
        // if the current field in the loop is empty
        if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {

            // add a reference to the errors array, indicating that these conditions have failed
            $errors[$fieldName] = "The {$fieldName} is required!";
        }
    }

    // Proceed if there aren't any errors
    if (empty($errors)) {
        // add fields as needed
        $name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
        $email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );

        // Email receivers
        $to_emails = "anonymous1@example.com, anonymous2@example.com";

        $subject = 'Contact form sent from ' . $name;
        $message = "From: {$name}";
        $message .= "Email: {$email}";

        $headers = "From: {$name}\r\n";
        $headers .= "Reply-To: {$email}\r\n";
        $headers .= 'X-Mailer: PHP/' . phpversion();

        if (mail($to_emails, $subject, $message, $headers)) {
            echo $success_message;
        } else {
            echo $sendmail_error_message;
        }
    } else {

        foreach($errors as $invalid_field_msg) {
            echo "<p>{$invalid_field_msg}</p>";
        }
    }
}
于 2013-02-03T00:49:14.713 回答