使用htmlspecialchars的php mail()函数遇到问题,每次加载页面时都会发送邮件

时间:2017-11-06 08:42:02

标签: php html email phpmailer

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

      <?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);
        ?>

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

  1. 在&lt;&lt;&lt;&lt;&lt; span&gt;表单底部的元素,模态弹出窗口或引导警报消息。

  2. 我需要propper isset函数才能使这个页面邮件程序工作,并且只有在提交时才会在POST上显示&lt;按钮&gt;点击。

  3. 下面我将列出我目前拥有的所有PHP和html表单代码。    先谢谢你。

    {{1}}

1 个答案:

答案 0 :(得分:0)

从我看到你在每个页面加载的文件末尾调用mail($to,$subject,$message,$headers);。你应该把这部分置于检查是否设置$ _POST的条件下。

相关问题