Newbie Mail Question

the mail() function

Moderators: macek, egami, gesf

gbrown
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Mar 17, 2003 5:00 pm

Newbie Mail Question

Postby gbrown » Mon Mar 17, 2003 6:00 pm

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: Select all

<?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;

?>

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Mon Mar 17, 2003 7:25 pm

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: Select all

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

gbrown
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Mar 17, 2003 5:00 pm

Postby gbrown » Tue Mar 18, 2003 3:44 am

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: Select all

<?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.

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Tue Mar 18, 2003 1:17 pm

No problem, I'm glad it worked :D

gbrown
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Mar 17, 2003 5:00 pm

In the end...

Postby gbrown » Wed Mar 19, 2003 1:50 pm

FYI, if you're interested that is...

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

Code: Select all

<?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

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Mar 19, 2003 7:46 pm

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.

xeno
New php-forum User
New php-forum User
Posts: 2
Joined: Mon May 02, 2005 2:06 pm
Contact:

Postby xeno » Fri Jul 29, 2005 12:47 pm

you can see examples of the php mail at http://flashmailforms.com and use the email forms free


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest