簡體   English   中英

PHP聯系人表格給用戶的電子郵件隨機確認號

[英]PHP contact form email random confirmation number to user

我有一個php聯系人表格,當用戶提交表格時,我和用戶都會收到一封確認電子郵件。 我想做的是生成一個隨機的確認號,並通過電子郵件發送給用戶和我作為參考。 這是我必須生成隨機數的代碼:

 <p>Your confirmation number is: <strong><?php echo mt_rand(100000,999999);?></strong>. Keep this for your records.</p>

該號碼顯示在表單上,​​但是我的問題是我不知道如何輸出此號碼才能通過電子郵件發送。 請原諒我不是程序員,但我確實有一些非常基本的PHP知識。 因此,如果有人可以幫我解決這個問題,我將不勝感激。

這是我學校網站上的表格: http : //www.inrf.uci.edu/facility/services/service-request/

這也是我表單中的一些php代碼:

    // name
    if(trim($_POST['contactName']) === '') {
        $nameError = '<span class="error">Please enter your name.</span>';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }
    // email
    if(trim($_POST['email']) === '')  {
        $emailError = '<span class="error">Please enter your email address.</span>';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = '<span class="error">You entered an invalid email address.</span>';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }



    // body ------------------------------------------
    if(!isset($hasError)) {
        $emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = 'myemail@email.com';
        }
        $subject = '[Foundry Form] From '.$name;

        $body .= "Name: $name \n\n";
        $body .= "Email: $email \n\n";
        $body .= "Phone: $phone \n\n";

        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: . $email';
        $headers .= "\r\n" . 'BCC: myotheremail@email.com' . "\r\n";

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }




  }


}


?>


<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
  <h1 class="title"><?php the_title(); ?></h1>


 <?php if(isset($emailSent) && $emailSent == true) { ?>
        <div class="thanks">
          <p><strong>Thank You for Submitting a Service Request!</strong></p>
          <p>An INRF Staff Member will contact you shortly.</p>
          <p>If you do not receive a confirmation e-mail within 1-2 business days, you may contact us directly at (949) 824-2819 or <a href="mailto:info@inrf.uci.edu">info@inrf.uci.edu</a>.</p>
          <p>We look forward to serving your research needs!</p>


           <hr style="border:1px dashed #CCC;" />
          <?php
             echo "<Strong>Your Service Request information</strong><br>";
             echo "(You may print this page for your records)<br><br>";
             echo "<u>Contact Information</u><br>";
             echo "Name: ".$name;
             echo "<br>";
             echo "Email: ".$email;
             echo "<br>";

?>

        </div>
        <?php } else { ?>
        <?php the_content(); ?>
        <?php if(isset($hasError) || isset($captchaError)) { ?>
        <p class="error">Sorry, an error occured. See below.<p>

          <?php } ?>


           <?php if($captchaError != '') { ?><span class="error"><?=$captchaError;?></span><?php } ?>


     <script type="text/javascript">var RecaptchaOptions = { theme : 'white'};</script>

        <form action="<?php the_permalink(); ?>" id="foundryForm" method="post">
          <strong>Contact</strong> <br />
          <br />
          <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><label for="contactName">*Name:</label></td>
              <td><input type="text" size="40" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
                <?php if($nameError != '') { ?>
                <span class="error">
                <?=$nameError;?>
                </span>
                <?php } ?></td>
            </tr>
            <tr>
              <td><label for="email">*Email:</label></td>
              <td><input type="text" size="40" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
                <?php if($emailError != '') { ?>
                <span class="error">
                <?=$emailError;?>
                </span>
                <?php } ?></td>
            </tr>


          </table>


    <br /><br /> 

      <?php
          require_once('scripts/recaptchalib.php');
          $publickey = "6LfR0eESAAAAAFtSdYmpqqVnVwP8ZMHCr--BIu5f"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>

          <p><input type="submit"></input></p>
          <input type="hidden" name="submitted" id="submitted" value="true" />
        </form>
        <?php } ?>      

您應該做的第一件事是在發送電子郵件之前創建確認號碼:

$confirmationNumber = mt_rand(100000,999999);

然后將其添加到電子郵件的正文中:

$body .= "Confirmation Number: $confirmationNumber\n\n";

然后在頁面上顯示時使用$confirmationNumber

<p>Your confirmation number is: <strong><?php echo $confirmationNumber; ?></strong>. Keep this for your records.</p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM