[SOLVED] Form processing using PHP mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Storm
Forum Newbie
Posts: 11
Joined: Tue Aug 31, 2004 12:30 am

[SOLVED] Form processing using PHP mail()

Post by Storm »

Hello all,

I have three forms as .php files where I set the $formname variable before sending the form action to "sendform.php"

Now, I have always used this method on my server (where Register Globals must be on because it works so easily). Now I am trying to email the form variables on a new client server and none of the variables get sent and the mail gets sent twice.

How do I send variables from a form to a mail.php page with Register Globals turned off?

I put the mail.php page on my server too and it works from there but doesn't work on the client server with the exact same file.

Code: Select all

<?php
mail("fake@emailaddy.com", "Online $formName ", "Hello, my name is $Name and I would like to make an online purchase. Please contact me for payment.\n\nBelow is my purchase request:\n\n$P1_howMany $P1_brand $P1_product $P1_size $P1_colour.\n\n$P2_howMany $P2_brand $P2_product $P2_size $P2_colour.\n\n$P3_howMany $P3_brand $P3_product $P3_size $P3_colour.\n\n$P4_howMany $P4_brand $P4_product $P4_size $P4_colour.\n\nHere is my contact information:\n\n$Name\n$Address\n$City\n$ProvState\n$Postal\n$Country\n\nI can be contacted by telephone at:\n$Telephone or $altTelephone\n\nComments:\n$Comments", "From: $Name\nReply-To: $email\nX-Mailer: PHP/" . phpversion());
?>
None of the variables come in the mail function. What else can I try. I am by no means a PHP Developer. Your help is appreciated and I don't mind making three different mail.php pages for the three different form pages if necessary. I am just trying to make it as easy as possible for the client to get the info from the form. I have all the text fields in the PHP form pages named properly.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Dont forget, variables are case sensitive!

Code: Select all

<?php

$message = "Hello, my name is $Name and I would like to make an ".
		"online purchase. Please contact me for payment.\n\n".
		"Below is my purchase request:\n\n$P1_howMany $P1_brand ".
		"$P1_product $P1_size $P1_colour.\n\n$P2_howMany $P2_brand ".
		"$P2_product $P2_size $P2_colour.\n\n$P3_howMany $P3_brand ".
		"$P3_product $P3_size $P3_colour.\n\n$P4_howMany $P4_brand ".
		"$P4_product $P4_size $P4_colour.\n\nHere is my contact ".
		"information:\n\n$Name\n$Address\n$City\n$ProvState\n$Postal".
		"\n$Country\n\nI can be contacted by telephone at:\n$Telephone".
		" or $altTelephone\n\nComments:\n$Comments"; 
		
$subject = "Online $formName";
$headers = "From: $Name\nReply-To: $email\nX-Mailer: PHP/" . phpversion();
mail('fake@emailaddy.com', $subject, $message);
?>
Storm
Forum Newbie
Posts: 11
Joined: Tue Aug 31, 2004 12:30 am

Post by Storm »

The form fields are exactly the same case-wise.

I put the mail() function call on my server and redirected them back to a thank you page as a temporary thing. I still can't get it to work on their server. If anyone comes up with something better, please let me know. It should not sit on my server in case they decide to move things later on. This is a bad temporary fix on my part.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Form processing using PHP mail()

Post by jayshields »

Storm wrote:Hello all,
How do I send variables from a form to a mail.php page with Register Globals turned off?
As far as that bit goes just send the form to a page with ?formid=blah at the end and use PHP $_GET array to fetch formid for you and use that to decipher which form is being sent.

If that is what you mean...?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: Form processing using PHP mail()

Post by bokehman »

Storm wrote:Hello all,
How do I send variables from a form to a mail.php page with Register Globals turned off?
Here's a temporary fix, just put it near the top of your script:

Code: Select all

foreach($_REQUEST as $k => $v){
     ${$k} = $v;
}
The proper way is to access the variable in super global array depending where it is coming from:

Code: Select all

$_POST['field_name'];
$_GET['field_name'];
$_COOKIE['field_name'];
$_SERVER['key_name'];
Storm
Forum Newbie
Posts: 11
Joined: Tue Aug 31, 2004 12:30 am

Post by Storm »

The proper way is to access the variable in super global array
Thanks bokehman. That helps.

So, my best method would be to redefine the variables to be sent as:

Code: Select all

$Name = $_POST['Name'];
$Address = $_POST['Address'];
$City = $_POST['City'];
before calling the mail function with the variables inserted? That makes sense. Thanks.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Or just use this: $_POST['Name'] as a direct replacement for this $Name throughout your script which save having a second set of unnecessary variables.
Storm
Forum Newbie
Posts: 11
Joined: Tue Aug 31, 2004 12:30 am

Post by Storm »

DONE. Mods can mark this answered.

Thanks everyone for your help.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form processing using PHP mail()

Post by John Cartwright »

bokehman wrote:

Code: Select all

foreach($_REQUEST as $k => $v){
     ${$k} = $v;
}
on a side note this can be accomplished by

Code: Select all

foreach($_REQUEST as $k => $v){
     $$k = $v;
}
its called variable.variables

or can even be done using

Code: Select all

extract($_REQUEST);
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Could you demonstrate extract($_REQUEST) in that context please?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

security notes about the above: doing either of those blindly, can lead to data injection. It's recommended to filter the variables during creation or before extraction so you only get the variables you expect.. This action should be apart of your tainted variable cleaning routines.
Post Reply