PHP Email Sender保持循环

时间:2012-06-25 04:59:29

标签: php html email

我有一个简单的电子邮件发件人。它工作正常,并发送电子邮件,但它提交了几个提交副本几乎像循环。我无法确定是什么导致它循环:

<div id="content">

    <?php 
    $email = $_POST['email'];
    $message = $_POST['message'];
    $firstName = $_POST['fname'];
    $lastName = $_POST['lname'];

    $errorForm = false;
    if(strlen($message) < 12){
        $errorForm = true;
        $errorMessage = true;
    }

    if(strlen($email) < 12){
        $errorForm = true;
        $errorMail = true;
    }

    if(strlen($firstName) < 3){
        $errorForm = true;
        $errorFname = true;
    }

    if(strlen($lastName) < 3){
    $errorForm = true;
        $errorLname = true;
    }

    if($errorForm == true ){?>

    <h3>Jane Doe</h3>
    <p>Owner, Founder &amp; Graphic Designer</p>
    <a href="mailto:info@example.com">info@example.com</a>

    <div id="contact">
            <form action="sender-contact.php" method="post">
              <fieldset>

                  <legend>There was an error in your submission. Please correct it.</legend>

                  <label for="fname">First Name</label>
                  <input type="text" size="20" maxlength="18" name="fname" id="fname" class="<?php if($errorFname == true){echo "error";}?>" value ="<?=$firstName?>"/>

                  <label for="lname">Last Name</label>
                  <input type="text" size="20" maxlength="18" name="lname" id="lname" class="<?php if($errorLname == true){echo "error";}?>"value ="<?=$lastName?>"/>

                  <label for="email"> eMail address</label>
                  <input type="text" size="40" maxlength="38" name="email" id="email" class="<?php if($errorMail == true){echo "error";}?>"value ="<?=$email?>"/>

                   <label for="message"> Your message</label>
                   <textarea name="message" id="message" rows="8" cols="60" class="<?php if($errorMessage == true) {echo "error"; }?>" ><?=$message?></textarea>

                  <input type="submit" value="Send" id="submit"  class="submit-button" />
              </fieldset>
          </form> 
    </div>

        <?php } 

        if($errorForm == false){

            $to = "info@example.com";
            $email = $_REQUEST['email'] ;
            $subject = "New Email from info@example.com" ;
            $message = $_REQUEST['message'] ;
            $headers = 'New message from '.$_POST['fname'].' '.$_POST['lname'].': \n';
            $headers .= $_POST['email'];
            mail($to, $subject, $message, $headers);
            ?>

         <?php
            echo ("<h3>\r\nWhen you buy from a small business, you aren't helping a CEO buy a 3rd vacation home. You are helping a little girl get dance lessons, a boy play hockey, a mom put food on the table, a dad pay a mortgage, or a student pay for college. Our customers are our neghbours and our shareholders...they are the ones we strive to make happy.<br/> <br/>Thank you for supporting small business and I'll get in touch with you within 72 hours!</h3>"); ?>

    <?php   } ?>
</div>

1 个答案:

答案 0 :(得分:1)

在显示表单并运行php mail函数之前,请尝试检查是否单击了提交按钮:

<?php

if (isset($_POST['theNameOfSubmitButton']))
{
    //create your variables from the form
      $email = $_POST['email'];
      $message = $_POST['message'];
      $firstName = $_POST['fname'];
      $lastName = $_POST['lname'];

      ....

    //send email
    if(mail("$to","$subject","$message","$headers"))
    {
    //if mail sends, say thank you
    echo ("<h3>\r\nWhen you buy from a small business, you aren't helping a CEO buy a 3rd vacation home. You are helping a little girl get dance lessons, a boy play hockey, a mom put food on the table, a dad pay a mortgage, or a student pay for college. Our customers are our neghbours and our shareholders...they are the ones we strive to make happy.<br/> <br/>Thank you for supporting small business and I'll get in touch with you within 72 hours!</h3>");

    // else, if mail fails, email web master, give "pretty" error message
      }
      else
      {
      echo "Message failed to send. sorry about that....";
      mail("webmaster@domain.com", "problem with email form", "fix this", From:admin@domain.com");
      }
    //else, the submit button was not clicked... display the form
    }

    <!-- code for form goes here -->

使用此方法有助于避免大量“奇怪的事情”