Jump to content

[SOLVED] Help with PHP Email SCript


bloodlinek0

Recommended Posts

Hi guys,

 

I'm a newbie to php and need help with the script below:

 

At the moment when I receive the automated email of data, I'm not receiving any information with the form field names I receive blank info and the html markup

eg:

 

Name: <br>

Company: <br>

 

and so on.

 

Also how can I make the from field in the email sent to me display as that of the user who has submitted the form and included his email in the form.

 

Site: http://experiment.treadeverywhere.com/itmb/contact.html

Script below:

 

 

 

<?php

/* Subject and Email Variables */

$emailSubject = 'Crazy PHP Scripting!';
$webMaster = 'dazan@treadeverywhere.com';

/*Gathering Data Varialbes */

$forenameField = $_POST['forename'];
$companyField = $_POST['company'];
$positionField = $_POST['position'];
$addressField = $_POST['address'];
$cityField = $_POST['city'];
$potscodeField = $_POST['postcode'];
$telephoneField = $_POST['telephone'];
$emailField = $_POST['email'];
$businessmobilesField = $_POST['businessmobiles'];
$blackberrysolutionsField = $_POST['blackberrysolutions'];
$otherField = $_POST['other'];
$timescaleField = $_POST['timescale'];

$body = <<<EOD
<br><hr><br>
Name: $forename <br>
Company: $company <br>
Postion: $position <br>
Address: $address <br>
City: $city <br>
Postcode: $postcode <br>
Telephone: $telephone <br>
Email: $email <br>
Business Mobiles: $businessmobiles <br>
Blackberry Solutions: $blackberrysolutions <br>
Other: $other <br>
Timescale: $timescale <br>
EOD;

$success = mail($webMaster, $emailSubject, $body);

/*Results Rendered as html*/

$theResults = <<<EOD
<html>

	<head>

	 <meta name="description" content="" />
	  <meta name="keywords" content="" />
	   <meta name="author" content="Dazan Alyanai" />
	    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

		<title>Unsupported Browser</title>

			<link rel="shortcut icon" HREF="favicon.ICO">


		   <style type="text/css">
		     <!--

	* {margin: 0; padding: 0}

	body {font-size: 62%;
	background-color: #666666;
	font: 1em Arial, Verdana; 
	color: #333333;
	}

	#wrapper {margin: 10em auto;
	padding-top: 0em;
	position: relative;
	padding: 1em;
	width: 400px;
	height: 200px;
	background-color: #999999;

	}
	      --> 
		   </style>

	</head>
	 <body>
	  <div id="wrapper">
		<h1>! Unsupported Browser !</h1>
		 <br>
		 <p>This Browser has known compatibility issues with modern web standards.<br><br>We recommend installing or updating to one of the following browsers.</p>
		   <br>
	        <a href="http://www.mozilla.com/en-US/products/download.html?product=firefox-3.5.3&os=osx&lang=en-US" style="float: right">Firefox</a><a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">IE 7+</a>
	  </div>

	 </body>

</html>
EOD;
echo "$theResults";



?>

 

Thanks in advance.

 

Regards

Link to comment
Share on other sites

I'm no expert but this might help...the script will do what you need to, but it's a little messy. Hopefully one of the php guru's can help put this right.

:D

 

Just to add but as far as I know  :-\  the <BR> in your code will not be valid, you will need to use \n or echo

 

Example: 

echo "<br>";

 

 

 

 

Contact.html

 
<form method="post" action="http://www.domainname.net/contact.php">
<table width="375">
<tr>
<td width="357">
<div align="justify">
<input type="text" name="name" size="38" /></td>
</tr>
</table>
                 
<table width="375">
<tr>
<td width="357">
<input type="text" name="email" size="38" />
</td>
</tr>
</table>

....etc, etc.... 

<table width="375">
<tr>
<td width="357">
<textarea rows="9" name="message" cols="30"></textarea> 
<input type="submit" value="Send" name="submit" />
</td>
</tr>
</table>
</form>

 

 

Contact.php

<?php 
if(isset($_POST['submit'])) { 
$to = "mail@domainname.net"; 
$subject = "Web Query"; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone'];
$message = $_POST['message']; 
  
$body = "From: $email\n\nName: $name\n\nPhone Number: $phone\n\nMessage:\n$message";  
  
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.domainname.net/contact_thanks.html\">";

mail($to, $subject, $email, $body); 
} else { 
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.domainname.net/contect_error.html\">";
} 
?>  

