Click to See Complete Forum and Search --> : Variable passing with mail() function


mg8
09-13-2003, 07:07 PM
<?php;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to="k@yahoo.com";
$subject="blah
$message="{$_POST['varA']}, and, {$_POST['varB']}";
mail($to, $subject, $message, $headers);
?>

I get the email, but the variables aren't included :(

Jona
09-13-2003, 08:56 PM
Originally posted by mg8
$subject="blah


Missing and end-quote, are we not?

[J]ona

mg8
09-14-2003, 07:40 AM
Not in the original script. I just forgot to type it there. :)

pyro
09-14-2003, 08:39 AM
I'm assuming this is the same problem you are having here: http://forums.webdeveloper.com/showthread.php?s=&postid=91409

mg8
09-14-2003, 09:08 AM
Yes sir.

I put it here also because I didn't know if you looked in all forums.

Jona
09-14-2003, 12:13 PM
Originally posted by mg8
Not in the original script. I just forgot to type it there. :)

Is that the only difference in what you posted here and the "original" script?

[J]ona

gizmo
09-14-2003, 01:45 PM
I have been battling all day with yhe code to e-mail a form and have stripped out the extras to debug it and it is now similar to one on my site that works, except that the variables aren't being read.
<?
$name=trim($_POST["name"]);
$address=trim($_POST["address"]);
$postcode=trim($_POST["postcode"]);

$toaddress="info.man@ntlworld.com";
$subject="Profile from FPS";
$mailcontent="Customer name: ".$name."\n"
."Address: ".$address."\n"
."Postcode: ".$postcode."\n";

mail($toaddress, $subject, $mailcontent);
?>
Can anyone spot what's wrong ?

pyro
09-14-2003, 01:51 PM
Are you getting any email? Also, are you sure you are using the POST method with the form? Any error messages?

AdamBrill
09-14-2003, 01:53 PM
This:<?php;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to="k@yahoo.com";
$subject="blah
$message="{$_POST['varA']}, and, {$_POST['varB']}";
mail($to, $subject, $message, $headers);
?> Should be this:<?php;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to="k@yahoo.com";
$subject="blah";
$message="{".$_POST['varA']."}, and, {".$_POST['varB']."}";
mail($to, $subject, $message, $headers);
?> NOTE:If it still doesn't work right, make sure that you named the post variables correctly.

mg8
09-14-2003, 02:05 PM
Crap.

Now, I'm not getting any email.

I'll take a looksee and hopefully locate the problem.

gizmo
09-14-2003, 02:11 PM
Hi Pyro,
I get the error message "Undefined index: name" and similar for each variable. I get the e-mail ok (less variables). The fact that the error says 'index' is curious, as if it thinks it is an array.

pyro
09-14-2003, 02:17 PM
It is an array. $_POST is an array of all the post variables. So, from the error message you are getting we can conclude that either a) your form does not use the post method, or b) you don't have any fields named with the names you are trying to use in your PHP script. Post the form that you are using...

mg8
09-14-2003, 02:18 PM
I get an unknown error.

Everthing is correct :confused:

I think the error is here:

$message="{".$_POST['varA']."}, and, {".$_POST['varB']."}";

or it is here:

mail("blah@yahoo.com", $subject, $message, $headers);

:confused:

pyro
09-14-2003, 02:22 PM
The exact error message would be helpful...

mg8
09-14-2003, 02:24 PM
Warning: Unknown error in C:\apache\htdocs\funny.php on line 456...

pyro
09-14-2003, 02:26 PM
and now, which line is 456? Also inlcude a few lines above and below it...

mg8
09-14-2003, 02:32 PM
mail($to, $subject, $message, $headers);

That is the line.

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to="blah@yahoo.com";
$subject="{".$_POST['varA']."}";
$message="{".$_POST['varA']."}";
$message .="{".$_POST['varB']."}";
$message=stripslashes($message);

This is before it.

Form:

<form action="process.php" method="post">
<input type="text" value="" name="varA">
<input type="text" value="" name="varB">

That is the part where they input they stuff.

I do not wish to post my entire script because I feel that it is not necessary.

pyro
09-14-2003, 02:36 PM
Hmm... it looks like it might be a server problem. Does it support mail()? Does this work?

mail("you@your.com","test subject","test message");

mg8
09-14-2003, 02:39 PM
Hmm..I am using my local machine while having Apache running..That might be the prob.

I tried writing a basic mail script (like you did) and it gave same error. I will try uploading to my ftp and see what happens.

mg8
09-14-2003, 02:42 PM
Alright. I got the email, but still no variables :(

pyro
09-14-2003, 02:42 PM
That is definitly the problem, if you don't have a mail server installed...

pyro
09-14-2003, 02:43 PM
Try echoing $message to the page...

mg8
09-14-2003, 02:49 PM
Alright.

I'm gonna mess with it for a little while and check back in later to let you guys know.

gizmo
09-14-2003, 02:59 PM
Sorry for th delay, my machine crashed. The code for the form is rather lengthy, but the relevent lines are as follows:

<FORM action="profile.php" method="post" enctype="text/plain">
<INPUT class="forms" NAME="name" TYPE="text" SIZE=15 maxlength=25><BR>
Address: <TEXTAREA class="forms" name="address" cols="48" rows="4" wrap="virtual"></TEXTAREA>
Post Code: <BR><INPUT class="forms" NAME="postcode" TYPE="text" SIZE=15 maxlength=10>
<INPUT class="go" NAME="Booking" type="submit" value="Submit">
</FORM>

mg8
09-14-2003, 03:15 PM
Ok. I think I got the problem.

For some reason, when the document loads, it sends the email right away, instead of waiting for the user to actually input the variables.

I tried putting the mail() in a function itself and then calling it when you clicked submit but that didn't work.

pyro
09-14-2003, 03:57 PM
Make sure that a) either the forms action points to a new page or b) you have a bit of code in there set to detect whether the form has been submitted or not, yet...

mg8
09-14-2003, 04:08 PM
I don't think I put a detection one in...

How would I go about doing that?

pyro
09-14-2003, 04:13 PM
if (count($_POST) > 0) {
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to="blah@yahoo.com";
$subject="{".$_POST['varA']."}";
$message="{".$_POST['varA']."}";
$message .="{".$_POST['varB']."}";
$message=stripslashes($message);
mail($to, $subject, $message, $headers);
}What that does is checks if the $_POST array is greater than 0 (contains content) and if so, mails the form...