Well my PHP contact us form is sending email too well
But I am getting 7 emails per submission
Problem is I have no idea why I am getting 7 emails per submission.
Any thoughts?
CODE
<?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>";
}
}
}
}
}
?>
You also have a very common error a lot of people have with "Contact Us" forms.
$mail->SetFrom($email, $name);
This will break SPF and also cause DMARC to fail and you will never get the message from some people, if your mail server you use has DMARC enabled on it and GMAIL does.
Since DMARC is a more recent protocol, a lot of the old cookie cutter code for contact us forms - doesn't take this into account.
You can read more about that here: "DMARC - Contact Us Form Nightmare"
The suggested workaround will be to do:
$mail->SetFrom("<Your email Adddress>, $name);
You have the the customers contact email in the body of the message which is perfect.
This way - you avoid the issue outline in the article. You won't quickly be able to hit the "Reply" button, but at least you'll get the emails from those customers who have DMARC enabled.