Link to comment
Share on other sites

Hey Thanks for that, appreciate the alternative code but as I'm still learning I'd like to be able to modify the code I have to get it to work so I'm also learning as well as fixing  :P.

 

But I guess if I don't get any feedback I'll have to go with the script example you've posted for me.

 

Regards

Link to comment
Share on other sites

Just been looking though your code and your Variables have Field at the end...

 

$forenameField

 

but in your display, it has the following...

 

$forename

 

:confused:

 

There is no variables call $forename ...so no data can be retrived?? try changing to this to $forenameField and add the Field part to the rest.

 

 

Give it a try it might work?

 

:-\

 

Link to comment
Share on other sites

Due to the quizzical way in which iQue suggested the fix I thought I'd confirm/clarify. At the top you are saving all the $_POST variables in $somethingField whereas when you construct the e-mail you are using just $something, if you substitute all those values it should work (providing you have no other issues).

Link to comment
Share on other sites

<?php

/* Subject and Email Variables */

$emailSubject = 'Crazy PHP Scripting!';
$webMaster = 'dazan@treadeverywhere.com';

/*Gathering Data Varialbes */

$forename = $_POST['forename'];
$company = $_POST['company'];
$position = $_POST['position'];
$address = $_POST['address'];
$city = $_POST['city'];
$potscode = $_POST['postcode'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$businessmobiles = $_POST['businessmobiles'];
$blackberrysolutions = $_POST['blackberrysolutions'];
$other = $_POST['other'];
$timescale = $_POST['timescale'];

$body = <<<EOD
<br><hr><br>
Name: $forename <br>
Company: $company <br>
Postion: $position <br>
Address: $address <br>
City: $city <br>
Postcode: $postcode <br>
Telephone: $telephone <br>
Email: $email <br>
Business Mobiles: $businessmobiles <br>
Blackberry Solutions: $blackberrysolutions <br>
Other: $other <br>
Timescale: $timescale <br>
EOD;

mail($webMaster, $emailSubject, $body);

/*Results Rendered as html*/

$theResults = <<<EOD
<html>

	<head>

	 <meta name="description" content="" />
	  <meta name="keywords" content="" />
	   <meta name="author" content="Dazan Alyanai" />
	    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

		<title>Unsupported Browser</title>

			<link rel="shortcut icon" HREF="favicon.ICO">


		   <style type="text/css">
		     <!--

	* {margin: 0; padding: 0}

	body {font-size: 62%;
	background-color: #666666;
	font: 1em Arial, Verdana; 
	color: #333333;
	}

	#wrapper {margin: 10em auto;
	padding-top: 0em;
	position: relative;
	padding: 1em;
	width: 400px;
	height: 200px;
	background-color: #999999;

	}
	      --> 
		   </style>

	</head>
	 <body>
	  <div id="wrapper">
		<h1>! Unsupported Browser !</h1>
		 <br>
		 <p>This Browser has known compatibility issues with modern web standards.<br><br>We recommend installing or updating to one of the following browsers.</p>
		   <br>
	        <a href="http://www.mozilla.com/en-US/products/download.html?product=firefox-3.5.3&os=osx&lang=en-US" style="float: right">Firefox</a><a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">IE 7+</a>
	  </div>

	 </body>

</html>
EOD;
echo "$theResults";



?>

Link to comment
Share on other sites

Ok, if that's giving blank values, try putting this at the top of the page.

 

echo '<pre>';
print_r($_POST);
echo '</pre>';

 

To see what the $_POST array contains, sounds abit like it's empty. The HTML tags being output is probably down to the header of the e-mail, it's probably being sent as plain text. To fix that replace your call to mail() with this...I'm not sure if both headers are technically required, but it's what I've used in the past. It should solve that problem.

 

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=us-ascii\r\n"; 

mail($webMaster, $emailSubject, $body, $headers);

Link to comment
Share on other sites

It’s just speculation but I just looked at some of the HTML code from your site, and I think it has something to do with that...not sure what??  ...I assume that's what you are using.

 

I tested your PHP script (modified) to suit my HTML code and it works.  So it could be something to do with your field names on the text input boxes.

 

 

<?php

/* Subject and Email Variables */

$emailSubject = 'Crazy PHP Scripting!';
$webMaster = 'your@email.net;

/*Gathering Data Varialbes */

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message']; 


$body = <<<EOD
             Name: $name
             Email: $email 
             Phone: $phone 
             Message: $message 

