Forum Moderators: coopster

Message Too Old, No Replies

apache sends blank email? - php email form

php email form double mails recipient

         

iclaudius

1:54 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Dear PHP forum

I'm having some trouble with a php contact form on my site. It is based around a basic contact.php form which POSTS and redirects to a sendemail.php form with the variables. As a result, the webmaster receives 2 emails: 1 correct submission and 1 blank email from "Apache".

Here's some code snippetts. Any suggestions hugely appreciated! Cheers - p

-- CONTACT.PHP --

<FORM action="sendemail.php" METHOD="POST">

<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type=hidden name="ip" value="<?php echo $ipi?>">
<input type=hidden name="httpref" value="<?php echo $httprefi?>">
<input type=hidden name="httpagent" value="<?php echo $httpagenti?>">
Your Name<br>
<input name="name" type="text" class="text">
<br>Your Email<br>
<input type="text" name="email" class="text">
<br>Subject<br>
<input type="text" name="subject" class="text">
<br>Comment<br>
<textarea name="notes" cols="30" rows="6" wrap="VIRTUAL" id="comment" class="text"></textarea>
<br><br><input type="submit" name="submit" value="-send-" class="buttons"><br>
</form>


--SENDEMAIL.PHP--

<?php

$myemail = "WEBMASTER@EXAMPLE.COM";

if(!$email == "" && (!strstr($email,"@") ¦¦!strstr($email,".")))
{
echo "<a href='javascript:history.back();'>Go Back</a> and enter valid e-mail";
$badinput = "<br>Feedback was NOT submitted";
}
if(empty($name) ¦¦ empty($email) ¦¦ empty($notes )) {
echo "<a href='javascript:history.back();'>Go Back</a> - fill in all fields";
}

$todayis = date("l, F j, Y, g:i a") ;

$notes = stripcslashes($notes);
$notes = "Somebody has sent you an email from the website.\n
$notes \n
From: $name ($email)\n \n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
Message sent at $todayis [GMT]
";

$from = "From: $email\r\n";

if ($myemail!= "")
mail($myemail, $subject, $notes, $from);

?>

THANKS AGAIN!

Timotheos

4:28 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh good ol' mail problems.

First thing I'd try is using \n at the end of the from part of the header.
$from = "From: $email\r\n";
to
$from = "From: $email\n";
It may not matter put some mail program are finickity about \r\n and \n

If that does nothing then I'd start doing some process of elimination by putting strings right in the mail funtion.
mail("me@example.com", "test", "test", "From: Me Myself <me@example.com>");
If that works then try to figure out which variable is causing the grief.

jusdrum

5:15 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



It may be wise to look in the $_POST variable for your content that was posted via form.

$_POST['email'] instead of $email

iclaudius

1:50 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



Thank you both very much. Unfortunately, I made both recommended changes and the problem still exists. The new code:

<?php

$myemail = "user@example.com";

if(!$_POST['email'] == "" && (!strstr($email,"@") ¦¦!strstr($email,".")))
{
echo "<a href='javascript:history.back();'>Go Back</a> and enter valid e-mail";
$badinput = "<br>Feedback was NOT submitted";
}
if(empty($name) ¦¦ empty($email) ¦¦ empty($notes )) {
echo "<a href='javascript:history.back();'>Go Back</a> - fill in all fields";
}

$todayis = date("l, F j, Y, g:i a") ;

$notes = stripcslashes($notes);
$notes = "Somebody has sent you an email from the icarus website.\n
$notes \n
From: $name ($email)\n \n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
Message sent at $todayis [GMT]
";

$from = "From: $email\n";

if ($myemail!= "")
mail($myemail, $subject, $notes, $from);

?>

thanks in advance for any further help!
p

jatar_k

5:24 pm on Jan 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



instead of sending it, build the email and display all of the vars used in the mail call to the screen and comment out the mail call. That will confirm that the vars are being properly constructed.

Then start using the mail function again, if you have access to mail logs that would help as well, as it may give you more information. Also, take a look at the headers on the email you receive as that also may show you that a header or 2 is messed up.

also

$notes = stripcslashes($notes);
$notes = "Somebody has sent you an email from the icarus website.\n

the second line overwrites the content of the notes var. I think you may have wanted to use .=

jusdrum

6:40 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



Sorry, I should have been more clear. I would imagine (maybe I'm wrong) that all the vars you are checking are being posted by a form. So, you should change ALL the vars you are using to $_POST['varname'] instead of just $varname

For instance:

$name becomes $_POST['name']
$notes becomes $_POST['notes']

Any field from the form you are referencing should be referenced with $_POST, unless global vars are on (which is a very bad thing, and your script may break on servers that have global vars off).

Hope this helps!