【问题标题】:My PHP Contact Form is not working我的 PHP 联系表不起作用
【发布时间】:2013-02-03 00:32:16
【问题描述】:

我正在创建一个网站,并且正在使用 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>

【问题讨论】:

  • 请提供您的完整代码,包括 HTML 表单。
  • 您是否在本地主机上进行测试?如果是这样,请将脚本移动到托管服务器并在那里尝试。所有大写字母的变量名称都不是很好看。确保也验证和清理用户的输入。
  • 您的 SMTP 服务器是否在 php.ini 中正确配置?您的 SMTP 服务器是否正常工作?如果您有权访问 SMTP 服务器日志,您是否看到任何错误/消息?
  • 虽然本身并没有错,但我必须告诉你,你选择的变量命名风格不是很聪明。您可以使用$name 代替$_NAME,或使用$email 代替$_EMAIL,因为以下划线开头的变量通常由PHP 用于(至少)超全局变量(像 $_POST** 或 **$_GET) 并且在其他人中这样做会导致混乱。更多关于 superglobals 的信息在这里 - php.net/manual/en/language.variables.superglobals.php

标签: php forms contact


【解决方案1】:

第一步是检查邮件函数的返回值,看看邮件是否成功发送(就 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 设置。你一定要看看 official documentation in the PHP Manual

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

【讨论】:

    【解决方案2】:

    简单示例:

    <?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>";
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      相关资源
      最近更新 更多