EOD;


mail($webMaster, $emailSubject, $body);

/*Results Rendered as html*/

$theResults = <<<EOD
<html>

	<head>

	 <meta name="description" content="" />
	  <meta name="keywords" content="" />
	   <meta name="author" content="Dazan Alyanai" />
	    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

		<title>Unsupported Browser</title>

			<link rel="shortcut icon" HREF="favicon.ICO">


		   <style type="text/css">
		     <!--

	* {margin: 0; padding: 0}

	body {font-size: 62%;
	background-color: #666666;
	font: 1em Arial, Verdana; 
	color: #333333;
	}

	#wrapper {margin: 10em auto;
	padding-top: 0em;
	position: relative;
	padding: 1em;
	width: 400px;
	height: 200px;
	background-color: #999999;

	}
	      --> 
		   </style>

	</head>
	 <body>
	  <div id="wrapper">
		<h1>! Unsupported Browser !</h1>
		 <br>
		 <p>This Browser has known compatibility issues with modern web standards.<br><br>We recommend installing or updating to one of the following browsers.</p>
		   <br>
	        <a href="http://www.mozilla.com/en-US/products/download.html?product=firefox-3.5.3&os=osx&lang=en-US" style="float: right">Firefox</a><a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">IE 7+</a>
	  </div>

	 </body>

</html>
EOD;
echo "$theResults";



?>

 

 

Link to comment
Share on other sites

I've done that and now my email comes through correctly and the html markup is treated as html markup and not displaying as text.

 

I'm one step closer! Was going to give up after this aswell.

 

Now all I need to do is display the results (which are still not showing)

 

the actual html markup can still be found at:

 

http://experiment.treadeverywhere.com/itmb/contact.html

 

I doubt very much something has gone wrong there but maybe I've overseen something that is required for the php to work.

 

Rgds

Link to comment
Share on other sites

Ok, looking at the form I can see that you have the action set to contactformprocess.php, so I assume the contents you posted originally is stored in the file contactformprocess.php? Did you try putting these lines...

 

echo '<pre>';
print_r($_POST);
echo '</pre>';

 

... at the top of your page and try submitting the form, It should show you the contents of the $_POST array.

Link to comment
Share on other sites

Hi cags,

 

