1

I made a PHP script for sending mail.....but I only get an "ERROR" message which is in the else condition...and I can't get mail.....that means my mail function doesn't work...

What could be the issue?

$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$comments=$_POST['comments'];
$verify=$_POST['verify'];

if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="error_message">Attention! Please enter a valid phone number.</div>';
exit();
} else if(!is_numeric($phone)) {
echo '<div class="error_message">Attention! Phone number can only contain digits.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}


$to="contact@yoursite.com";

$subject='Inquiry';

$message='Name : '.$name."<br />".'Email : '.$email."<br />".'Mobile : '.$phone."<br />".'Message : '.$comments;

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "X-Mailer: PHP/".phpversion();

$mail=mail($to, $subject, $message, $headers);

if($mail) {
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h4>Email Sent Successfully</h4>";
echo "<p>Thank you $name, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
}
else {
echo "ERROR";
}
Siddhu
  • 37
  • 8

2 Answers2

3

It seems mail function is returning false. mail function always return true or false. So you cant find out easily what went wrong if mail didnt work.It could be SMTP error, Connection Error or anything. Number of possibilities are there. have a look at this link

Community
  • 1
  • 1
gaurav malik
  • 556
  • 1
  • 6
  • 17
2

This is the issue i assume.

if(!isEmail($email)) {}

IsEmail is not a fucntion unless you mean is_email which i think is wordpress? or one i know of but you can use filter_var with filter_email

this would work too

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { //Return True Is Valid
echo "HEY IM VALID";
}

Also i would redo the if statement in some way like this

foreach ($_POST as $item => $value){
    if($value === ""){ echo $item." must be a valid input"; exit();}
 }
  • ok...bt its a validation issue...I want to solve mail issue....plz help me on that.. – Siddhu Nov 16 '16 at 06:05
  • your mail structure is fine, and does send the email, might want to check your hosting, do they allow the mail function most free hosting blocks it but msg me up ill give ya a free account on my hosting server – Steven Koelsche Nov 16 '16 at 06:07
  • else if(trim($verify) != '4') { echo '
    Attention! The verification number you entered is incorrect.
    '; exit(); } Make sure $_POST['verify'] = 4
    – Steven Koelsche Nov 16 '16 at 06:15