【问题标题】:Having trouble with php mail() function using htmlspecialchars, mail sends every time page loads [duplicate]使用htmlspecialchars的php mail()函数遇到问题,每次页面加载时都会发送邮件[重复]
【发布时间】:2018-04-18 08:55:03
【问题描述】:

我已经搜索并尝试了多种方法,但似乎都没有按计划进行。我正在尝试设置联系表格并使用

<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>

作为表单操作,不确定现在是否需要,但这就是我问的原因,我不希望进行任何注射。该表单正在按照我想要的方式工作并发送邮件,除了每次页面加载时都会发送邮件,无论框中是否有信息或出现错误。 这里有两件事我需要帮助。

  1. 在表单底部的 元素中正确显示感谢消息、模式弹出窗口或引导警报消息。

  2. 我需要 propper isset 函数才能使这个单页邮件程序工作,并且仅在单击提交 时才在 POST 上工作。

下面我将放上我目前拥有的所有 PHP 和 html 表单代码。 提前谢谢你。

      <?php
        // define variables and set to empty values
        $nameErr = $emailErr = $ethaddressErr = $txhashErr = $messagesErr = "";
        $name = $email = $ethaddress = $txhash = $messages = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
            $nameErr = "Name is required";
        } else {
            $name = test_input($_POST["name"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Valid Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["ethaddress"])) {
            $ethaddress = "";
        } else {
            $ethaddress = test_input($_POST["ethaddress"]);
            if (!preg_match("/^(0x)?[0-9a-f]{40}$/i",$ethaddress)) {
            $ethaddressErr = "Invalid ETH Address Format"; 
            }
        }

        if (empty($_POST["txhash"])) {
            $txhash = "";
        } else {
            $txhash = test_input($_POST["txhash"]);
            if (!preg_match("/^(0x)?[0-9a-f]{64}$/i",$txhash)) {
            $txhashErr = "Invalid Ethereum Transaction Hash Format"; 
            }
        }

        if (empty($_POST["messages"])) {
            $messages = "";
        } else {
            $messages = test_input($_POST["messages"]);
        }

        }

        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
      ?>

<form id="my-form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <div class="form-group has-feedback">
    <label for="name" class="control-label sr-only">Name</label>
    <input type="text" name="name" value="<?php echo $name;?>" required placeholder="Please Enter Your Name" autofocus class="form-control" id="firstname" /><i aria-hidden="true" class="form-control-feedback fa fa-user"></i></div>
    <span class="error"><?php echo $nameErr;?></span>
  <div class="form-group has-feedback">
    <label for="ethaddress" class="control-label sr-only">ETH Address</label>
    <input type="text" name="ethaddress" value="<?php echo $ethaddress;?>" placeholder="ERC20 Compliant ETH Address" class="form-control" id="lastname" /><i aria-hidden="true" class="form-control-feedback fa fa-link"></i></div>
    <span class="error"><?php echo $ethaddressErr;?></span>
  <div class="form-group has-feedback">
    <label for="txhash" class="control-label sr-only">TxHash</label>
    <input type="text" name="txhash" value="<?php echo $txhash;?>" placeholder="Transaction Hash of Purchase, if applicable" class="form-control" id="phonenumber" /><i aria-hidden="true" class="form-control-feedback fa fa-hashtag"></i></div>
    <span class="error"><?php echo $txhashErr;?></span>
  <div class="form-group has-feedback">
    <label for="email" class="control-label sr-only">Email Address</label>
    <input type="text" name="email" required value="<?php echo $email;?>" placeholder="Please Enter Valid Email Address" class="form-control" id="email" /><i aria-hidden="true" class="form-control-feedback fa fa-envelope"></i></div>
    <span class="error"><?php echo $emailErr;?></span>
  <div class="form-group has-feedback">
    <label for="messages" class="control-label sr-only">Additional comments for the team</label>
    <textarea rows="8" name="messages" placeholder="Additional Comments for the Team" required class="form-control"><?php echo $messages;?></textarea><i aria-hidden="true" class="form-control-feedback fa fa-pencil"></i></div>
    <span class="error"><?php echo $messagesErr;?></span>
  <button class="btn btn-default btn-lg" type="submit" name="submit" id="form-btn">SEND </button>
</form>

        <?php
        $to = "hashguide@biopaycoin.com";
        $subject = "BioPayCoin Contact Form Submission";
        $name = $_POST["name"];
        $messages = $_POST["messages"];
        $email = $_POST["email"];
        $ethaddress = $_POST["ethaddress"];
        $txhash = $_POST["txhash"];
        $message = $name . " sent you a message" . "\r\n" . $email . "\r\n" . $ethaddress . "\r\n" . $txhash . "\r\n" . $messages;
        $headers = "From: BPC-Contact-form@biopaycoin.com" . "\r\n" .
        "CC: hash.guide@gmail.com";

        mail($to,$subject,$message,$headers);
        ?>

【问题讨论】:

  • 可以去掉“action”属性。

标签: php html email phpmailer


【解决方案1】:

据我所知,您在每次页面加载时在文件末尾调用mail($to,$subject,$message,$headers);。您应该将此部分置于检查是否设置了 $_POST 的条件下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2014-03-24
    • 2015-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多