yes this is true, the php file is called that, I did enter that bit of code on the top and it has made a difference in actually rendering the html as html (ie showing hr instead of displaying the text <hr> but the actual data itself is not displaying.

 

Id its working for others it must be my html but as far as I was aware when defining the variable the name in the [] was the name given to the field in the html and this is what I have done.

 

Is that correct?

 

Also after entering the snippet of code you provided,  the results page shows array () with no data in it.

Link to comment
Share on other sites

If thats all you got then it means the $_POST array is empty. Which means the problem is not with this script, the problem is something to-do with the posting of the data. Looking at your HTML in the link you provided I can see that you have set the action and the method attributes correctly. So the problem seems very strange, it's possible the JavaScript is causing an issue, try removing onSubmit="return ValidateForm(contactform); from the <form> node in the HTML and see if it makes a difference.

Link to comment
Share on other sites

Got it!

 

it was the

 

enctype="text/plain"

 

that needed to be removed from the html. Who would have thought three words could cause so much grief.

 

Anyway one last thing and you'll be glad to know it's nothing to do with debugging it's to do with how do i do it :P

 

My checkboxes are rendered as on in my email which is showing me what is checked and what isn't but the radio box field is blank, how do i display what has been selected from the radio box?

 

 

Link to comment
Share on other sites

I did consider mentioning I'd never use enctype="text/plain", but I dismissed it as being harmless, just goes to show I guess. With regards to checkboxes on a form. I believe they only appear in the $_POST array if it is checked. And it will appear in the array under the index/key of the name attribute of the <input> node with the value of the value attribute.

Link to comment
Share on other sites

  • 2 weeks later...

I'm Back :P

 

I'm working on radio buttons now and I can't seem to get them to pass through to the email.

 

I've looked at a few tutorials and still none the wiser.

 

Basically I need to know what to do to pass data through via php from my form for radio buttons. If you need the php and html again I can repost it, but a good explanation and example of how it should be done will (hopefully) suffice.

 

Thanks again.

 

 

Link to comment
Share on other sites

How would that fit into the php file that transfers the data via email.

 

When I add the code it just echoes the results on the confirmation page but nothing gets sent through to the email.

 

The name of the radio button is Timescale.

 

<?php

/* Subject and Email Variables */

$emailSubject = 'ITMB Form Submission';
$webMaster = 'bloodlinek0@yahoo.com';

/*Gathering Data Varialbes */

$forename = $_POST['forename'];
$company = $_POST['company'];
$position = $_POST['position'];
$address = $_POST['address'];
$city = $_POST['city'];
$potscode = $_POST['postcode'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$businessmobiles = $_POST['businessmobiles'];
$blackberrysolutions = $_POST['blackberrysolutions'];
$other = $_POST['other'];
$timescale = $_REQUEST['timescale'];
$comments = $_POST['comments'];

$body = <<<EOD
<br><hr><br>
Name: $forename <br>
Company: $company <br>
Postion: $position <br>
Address: $address <br>
City: $city <br>
Postcode: $postcode <br>
Telephone: $telephone <br>
Email: $email <br>
Business Mobiles: $businessmobiles <br>
Blackberry Solutions: $blackberrysolutions <br>
Other: $other <br>
Timescale: $timescale <br>
Comments: $comments <br>

EOD;

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=us-ascii\r\n"; 

mail($webMaster, $emailSubject, $body, $headers);

/*Results Rendered as html*/

$theResults = <<<EOD
	<html>

	<head>

		 <meta name="description" content="" />
		  <meta name="keywords" content="" />
		   <meta name="author" content="Dazan Alyanai" />
		    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

		<title>ITMB</title>

			<link rel="shortcut icon" HREF="favicon.ICO">

			<script type="text/javascript" src="assets/flash/swfobject.js"></script>
			<script type="text/javascript">
				var flashvars = {};
				var params = {};
				params.menu = "false";
				params.quality = "best";
				params.scale = "noscale";
				var attributes = {};
				swfobject.embedSWF("assets/flash/logo.swf", "logo", "341", "86", "9.0.0", "assets/flash/expressInstall.swf", flashvars, params, attributes);
			</script>


	    <link rel="stylesheet" type="text/css" href="styles.css">
	     <style type="text/css">
		     <!--	 		 

			 -->
		   </style>

	</head>
	 <body>
	  <div id="wrapper">

	   <div id="head">

		<div id="nav" style="float: right">
		 <ul> 
				<li class="link"><a href="index.html">Home</a></li>
				<li class="link"><a href="about.html">About Us</a></li>
				<li class="link"><a href="web.html">Web</a></li>
				<li class="link"><a href="plans.html">Plans</a></li>
				<li class="link"><a href="partnerships.html">Partnerships</a></li>
				<li class="link" id="active"><a href="contact.html">Contact</a></li>

		 </u>
		</div>
			<div id="logo">
				<img src="assets/images/logo.gif" alt="ITMB" />
			</div>

	   </div>

	<!--Flash Banner-->
	   <div id="banner">
		<img src="assets/images/contact_banner.jpg" />
	   </div>

	<!--Row 1 Header Content (About Us..)-->
	   <div id="content_top">
		<img src="assets/images/thank_you.gif" alt="Thank You">
	   </div>

	<!--Content-->

	   <div class="text_only" style="height: 220px">	
				<img src="assets/images/tick.jpg" alt="Tick" style="float: left; padding: 20px 20px 0 20px"/>
		<h4>Thank You for your submission.</h4>
		 <h4>Your request will be dealt with as soon as possible and one of our representatives will contact you shortly.</h4>
		 <p>Return to the <a href="index.html" class="standlink">Home Page</a></p>
	</div>

	<!--Footer-->	
		<br>
	<img src="assets/images/networks.jpg" alt="Networks" style="margin-left: 750px" />
	<div id="footer">
	<address>ITMB | Copyright 2009 | All Rights Reserved</address><address style="float: left"><a class="standlink" href="index.html">ITMB</a>&#160;Registered Company Number: 06925752</address>
	</div>

	 </body>

	</html>

EOD;
echo "$theResults";



?>

 

Link to comment
Share on other sites

a.) why are you using the $_REQUEST array to fetch timescale rather than the $_POST array that you are using for everything else.

b.) if you want it in the e-mail you just place the $timescale variable into the e-mail.

Link to comment
Share on other sites

print_r is a function that prints out the contents of an array. $_POST is the superglobal array that should contain all the variables submitted from your form. If $timescale has no value that infers that $_POST['timescale'] either doesn't exist or has a blank value. Therefore I am suggesting you print out the contents of the $_POST array to find out why $timescale has no value.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.