RiCHiE
RiCHiE

Reputation: 288

PHP Basic email form all messages are from ( Unknown ) despite reply working

link to PHP File Link to HTML File

Ok so I am doing my first php form and I have tried Googling this and didn't have any luck (if anyone knows any good tutorials on basic php or php forms I have been looking at a lot of them and could use a good one).

Anyway basically I can't figure out why my emails get sent with from ( Unknown ) in Hotmail, even though when I go to reply I get the correct email put in.

Here is the HTML page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My First Basic Form</title>
</head>

<style type="text/css">
</style>

<!-- Form Opening Tage -->
<table width="600">
<tr><td>
<form method="post" name="form1" action="http://www.richie.id.au/TAFE/form        /php/form1_send.php"><br /><br />
<table width="600" border="5" bordercolor="#990000">

<!-- Name Field -->
<tr><td>Name:</td></tr>
<tr><td><input type="text" name="name" size="40" /></td></tr>

<!-- email Field -->
<tr><td>Email:<br /></td></tr>
<tr><td><input type="text" name="email" size="40" /><br /><br /></td></tr>

<!-- Message Field -->
<tr><td>Message: <br /></td></tr>
<tr><td><textarea name="message" width="100%" rows="12" cols="60" style="resize:none;">    </textarea><br /><br /></td></tr>

<!-- Sbumit Button -->
<tr><td><input type="submit" name="submit" /><br /><br /></td></tr>

</table>
</form>
</td>
</tr>
</table>

<body>
</body>
</html>

Here is the PHP page

<?PHP

// $name variable declared as name that was inputed into text field
$name = $_POST['name'];

// $email variable declared as email that was inputed into text field
$visitor_email = $_POST['email'];

// Email to show the message as coming from
$email_from = $_POST["$visitor_email"];

// Message text asigned to variable message
$message = $_POST['message'];

// Email subject
$subject = 'New form submission from richie.id.au';

// Email Body
$email_body = "New message from: $name.\n\n Message:\n\n $message\n";

// Email address to send form submission email to
$to = "[email protected]";

// headers
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

// Function that actually sends the mail
mail($to,$subject,$email_body,$headers);


?>

Upvotes: 2

Views: 520

Answers (2)

daiscog
daiscog

Reputation: 12077

$email_from = $_POST["$visitor_email"]; should be just $email_from = $visitor_email; or $email_from = $_POST['email'];

Although, why you have two PHP variables for the same thing, I don't know. You could even do this:

$email_from = "$name <$visitor_email>";

Although you may want to sanitise the $name and $visitor_email variables to make sure they are valid according to RFC 5322, section 3.4.

Upvotes: 2

Neelam Gahlyan
Neelam Gahlyan

Reputation: 358

Problem is in below line in your php code

// Email to show the message as coming from
$email_from = $_POST["$visitor_email"];

$email_from is blank and that is the reason for unknown sender.

Upvotes: 1

Related Questions