Jump to content

Validation Server Side Not Sending Email


CBaZ

Recommended Posts

It validates fine just doesn't send email and no errors.  Totally stomped.

 

<?php
//simple captcha
if(isset($_POST['answer']) && $_POST['answer']!='14') { echo "<script>alert('You provided a wrong answer for the Security Question')</script>"; 
echo "<a href='contact.php'>Please return to the Form and try again.</a><br /><br />\n";


 //simple captcha
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_phone = $_POST['phone'];
$field_preferred = $_POST['preferred'];
$field_datepicker = $_POST['datepicker'];
$field_passengers = $_POST['passengers'];
$field_pickup = $_POST['pickup'];
$field_drop = $_POST['drop'];
$field_message = $_POST['message'];

if(!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $field_email))
   {
    echo "That is not a valid <b>email</b> address. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    exit;
   }

   if(empty($_POST['name']) || strlen(trim($_POST['name'])) ==0)
   {
    echo "Your <b>name</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['email']) || strlen(trim($_POST['email'])) ==0)
   {
    echo "Your <b>email</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['message']) || strlen(trim($_POST['message'])) ==0)
   {
    echo "You <b>message</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['pickup']) || strlen(trim($_POST['pickup'])) ==0)
   {
    echo "Your <b>pickup</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['drop']) || strlen(trim($_POST['drop'])) ==0)
   {
    echo "Your <b>drop off</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else
   {

$mail_to = '[email protected]';
$subject = 'New Message from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Preferred Method: '.$field_preferred."\n";
$body_message .= 'Appointment Date: '.$field_datepicker."\n";
$body_message .= 'Number of Passengers: '.$field_passengers."\n";
$body_message .= 'Pick Up Location: '.$field_pickup."\n";
$body_message .= 'Drop Off Location: '.$field_drop."\n";
$body_message .= 'Message: '.$field_message."\n";

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";


$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.htm';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to techsuppor');
window.location = 'index.htm';
</script>
<?php
}
}
}
?>

 

 

How about you try not having the mail() command as a variable. Like below:

 

<?php
//simple captcha
if(isset($_POST['answer']) && $_POST['answer']!='14') { echo "<script>alert('You provided a wrong answer for the Security Question')</script>"; 
echo "<a href='contact.php'>Please return to the Form and try again.</a><br /><br />\n";


 //simple captcha
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_phone = $_POST['phone'];
$field_preferred = $_POST['preferred'];
$field_datepicker = $_POST['datepicker'];
$field_passengers = $_POST['passengers'];
$field_pickup = $_POST['pickup'];
$field_drop = $_POST['drop'];
$field_message = $_POST['message'];

if(!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $field_email))
   {
    echo "That is not a valid <b>email</b> address. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    exit;
   }

   if(empty($_POST['name']) || strlen(trim($_POST['name'])) ==0)
   {
    echo "Your <b>name</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['email']) || strlen(trim($_POST['email'])) ==0)
   {
    echo "Your <b>email</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['message']) || strlen(trim($_POST['message'])) ==0)
   {
    echo "You <b>message</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['pickup']) || strlen(trim($_POST['pickup'])) ==0)
   {
    echo "Your <b>pickup</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else if(empty($_POST['drop']) || strlen(trim($_POST['drop'])) ==0)
   {
    echo "Your <b>drop off</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
   }
   else
   {

$mail_to = '[email protected]';
$subject = 'New Message from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Preferred Method: '.$field_preferred."\n";
$body_message .= 'Appointment Date: '.$field_datepicker."\n";
$body_message .= 'Number of Passengers: '.$field_passengers."\n";
$body_message .= 'Pick Up Location: '.$field_pickup."\n";
$body_message .= 'Drop Off Location: '.$field_drop."\n";
$body_message .= 'Message: '.$field_message."\n";

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

if (mail($mail_to, $subject, $body_message, $headers)) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.htm';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to techsuppor');
window.location = 'index.htm';
</script>
<?php
}
}
}
?>

 

Let me know if it works! :)

You have got it muddled up, the first if statement contains everything else. I tidied up your code, here is what it looks like.

 

<?php
//simple captcha
if (isset($_POST['answer']) && $_POST['answer'] != '14') {
    echo "<script>alert('You provided a wrong answer for the Security Question')</script>";
    echo "<a href='contact.php'>Please return to the Form and try again.</a><br /><br />\n";
    
    //simple captcha
    $field_name       = $_POST['name'];
    $field_email      = $_POST['email'];
    $field_phone      = $_POST['phone'];
    $field_preferred  = $_POST['preferred'];
    $field_datepicker = $_POST['datepicker'];
    $field_passengers = $_POST['passengers'];
    $field_pickup     = $_POST['pickup'];
    $field_drop       = $_POST['drop'];
    $field_message    = $_POST['message'];
    
    if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $field_email)) {
        echo "That is not a valid <b>email</b> address. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
        exit;
    }
    
    if (empty($_POST['name']) || strlen(trim($_POST['name'])) == 0) {
        echo "Your <b>name</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    } else if (empty($_POST['email']) || strlen(trim($_POST['email'])) == 0) {
        echo "Your <b>email</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    } else if (empty($_POST['message']) || strlen(trim($_POST['message'])) == 0) {
        echo "You <b>message</b> was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    } else if (empty($_POST['pickup']) || strlen(trim($_POST['pickup'])) == 0) {
        echo "Your <b>pickup</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    } else if (empty($_POST['drop']) || strlen(trim($_POST['drop'])) == 0) {
        echo "Your <b>drop off</b> location was not entered into the field. Please return to the" . " <a href='contact.php'>previous page and try again.</a>";
    } else {
        
        $mail_to = '[email protected]';
        $subject = 'New Message from ' . $field_name;
        
        $body_message = 'From: ' . $field_name . "\n";
        $body_message .= 'E-mail: ' . $field_email . "\n";
        $body_message .= 'Phone: ' . $field_phone . "\n";
        $body_message .= 'Preferred Method: ' . $field_preferred . "\n";
        $body_message .= 'Appointment Date: ' . $field_datepicker . "\n";
        $body_message .= 'Number of Passengers: ' . $field_passengers . "\n";
        $body_message .= 'Pick Up Location: ' . $field_pickup . "\n";
        $body_message .= 'Drop Off Location: ' . $field_drop . "\n";
        $body_message .= 'Message: ' . $field_message . "\n";
        
        $headers = 'From: ' . $field_email . "\r\n";
        $headers .= 'Reply-To: ' . $field_email . "\r\n";
        
        
        $mail_status = mail($mail_to, $subject, $body_message, $headers);
        
        if ($mail_status) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.htm';
</script>
<?php
        } else {
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to techsuppor');
window.location = 'index.htm';
</script>
<?php
        }
    }
}
?>

 

If your captcha answer is incorrect, the rest of the form will validate. Your logic there is a little bit off. Now that I've given you something to think about, you should be able to fix it.
 
Also, ereg functions are deprecated. It is advised to use preg_match and other similar functions.
 
If you are still stuck, perhaps try placing the following block of code after your opening <?php at the top of the page just incase.
 
error_reporting(E_ALL);
ini_set("display_errors", "On");
 
Otherwise, paste your updated code and we can have another look.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.