我有一个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 } ?>
答案 0 :(得分:1)
您应该做的第一件事是在发送电子邮件之前创建确认号码:
$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>