Page 1 of 1

mail() not going to my inbox

Posted: Tue Jul 04, 2006 9:02 pm
by mabufo
I know there was recently a similar thread posted - but instead of me hijacking his thread I have decided to start a new one. Keep in mind my problem is not the same either.

Ok, so I have a faily simple feedback form set up. This form takes the user's name, email, and message - and then sends them all to a specified email address. As far as I know, the code is fine - it's just that nothing is being sent to my inbox, and I am wondering if that has to do with the server I am hosted on... or if that my code is actually wrong.

Here is the form:

Code: Select all

<html>
<head>
  <title>Feedback</title>
</head>
<body>

<h1>Feedback</h1>

<p>Please tell us what you think.</p>

<form method=post action="processfeedback.php">

Your name: <br />
<input type=text name="name" size=40><br />

Your email address: <br />
<input type=text name="email" size=40><br />

Your feedback:<br />
<textarea name="feedback" rows=5 cols=30>
</textarea><br />

<input type=submit value="Send feedback">

</form>

</body>
</html>
here is "processfeedback.php"

Code: Select all

<html>
<head>
<title>Feedback Submitted</title>
</head>
<body>

<?php>
	//short variable names
	$name = $_POST['name'];
	$email = $_POST['email'];
	$feedback = $_POST['feedback'];
	
	//mail() function variables
	$toaddress = 'mabufo@gmail.com';
	$subject = 'Php form test';
	$mailcontent =	'Customer Name: '.$name. "\n"
			.'Customer email: '.$email. "\n"
			."Customer Feedback: \n".$feedback."\n";
	$fromaddress = 'From: webserver@example.com';

	//mails the form results
	mail($toaddress, $subject, $mailcontent, $fromaddress);
?>

<h1>Feedback Submitted.</h1>
<p>Your feedback has been shipped as is:</p><br />

<?php
	echo n12br($mailcontent);

?>
  
			

</body>
</html>
In addition to the problem of the mail not being sent - I have another question regarding the error handling behind forms. On any reputable website with a form - if you miss a required value, and click submit... the site will point you back to the form. I am wondering how I can get this same functionality in my mail() script. Could someone enlighten me to the best way of getting that sort of thing done?

Posted: Wed Jul 05, 2006 1:23 am
by RobertGonzalez
Your first problem may be solved by the use of headers in your email. While I am not the biggest fan of the PHP Mail function, if you are going to use it, check the manual page for it. There is a pretty decent sample code on that manual page that I have used before and has worked with no problems at all.

For your second problem, for checking user supplied data, I usually take the $_POST aray and read the components into individual vars. Then I check each var to make sure a) It is not empty, b) it is an acceptable type and c) it meets the criteria for the data being passed (regex email address, url, etc). If everything is cleared, then I process the form. If not, then I usually skip the form processing and continue to the form with all errors displayed to the user so they know what they did wrong.

Posted: Wed Jul 05, 2006 6:41 pm
by mabufo
What confuses me about the error handling is how I am supposed to display the form to the user a 2nd time.

Would it be like this?

Code: Select all

if(variable == bad){
     redisplay form 
     }

else{
     process form
     }
How exactly would I redisplay the form? Just copy and paste the thing into the script where appropriate? What about using an include?

EDIT: I've looked up the include function and that certainly did the trick. Here is the code.

Code: Select all

<html>
<head>
<title>Feedback Submitted</title>
</head>
<body>

<?php
	//short variable names
	$name = $_POST['name'];
	$email = $_POST['email'];
	$feedback = $_POST['feedback'];
	

	/* Detects erroneous input from the user...
	if an error is detected, the error will be displayed, 
	and the form will be shown to the user again...at
	which point the current php script is stopped
	to prevent erroneous input from being passed to 
	the lovely mail function
	 */
	
	//see 'Regular Expressions in PHP' (so complicated! EW!)
	if(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email))
		{
			echo 'Invalid email address!';

			include('feedback.php');			

			exit;

	} elseif (strlen($name) > 0)
		{
			echo 'Invalid Name!';
			
			include('feedback.php');

			exit;
	
	}elseif (strlen($feedback) > 0)
		{
			echo 'Invalid feedback!';

			include('feedback.php');
			
			exit;
		}
		 
	




	//mail() function variables
	$toaddress = 'mabufo@gmail.com';
	$subject = 'Php form test';
	$mailcontent =	'Customer Name: '.$name. "\n"
			.'Customer email: '.$email. "\n"
			."Customer Feedback: \n".$feedback."\n";
	$headers = 'From: webmaster@example.com'  . "\r\n" .
  		 'Reply-To: webmaster@example.com' . "\r\n" .
  		 'X-Mailer: PHP/' . phpversion();

		




	//mails the form results
	mail($toaddress, $subject, $mailcontent, $headers);



	//determines if the mail() function was succesful
	if (mail){
			echo "<h1>Feedback Submitted.</h1>";
			echo "<p>Your feedback has been shipped as is:</p><br />";


			echo nl2br($mailcontent);
		}
	
	else{
		echo 'Error Sending mail.';
		}
	
?>
  
			

</body>
</html>
Is there an even better way to get that same result? Also, I went with some simple error handling with the $name, and $feedback - however the book I am reading walked me through how to set up that regular expression for the email. I have to say, I'm still a little confused - but for the most part I understand what's going on within the regular expression.

One more thing, towards the bottom of my code - is the if statement with the mail function okay? While looking in the php manual I saw that the mail() function was a bool... so am I able to process it in an if statement like that?

Posted: Wed Jul 05, 2006 7:04 pm
by mabufo
I've gone and tested the code out a few times - and this part of the code seems to be causing trouble:

Code: Select all

elseif (strlen($name) > 0)
                {
                        echo 'Invalid Name!';
                       
                        include('feedback.php');

                        exit;
       
        }elseif (strlen($feedback) > 0)
                {
                        echo 'Invalid feedback!';

                        include('feedback.php');
                       
                        exit;
                }
I can leave the name and feedback section of the form blank - and it will go to the mail() function! Am I using strlen() correctly inside the if statement? Could I be using the elseif improperly?

EDIT: The code also echos the 'invalid name!' if I enter a letter.

Posted: Wed Jul 05, 2006 10:24 pm
by RobertGonzalez
That code is checking to see if the length of $name is greater than 0. If it is, you are telling it to fail and error out. Check out my comments...

Code: Select all

<?php
elseif (strlen($name) > 0) // If the length of the string $name is longer than zero characters...
{
    echo 'Invalid Name!'; // Tell the user that they entered an 'Invalid Name'
    include('feedback.php'); // Include the 'feedback.php' file
    exit; // Stop processing
} elseif (strlen($feedback) > 0) { // If the length of the var $feedback is longer than zero characters...
    echo 'Invalid feedback!'; // Tell the user they entered 'Invalid Feedback'
    include('feedback.php'); // Include the 'feedback.php' file
    exit; // Stop processing
} 
?>

Posted: Thu Jul 06, 2006 11:59 am
by mabufo
Eh, that's my fault. I think I need a break. :lol: