Welcome to WebmasterWorld Guest from 54.242.193.41

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to fix error in "Contact Me" e-mail script?

PHP beginner needs help

     
1:45 am on Apr 3, 2018 (gmt 0)

Junior Member from CA 

Top Contributors Of The Month

joined:July 9, 2017
posts: 47
votes: 5


My hosting company uses cPanel and I am getting a strange PHP error message in my error log whenever someone sends me an e-mail from my html Contact page. The page is simple using a simple PHP script to originate an e-mail to me. I am "no whiz" at PHP and don't know how to eliminate the error.

The error message says :
Undefined variable: email in /home/example/public_html/phpscript.php on line 26


The relevant lines in the phpscript.php script are :
10 <?php>
11 if (isset($_POST['send'])) {
12 $to = 'info@example.com';
13 $subject = 'Contact request';
14 $message = 'Email: ' . $_POST['email'] . "\r\n\r\n";
15 $message .= 'Subject: ' . $_POST['subject'] . "\r\n\r\n";
16 $message .= 'Comments: ' . $_POST['comments'];
17 $headers = "From: webmaster@example.com\r\n";
18 $headers .= 'Content-Type: text/plain; charset=utf-8';
19 $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
20 if ($email) {
21 $headers .= "\r\nReply-To: $email";
22 $success = mail($to, $subject, $message, $headers, '-finfo@example.com');
23 }
24 }
25
26 if ($email) { ?>
27
28 <h1>Thank you blah blah.</h1>
29 <?php
30 } else { ?>
31 <h1>Ooops! blah blah.</h1>
32 <?php }


Standard script I borrowed from only Heaven remembers where. But I have done something wrong with it and can't figure out what it is. It 'seems' to work fine but I keep getting this error message.

Thanks for any help.
2:06 am on Apr 3, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15705
votes: 812


Count the { braces } and you'll see that line 26 is outside the loop that defines $email, so your script will encounter this line whether or not $email has in fact been defined. You could change it to an ISSET formulation, but you're right, this tends to be a non-lethal error.

:: snickering a bit at \r\n, which helps narrow down the possible sources of the script ::
8:26 am on Apr 3, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


I assume (but you'll want to check) that the mail() function returns true/false, so why not use that? Checking $email only verifies if it's a correctly formatted e-mail address.

Something like:
if(isset($success) && $success === true) {

This will output the "Oops" message even if $email isn't set.

If you want to provide a more helpful error message, you should probably add in a few extra conditions.
9:08 pm on Apr 3, 2018 (gmt 0)

Junior Member from CA 

Top Contributors Of The Month

joined:July 9, 2017
posts: 47
votes: 5


Thank you, lucy24, my gratitude exceeds all but my embarrassment. At my age and experience with other things, I should have caught that. And you are probably right about the source.

And robzilla, thank you for the suggestions. I will definitely look into them. And a more diagnostic error message would certainly be helpful.

Thanks again, both of you. And I now see something else in the script as quoted that I might have gotten wrong. Back to the book! The learning never ends.
9:09 pm on Apr 3, 2018 (gmt 0)

Junior Member from CA 

Top Contributors Of The Month

joined:July 9, 2017
posts: 47
votes: 5


Thank you, lucy24, my gratitude exceeds all but my embarrassment. At my age and experience with other things, I should have caught that. And you are probably right about the source.

And robzilla, thank you for the suggestions. I will definitely look into them. And a more diagnostic error message would certainly be helpful.

Thanks again, both of you. And I now see something else in the script as quoted that I might have gotten wrong. Back to the book! The learning never ends.
5:34 pm on Apr 4, 2018 (gmt 0)

Junior Member from CA 

Top Contributors Of The Month

joined:July 9, 2017
posts: 47
votes: 5


Repeating the message was done, of course, to emphasize my own incompetence. I trust the point has been made.
6:04 pm on Apr 4, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15705
votes: 812


Well, better than a duplicated post saying “Yes, thank you lucy, I can see that, now come up with something useful” ;)

In theory it should always be possible to write code that doesn't throw errors. But some of those “errors” are not far removed from the “errors” that GSC is so fond of reporting: not really a problem, just thought you’d like to know.