Log in

View Full Version : PHP form emailer



fileserverdirect
11-28-2007, 02:36 AM
Hello,
Sometimes I just don't get it. When I run it through a PHP debuger No syntax Errors found. No General Errors
But it doesn't work. Here is the script:


<?php
//PHP Form Mail - By fileserverdirect
$message="Hello, Your Website just Had another submisson at (".time()."), Here are the details:<br>\n";
foreach($_POST as $var -> $val)
{
if($var!="emailowner")
{
$var = ucfirst($var);
$message.="-----<br>\n".$var." : ".$val."<br>\n";
}
if($var=="emailowner")
{
$email = $val;
}
else if($var=="name")
{
$subj = "Message from " . $name;
}
}
/*$message = $_POST['mess'];
$email = $_POST['email'];
$name = $_POST['name'];*/
$sent="false";
if($_POST['submit'])
{
if(mail($email, $subj, $message, "From: \"Your Website\""))
{
echo "Message sent!";
$sent = "true";
}
else
{
echo "Error sending message.";
}
}
else
{
echo"Please fill out the form on this page.";
}
echo '<form action="mailit.php" method="post">
<input type="hidden" name="emailowner" value="me@myemail.com">
<input type="hidden" name="submit" value="true">
<textarea name="mess" rows=10 cols=30>
</textarea><br>
Email:<input type="text" name="email">
Name:<input type="text" name="name">
<input type="submit" value="Submit">
</form><br>';
?>
<!-- some other website stuff here-->
<?
if($sent=="true")
{
echo " The Message \"" . $message . "\" was sent to \"" . $email . "\"";
}
?>

Can someone show me what is wrong with this script?

djr33
11-28-2007, 02:48 AM
Seems ok. Hmm... are you using a godaddy windows server?

Best way to debug is have it output the message instead, and see if that's right. Then make a simpler page, to just check a simple mail function (no dynamic content, just strings), and see if that works.

phpinfo() might also tell you a bit more about your configuration.

Nothing jumped out at me as wrong, though I can't promise that's the case.

fileserverdirect
11-28-2007, 03:25 PM
Thats what I mean, No syntax errors, I think that it is getting stuck on the foreach-loop, but I don't know why. I think that it is sending the foreach loop into an endless loop, so if there mere only two things being submitted, t $message might look like this:


test1:hello
---
test2:hi
---
test1:hello
---
test2:hi
---
test1:hello
and so on.

Im not shure, This was just a guess.
I will do some more checks on it later, but I will see what you can come up with if you run the script.
---
No I do not use godaddy on a windows server.

djr33
11-28-2007, 07:05 PM
You have something like:
if ($a!=1) {
....
}
if ($a==1) {
...
}

You could just use an else for the second one.
Also, here, $subj = "Message from " . $name; has $name which was not defined.

Rethink some of that loop and you may find the problem as well.

[edit]in order to debug somethign like that, especially with loops, th easiest way is just to add checks along the way. see if the variables are what you expect.
echo $var;
print_r($array);
die('it got to line ##');

Use those logically, line by line, and you'll track it down within a couple minutes.

fileserverdirect
11-29-2007, 12:05 AM
I fainally got an error message out of my program:
Fatal error: Cannot access empty property in C:\my\folder\here\mailit.php on line 6
echo $message; does not print anywere but before the foreach-loop. Line 6 is the acutal foreach loop. But what does "Cannot access empty property" mean. This is the UPDTAED code:


foreach($_POST as $var -> $val)
{
if($var!="emailowner")
{
$var = ucfirst($var);
$message.="-----<br>\n".$var." : ".$val."<br>\n";
}
elseif($var=="emailowner")
{
$email = $val;
}
else
{
$subj = "Message from " . $val;
}
}

I don't get it:confused: :( :confused:

thetestingsite
11-29-2007, 12:48 AM
Try this:



foreach($_POST as $var => $val)


Hope this helps.

fileserverdirect
11-29-2007, 01:50 AM
Thanks, IT WORKED! -- for a whlie,
I don't know when because I added a couple of features, and now for whatever reason, the "emailowner", "submit", and "websitename" are being included in the email. But the email is never sent because emailowner (a hidden value) is never set. Why is the foreach loop totally bypassing this line?!?!?
if($var!="emailowner"||$var!="submit"||$var!="websitename")
?!?!:confused:
I just don't get it.

I figuerd it out:
if it was emailowner it was not submit so It went through with it. So now the line reads:
if($var!="emailowner"&&$var!="submit"&&$var!="websitename")