Forum Moderators: coopster

Message Too Old, No Replies

struggling to get my mail function working

         

malcolmcroucher

3:47 pm on Aug 3, 2008 (gmt 0)

10+ Year Member



Hi ,

I can send mail from my website except when I add information from a form.

The second I add user enter variables somethig goes wrong .

Perhaps you guys can help me

Here is the form :

<form name="input" action="email.php" method="post" enctype="text/plain">

Type your first name:
<br>
<input type="text" name="FirstName" value="Name" size="20" style="font-

family:verdana;font-size:80%;color:#000000">
<br>Type your last name:
<br>
<input type="text" name="LastName" value="Last Name" size="20"

style="font-family:verdana;font-size:80%;color:#000000">
<br>Email address
<br>
<input type="text" name="Email" value="email@email.com" size="20"

style="font-family:verdana;font-

size:80%;color:#000000" >
<br>Title
<br>
<input type="text" name="Title" value="Title" size="20" style="font-

family:verdana;font-size:80%;color:#000000" >
<br>
<textarea rows="10" cols="30" name="Message" style="font-family:verdana;font-size:80%;color:#000000" >Enter Message here </textarea>
<br>
<br>
<input type="submit" value="Submit">
<input type="submit" value="Reset">
</form>

and here is the adjoining php script :

<?php

$subject = "Test Message";

$name = $_POST["FirstName"];

$surname = $_POST["LastName"] ;

$email2 = $_POST["Email"] ;

$from = "FROM: $name $surname <$email2>";

//$from = "FROM: test <test@test.com>";

$email = "me@me.com";

//estmessage body

$body = $_POST["Message"] ;

if (!mail($email, $subject, $body, $from)) { echo "Error Sending Email!"; }

else

{ echo "Mail sent!"; }

///////////////////////////

// Finish sending emails //

//////////////////////////

?>

What I definately know is wrong is the $from , I have been messing around with a couple of variables . but its not even sending the body.

RonPK

5:24 pm on Aug 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure, but perhaps FROM needs to be spelled as From?

Another issue is that this script is vulnerable for email header injection. Evil folks could exploit it for spam runs. You really need to validate and sanitize user input before pasting it into mail params.

RonPK

5:26 pm on Aug 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="submit" value="Submit">
<input type="submit" value="Reset">

Surely you mean

<input type="submit" value="Submit">
<input type="reset" value="Reset">

:)

malcolmcroucher

8:13 am on Aug 4, 2008 (gmt 0)

10+ Year Member



okay so i tried this :

<form method="POST" action="email.php">

Name:
<br>
<input type="text" name="name" size="19"><br>
<br>
Email:
<br>
<input type="text" name="email" size="19"><br>
<br>
Message:
<br>
<textarea rows="9" name="message" cols="30"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {
$to = "blah@gmail.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>

And all i get is blarg does anyone know what the problem with the if(isset($_POST['submit'])) or the posting ?

Regards

Malcolm

omoutop

9:35 am on Aug 4, 2008 (gmt 0)

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



first try this:
if($_POST['submit']=="Submit")
{
// execute code
}

Second, your form submits data to a page called email.php.
Make sure you place the mail code in that page.

malcolmcroucher

10:09 am on Aug 4, 2008 (gmt 0)

10+ Year Member



okay ill try that now , thanks ...

yea i got the code in email.php

malcolmcroucher

10:15 am on Aug 4, 2008 (gmt 0)

10+ Year Member



I have figured out where the error is , its not in the mail function or the mail , its in the posting , if I take away if($_POST['submit']=="Submit") it send a mail without the posted information so my problem is to do with posting .

So i have the following questions :
1. Do you need any special settings to post

Regards

Malcolm

malcolmcroucher

11:25 am on Aug 4, 2008 (gmt 0)

10+ Year Member



okay i got it working , thanks for all the help ....

omoutop

1:52 pm on Aug 4, 2008 (gmt 0)

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



glad you managed to work things out.

Little info:
in case you have problems with submitted data try this trick...

echo "<pre>":
print_r($_POST);
echo "</pre>";

You will see all submitted data to your page via POST method.
This works also for GET, SESSION, COOKIE and many others.

Btw, what was the problem?