【发布时间】:2015-10-08 08:50:24
【问题描述】:
嗯,我的 PHP 联系我们表单发送电子邮件太棒了
但我每次提交都会收到 7 封电子邮件
问题是我不知道为什么每次提交都会收到 7 封电子邮件。
有什么想法吗?
代码
<?php
if(isset($_POST['Send'])){
$first_name =trim($_POST['first_name']);
$last_name=trim($_POST['last_name']);
$phone_number=trim($_POST['phone_number']);
$email=trim($_POST['email']);
$msg=trim($_POST['msg']);
$name=$first_name." ".$last_name;
if($first_name == '' ||$last_name =='' || $phone_number == '' || $email == ''|| $msg == '' ){
$merror = "<p style='color:red;'> * Kindly fill all Fileds<p>";
}else{
foreach($_POST as $value){
if(stripos($value, 'Content-Type:')!== FALSE || $_POST['Address']!== "" ) {
$merror = "<p style='color:red;'> * The information you have entered has a problem</p>";
}else{
require_once "class.phpmailer.php";
$mail= new PHPMailer();
if(!$mail->ValidateAddress($email)){
$merror = "<p style='color:red;'> * Please enter a valid email address</p>";
}else{
$email_body = "";
$email_body = $email_body . "Name: ". $name ."<br>";
$email_body = $email_body . "Phone: ". $phone_number. "<br>";
$email_body = $email_body . "Email: ". $email . "<br>";
$email_body = $email_body . "Message: " . $msg . "<br>";
$mail->SetFrom($email, $name);
$address = "s@example.co";
$mail->AddAddress($address, Trial);
$mail->Subject= "Ess contact form message ".$name;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo"<script>window.open('Contact.php','_self')</script>";
}
echo"<script>window.open('Contact.php?status=thanks','_self')</script>";
}
}
}
}
}
?>
【问题讨论】:
-
你的代码在哪里?!!
-
好吧,我可以告诉你,这是你的
foreach循环。您正在为尽可能多的输入字段运行它。 -
是的,如果您的
$mail->Send()呼叫处于循环中并且该循环运行了七次,那么您将收到七封电子邮件。