...

View Full Version : PHP Validation/Email Wont Send



saxchick1
09-19-2011, 08:56 PM
I am having a few problems, my email wont send I get a few errors where I have my mail (). Also, not too sure how to validate my form. I am using php 6 and if possible would like to stick to it cause its what I understand better.





<?php

$first_name=$_POST['author'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

if (empty($_POST['author']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['subject']))
{
$errors[] = 'Please enter a subject';
}

if (empty($_POST['text']))
{
$errors[] = 'Please enter your comments.';
}

if(empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address.";
}
if (count($errors) == 0)
{
// Process form
mail("myemailaddress@gmail.com","Subject: $subject",
$message,"From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
}
else
{
echo $errors[0];
}
?>

tangoforce
09-19-2011, 09:05 PM
mail() returns a boolean therefore you should always use it inside an if conditional so that you know the result from the mail() call.

True means it connected and communicated with sendmail (not that sendmail successfully sent) and false means something went wrong. This is vital as it lets you track down where the problem lies - your code/server or the internet.

saxchick1
09-20-2011, 12:09 AM
mail() returns a boolean therefore you should always use it inside an if conditional so that you know the result from the mail() call.

True means it connected and communicated with sendmail (not that sendmail successfully sent) and false means something went wrong. This is vital as it lets you track down where the problem lies - your code/server or the internet.

So, my error lies in this portion?




if (count($errors) == 0)
{
// Process form
mail("myemailaddress@gmail.com","Subject: $subject",
$message,"From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
}
else
{
echo $errors[0];
}
?>


I need to put my mail () in an if statement?

Time Sheep
09-23-2011, 08:31 PM
What he tells you to is to make sure it tells you something is wrong if mail() returns false...
You can do this by using this instead.

mail("myemailaddress@gmail.com","Subject: $subject",
$message,"From: $first_name <$email_address>") or die("Mail failed to communicate with the server");
By adding "or die("Error message here");" to the end of a function returning boolean, the script will exit when the function returns false and then print out the given error message.

saxchick1
09-23-2011, 11:10 PM
Thanks! I got it working. :thumbsup:

Time Sheep
09-23-2011, 11:37 PM
Thanks! I got it working. :thumbsup:

Got what working? Die or your problem?
If it's your issue, then please explain how, as I have a problem quite similar to this :-)

saxchick1
09-23-2011, 11:59 PM
Got what working? Die or your problem?
If it's your issue, then please explain how, as I have a problem quite similar to this :-)

I went about a different route and used a different type of form validation.

tangoforce
09-24-2011, 02:16 PM
Got what working? Die or your problem?
If it's your issue, then please explain how, as I have a problem quite similar to this :-)

No you have a problem where you're not setting the senders address from what I recall.

Fakesendmail doesn't like that and on my setup it refuses to send.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum