Jump to content

Problems With Php Mail() Function


Recommended Posts

hi guys.


I'm working on my frist PHP script and running into a few stumbling blocks (code below). The email is being sent successfully, but none of the variable data is populated. Here is the page http://www.sitesamurai.com/signupForm.php. Any help would be appreciated. Thanks! :thumbup:




>$name = $_REQUEST["userName"];
$teamName = $_REQUEST["teamName"];
$phone = $_REQUEST["phone1"] . "-" . $_REQUEST["phone2"] . "-" . $_REQUEST["phone3"];
$city = $_REQUEST["city"];
$state = $_REQUEST["state"];
$zipCode = $_REQUEST["zipCode"];
$country = $_REQUEST["country"];
$comments = $_REQUEST["comments"];

function PopulateMessage($name,$teamName,$phone,$city,$state,$zipCode,$country,$comments)
	$sMessage = '<html><head><title>User Inquiry</title></head><body><p>';
	$sMessage .= '<strong>name:</strong><br>' . $name + '<br><br>';
	$sMessage .= '<strong>team name:</strong><br>' . $teamName . '<br><br>';
	$sMessage .= '<strong>phone:</strong><br>' . $phone . '<br><br>';
	$sMessage .= '<strong>city:</strong><br>' . $city . '<br><br>';
	$sMessage .= '<strong>state:</strong><br>' . $state . '<br><br>';
	$sMessage .= '<strong>zip code:</strong><br>' . $zipCode . '<br><br>';
	$sMessage .= '<strong>country:</strong><br>' . $country . '<br><br>';
	$sMessage .= '<strong>comments:</strong><br>' . $comments . '<br><br>';
	$sMessage .= '</p></body></html>';
	return $sMessage;

//populate email message
$Message =  PopulateMessage($name,$teamName,$phone,$city,$state,$zipCode,$country,$comments);

//populate email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <xxx@xxxxx.com>' . "\r\n";

//send mail
mail("xxx@xxxxx.com","Test Email",$Message,$headers);

Link to comment
Share on other sites

The email is being sent successfully, but none of the variable data is populated. Here is the page


I figured out what was going on.


I was using a type="button" input instead of a type="submit". The page was not posting which would explain why the variables i created were never populated. This leads me to another question. Does anyone know of a function that exists in php to redirect from one page to another? I was not able to find this in any of the books i own. I would like to call this function and redirect to another page after the mail() function has been executed.





Edited by samporras
Link to comment
Share on other sites

>header("Location: http://www.example.com/"); /* Redirect browser */


but you can't output anything first, not even a space in the code


Thanks Don :thumbup:


I'm not 100% sure i follow what you're saying. What would i want to output when redirecting from one page to the other?

Link to comment
Share on other sites

It's usually unintentional output that messes you up. For instance, a blank line above the opening "<?php" tag or in an included file. When Apache parses the page it will go ahead and send the headers and blank line before getting to the php header code. In this case, php will complain that the headers have already been sent.

Link to comment
Share on other sites

Two quick things. You can address some of these header issues by putting this at the very beginning of the php code (still can't have space or line break or any output before <?php)




The other thing is that you should consider replacing $_REQUEST with $_POST (or $_GET). It's more specific, and therefore more secure.

Link to comment
Share on other sites

Just some minor suggestions with your code


1 - Always use single quotes around strings and not doubles. This is because PHP reads through double quoted strings looking for variables everytime it comes cross it. Same applies to array keys. You only need double quotes when using special characters such as \n \t etc.


2 - use "NAME" <email@address.com> to add a more personal touch to your emails (I know you are only sending to yourself right now).

Link to comment
Share on other sites

I would also change this line:


>mail("xxx@xxxxx.com","Test Email",$Message,$headers);


to read like this:


>mail("xxx@xxxxx.com","Test Email",$Message,$headers  "-f".$myemail);


Set up $myemail to a valid from email address.


Thanks Bruce. i thought i took care of that when i set up my $headers variable (line 4 of the below code). Is there a preferred way to declare a "from" email address?


>//populate email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <xxx@xxxxx.com>' . "\r\n";

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...