Newbie Mail Question

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
Quick question for someone new to PHP. I have a pretty good background in ColdFusion, and was able to piece this together from my CF knowledge and what's on the php.net web site, but it's not working the way I'd like. Any ideas on what's wrong?

Thanks very much in advance.

The following script processes data "message," "email," and "name" posted from a simple form.

Code:
<?php

$message = "<?php echo $_POST["message"]; ?>";
$header = "From: <?php echo $_POST["email"]; ?>";
$name = "<?php echo $_POST["name"]; ?>";

mail ("me@myhost.com", "New Website Contact", $message, $header);

{print "Thank you, $name, for your feedback.\n";}

exit;

?>
 
First off you should use $_POST if your getting it by a form, especially since more and more servers are forcing it to only work that way (ex: $_POST['message'] instead of $message). Second, you can't put PHP code in an email, PHP code must be parsed on the server. Lastly, your $header is supposed to be headers like who the email is from, where its going, etc... refer to http://www.php.net/manual/en/function.mail.php for a bit more information on the headers. Also, exit isn't necessary unless you have more code that you don't want to be processed :)

Try this:
Code:
<?php 
mail("me@myhost.com", "New Website Contact", $_POST['message']); 
print "Thank you, ".$_POST['name'].", for your feedback.\n";
?>
 
Xerpher-

Your script worked beautifully as posted. I guess I was looking at too many scripts from too many places. The way you boiled it down was just what I was looking for.

I modified it to the following:

Code:
<?php 

$headers .= "From: ".$_POST['name']."<".$_POST['email'].">\n";
$headers .= "X-Sender: <".$_POST['email'].">\n";
$headers .= "X-Mailer: PHP\n"; //mailer
$headers .= "Return-Path: <".$_POST['email'].">\n";

mail("me@myhost.com", "New Website Contact", $_POST['message'], $headers); 
print "Thank you, ".$_POST['name'].", for your feedback.\n";
 
?>


Now to tackle required fields... 8O

Thanks VERY much for your help.
 
FYI, if you're interested that is...

Some really simple required field work came out as the following:

Code:
<?php
if ($_POST['email'] == "") {
print "<font size=\"+1\" color=\"red\">You must include your E-Mail Address.</font>\n";
include( "form.inc" );

         } else { 

$headers .= "From: ".$_POST['name']."<".$_POST['email'].">\n"; 
$headers .= "X-Sender: <".$_POST['email'].">\n"; 
$headers .= "X-Mailer: PHP\n"; //mailer 
$headers .= "Return-Path: <".$_POST['email'].">\n"; 

mail("me@myhost.com", "New Website Contact", $_POST['message'], $headers); 
print "Thank you, ".$_POST['name'].", for your feedback.\n"; 
}
?>

Where "form.inc" is the simple HTML form.

I'm posting it in the hope that it will help some other newbie to come along with a similar issue. Hope it helps. :D
 
Your getting these because you have E_ALL enabled in your php.ini file which shows every single error, and although not fatal, that code has errors...

$headers .= "From: ".$_POST['name']."<".$_POST['email'].">\n";
$headers .= "X-Sender: <".$_POST['email'].">\n";
$headers .= "X-Mailer: PHP\n"; //mailer
$headers .= "Return-Path: <".$_POST['email'].">\n";

In this first section, romove the first .= like so...

$headers = "From: ".$_POST['name']."<".$_POST['email'].">\n";
$headers .= "X-Sender: <".$_POST['email'].">\n";
$headers .= "X-Mailer: PHP\n"; //mailer
$headers .= "Return-Path: <".$_POST['email'].">\n";

This is because .= tell php to add to $headers but $headers doesn't exist yet.

mail("me@myhost.com", "New Website Contact", $_POST['message'], $headers);

The error you showed basically means you didn't use the code right (he said you need to make an html form to use this) and that the form variable 'message' was never defined.
 
Back
Top