POST Form data to Email

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
hcsdroid
Forum Newbie
Posts: 7
Joined: Tue Aug 24, 2010 1:33 pm

POST Form data to Email

Post by hcsdroid »

Ok here is what i have a problem with. this is my first PHP Ground up code and Form code EVER.

Here is my Form code:

Code: Select all

		<h1><a>Residential Application</a></h1>
		<form method="post" action="contact.php">
					<div class="form_description">
			<h2>Residential Application</h2>
			<p>Dear Earlville Buying Group Member,</p>
			<p> Ferrellgas is proud to be the propane supplier for the Earlville Buying Group. In order to process your application, please return the enclosed application applicable annual membership fee, to Ralph Griffiths via mail to:</p>
			<p><strong>Earlville Buyers Group<br>
			PO Box 200<br>
			Sherburne, NY, 13460<br>
			</strong> </p>
			<p> A commercial application must also be completed for businesses.Thank you for your time.</p>
			<p><strong>Ralph Griffiths<br>
			Phone 1-607-316-6008<br>
			Fax: 1-607-330-3664</strong></p>
          </div>						
			<ul >
			
					<li id="li_1" ><strong>Full Name</strong>
					  <div>
			<input id="Full_Name_1" name="Full_Name" class="element text" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_2" >
		<label class="description" for="element_2">Phone </label>
		<span>
			<input id="Phone_1" name="Phone" class="element text" size="18" maxlength="13" value="" type="text"><br />
		  ###-###-####</span></li>		<li id="li_3" >
		<label class="description" for="element_3">Billing Address </label>
		
		<div>
			<input id="Billing_Address_1" name="Billing_Address" class="element text large" value="" type="text">
			<label for="element_3_1">Street Address or PO Box</label>
		</div>
	
		<div>
			<input id="Billing_address_line_2" name="Billing_address_line_2" class="element text large" value="" type="text">
			<label for="element_3_2">Address Line 2</label>
		</div>
	
		<div class="left">
			<input id="City" name="City" class="element text medium" value="" type="text">
			<label for="element_3_3">City</label>
		</div>
	
		<div class="right">
			<input id="State" name="State" class="element text medium" value="" type="text">
			<label for="element_3_4">State / Province / Region</label>
		</div>
	
		<div class="left">
			<input id="Zip" name="Zip_Code" class="element text medium" maxlength="15" value="" type="text">
			<label for="element_3_5">Postal / Zip Code</label>
		</div>
		  </li>		<li id="li_4" >
		<label class="description" for="element_4">Delivery Address (If Different from Billing) </label>
		
		<div>
			<input id="Delivery_Address" name="Delivery_Address_Line_1" class="element text large" value="" type="text">
			<label for="element_4_1">Street Address</label>
		</div>
	
		<div>
			<input id="Delivery_Address_line_2" name="Delivery_Address_line_2" class="element text large" value="" type="text">
			<label for="element_4_2">Address Line 2</label>
		</div>
	
		<div class="left">
			<input id="Delivery_City" name="Delivery_City" class="element text medium" value="" type="text">
			<label for="element_4_3">City</label>
		</div>
	
		<div class="right">
			<input id="Delivery_State" name="Delivery_State" class="element text medium" value="" type="text">
			<label for="element_4_4">State / Province / Region</label>
		</div>
	
		<div class="left">
			<input id="Delivery_Zip_Code" name="Delivery_Zip_Code" class="element text medium" maxlength="15" value="" type="text">
			<label for="element_4_5">Postal / Zip Code</label>
		</div>
	
		<div class="right"></div> 
		</li>		<li id="li_5" >
		<label class="description" for="element_5">Email </label>
		<div>
			<input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_6" >
		<label class="description" for="element_6">Current Tank Size </label>
		<div>
			<input id="Current_Tank_Size" name="Current_Tank_Size" class="element text medium" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_15" >
		<label class="description" for="element_15">Tank Location </label>
		<span>
			<input id="Tank_Location_Above" name="Tank_Location_Above" class="element radio" type="radio" value="Above Ground" />
<label class="choice" for="element_15_1">Above Ground</label>
<input id="Tank_Location_Below" name="Tank_Location_Below" class="element radio" type="radio" value="Below Ground" />
<label class="choice" for="element_15_2">Below Ground</label>

		</span> 
		</li>		<li id="li_16" >
		<label class="description" for="element_16">Do you preffer automatic or will call delivery? </label>
		<span>
			<input id="element_16_1" name="Delivery_Preferance_1" class="element radio" type="radio" value="Will Call" />
<label class="choice" for="element_16_1">Will Call</label>
<input id="element_16_2" name="Delivery_preferance_2" class="element radio" type="radio" value="Automatic" />
<label class="choice" for="element_16_2">Automatic</label>

		</span> 
		</li>		<li id="li_7" >
		<label class="description" for="element_7">If Automatic please tell us how often you would like delivery. </label>
		<div>
			<input id="element_7" name="how_often_you_would_like_delivery" class="element text large" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_8" >
		<label class="description" for="element_8">Social Security Number </label>
		<div>
			<input id="element_8" name="Social_Security_Number" class="element text medium" type="text" maxlength="11" value=""/> 
		</div><p class="guidelines" id="guide_8"><small>123-12-1234</small></p> 
		</li>		<li id="li_9" >
		<label class="description" for="element_9">Date of Birth </label>
		<span>
			<input id="element_9_1" name="DOB" class="element text" size="15" maxlength="10" value="" type="text">
		<label for="element_9_1">MM-DD-YYYY</label>
		</span>
		</li>		<li id="li_10" >
		<label class="description" for="element_10">Drivers License </label>
		<div>
			<input id="element_10" name="Drivers_License" class="element text medium" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_11" >
		<label class="description" for="Employer_L">Employers Name and Address </label>
		<div>
			<textarea id="element_11" name="Employer_info" class="element textarea small"></textarea> 
		</div> 
		</li>		<li id="li_12" >
		<label class="description" for="element_12">Mothers Maiden Name </label>
		<div>
			<input id="element_12" name="Mothers_Maiden_Name" class="element text medium" type="text" maxlength="255" value=""/> 
		</div> 
		</li>		<li id="li_13" >
		<label class="description" for="element_13">Banks Name, Address and Phone # </label>
		<div>
			<textarea id="element_13" name="Bank_Info" class="element textarea small"></textarea> 
		</div> 
		</li>		<li id="li_17" >
		<label class="description" for="element_17">Do you already OWN your tanks? </label>
		<span>
			<input id="element_17_1" name="Do_You_Own_Your_Tanks_1" class="element radio" type="radio" value="Yes" />
<label class="choice" for="element_17_1">Yes</label>
<input id="element_17_2" name="Do_You_Own_Your_Tanks_2" class="element radio" type="radio" value="No" />
<label class="choice" for="element_17_2">No</label>

		</span> 
		</li>		<li id="li_14" >
		<label class="description" for="element_14">What date would you like to start Delivery? </label>
		<span>
		<input id="element_14_1" name="Delivery_Start_Date" class="element text" size="15" maxlength="10" value="" type="text"> 
		<label for="element_14_1">MM-DD-YYYY</label>
		</span>
		</li>
			
					<li class="buttons">
			    <input type="hidden" name="form_id" value="303866" />
			    
				<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
		</li>
			</ul>
		</form>
Here is my sendmail.php code so far:

Code: Select all

<?php
  $Full_Name = $_REQUEST['Full_Name'] ;
  $Phone = $_REQUEST['Phone'] ;
  $Billing_Address = $_REQUEST['Billing_Address'] ;
  $Billing_address_line_2 = $_REQUEST['Billing_address_line_2'] ;
  $City = $_REQUEST['City'] ;
  $State = $_REQUEST['State'] ;
  $Zip_Code = $_REQUEST['Zip_Code'] ;
  $Delivery_Address_Line_1 = $_REQUEST['Delivery_Address_Line_1'] ;
  $Delivery_Address_Line_2 = $_REQUEST['Delivery_Address_Line_1'] ;
  $Delivery_State = $_REQUEST['Delivery_State'] ;
  $Delivery_Zip_Code = $_REQUEST['Delivery_Zip_Code'] ;
  $Email = $_REQUEST['Email'] ;
  $Current_Tank_Size = $_REQUEST['Current_Tank_Size'] ;
  $Tank_Location_Above = $_REQUEST['Tank_Location_Above'] ;
  $Tank_Location_Below = $_REQUEST['Tank_Location_Below'] ;
  $Delivery_Preferance_1 = $_REQUEST['Delivery_Preferance_1'] ;
  $Delivery_Preferance_2 = $_REQUEST['Delivery_Preferance_2'] ;
  $how_often_you_would_like_delivery = $_REQUEST['how_often_you_would_like_delivery'] ;
  $Social_Security_Number = $_REQUEST['Social_Security_Number'] ;
  $DOB = $_REQUEST['DOB'] ;
  $Drivers_License = $_REQUEST['Drivers_License'] ;
  $Employer_info = $_REQUEST['Employer_info'] ;
  $Mothers_Maiden_Name = $_REQUEST['Mothers_Maiden_Name'] ;
  $Bank_Info = $_REQUEST['Bank_Info'] ;
  $Do_You_Own_Your_Tanks_1 = $_REQUEST['Do_You_Own_Your_Tanks_1'] ;
  $Do_You_Own_Your_Tanks_2 = $_REQUEST['Do_You_Own_Your_Tanks_2'] ;
  $Delivery_Start_Date = $_REQUEST['Delivery_Start_Date'] ;
  $message = $_REQUEST[''] ;
  

  mail( "sales@earlvillebuyersgroup.com", "Residencial Application Submision",
    $message, "From: $Email" );
  header( "Location: http://www.earlvillebuyersgroup.net/Thank_You_For_your_Application.html" );
?>
what i cant figure out is how to do the $message = [all my form post data so it is easy to read here.]
I have searched all over and cant find anything i'm looking for. how i want it to look does not seem hard. eventually i would like it in HTML format but lets get the basics down first.
this is what i want it to look like in an email message

Full name: Customers full name from posted data
Phone #: Customers phone#

ETC

can any one help me format the $message variable to do this? even a video tutorial or sample.

Thanks WAY in advance.
HCS Droid
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: POST Form data to Email

Post by jraede »

You can run through each piece of data in the $_REQUEST array and construct the message variable like this:

Code: Select all

foreach($_REQUEST as $key=>$value) {
    $message .= "$key: $value \n";
}
hcsdroid
Forum Newbie
Posts: 7
Joined: Tue Aug 24, 2010 1:33 pm

Re: POST Form data to Email

Post by hcsdroid »

I was thinking more along the lines of:
$message = ['full name: ' $Full_Name /n 'phone: ' $Phone /n 'address_line_1: '$address_line_1/ n]

And so on. I just need the syntex and deliminator then i can plug it for html later.

Thanks.
hcsdroid
Forum Newbie
Posts: 7
Joined: Tue Aug 24, 2010 1:33 pm

Re: POST Form data to Email

Post by hcsdroid »

hcsdroid wrote:I was thinking more along the lines of:
$message = ['full name: ' $Full_Name /n 'phone: ' $Phone /n 'address_line_1: '$address_line_1/ n]

And so on. I just need the syntex and deliminator then i can plug it for html later.

Thanks.

Would something like this work?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: POST Form data to Email

Post by McInfo »

Spend a few minutes reading this page: Strings.
hcsdroid
Forum Newbie
Posts: 7
Joined: Tue Aug 24, 2010 1:33 pm

Re: POST Form data to Email

Post by hcsdroid »

McInfo wrote:Spend a few minutes reading this page: Strings.
Thanks Will Do.
hcsdroid
Forum Newbie
Posts: 7
Joined: Tue Aug 24, 2010 1:33 pm

Re: POST Form data to Email

Post by hcsdroid »

McInfo wrote:Spend a few minutes reading this page: Strings.

Ok i'm getting Fustrated now. i have read it all and again and again. i'm just freaking lost. it seems so simple but i just dont learn well looking at a maunual. i need an example. im one of those hands on people.

can some one give me an example?

This is what i have so far:

Code: Select all

<?php
  $Full_Name = $_REQUEST['Full_Name'] ;
  $Email = $_REQUEST['Email'] ;
  $headers = "From: $Email"; 
  $subject = "Web Contact Data"; 
  $fields = array(); 
  $fields[] = $_REQUEST['Full_Name'] ;
  $fields[] = $_REQUEST['Phone'] ;
  $fields[] = $_REQUEST['Billing_Address'] ;
  $fields[] = $_REQUEST['Billing_address_line_2'] ;
  $fields[] = $_REQUEST['City'] ;
  $fields[] = $_REQUEST['State'] ;
  $fields[] = $_REQUEST['Zip_Code'] ;
  $fields[] = $_REQUEST['Delivery_Address_Line_1'] ;
  $fields[] = $_REQUEST['Delivery_Address_line_2'] ;
  $fields[] = $_REQUEST['Delivery_City'] ;
  $fields[] = $_REQUEST['Delivery_State'] ;
  $fields[] = $_REQUEST['Delivery_Zip_Code'] ;
  $fields[] = $_REQUEST['Email'] ;
  $fields[] = $_REQUEST['Current_Tank_Size'] ;
  $fields[] = $_REQUEST['Tank_Location'] ;
  $fields[] = $_REQUEST['Delivery_Preferance'] ;
  $fields[] = $_REQUEST['how_often_you_would_like_delivery'] ;
  $fields[] = $_REQUEST['Social_Security_Number'] ;
  $fields[] = $_REQUEST['DOB'] ;
  $fields[] = $_REQUEST['Drivers_License'] ;
  $fields[] = $_REQUEST['Employer_info'] ;
  $fields[] = $_REQUEST['Mothers_Maiden_Name'] ;
  $fields[] = $_REQUEST['Bank_Info'] ;
  $fields[] = $_REQUEST['Do_You_Own_Your_Tanks'] ;
  $fields[] = $_REQUEST['Delivery_Start_Date'] ;
  $to = "hcsdroid@gmail.com";
  $body = "We have received the following information: \n\n "; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
//  $body = "We have received the following information:\n\n"; "Full Name: " $Full_name \n; 
  $headers2 = "From: hcsdroid@gmail.com"; 
  $subject2 = "Thank you for contacting us"; 
  $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at http://www.earlvillebuyersgroup.com";
	if($Email == '') {print "You have not entered an email, please go back and try again";} 
		else { 
			if($Full_Name == '') {print "You have not entered a name, please go back and try again";} 
				else { $send = mail($to, $subject, $body, $headers); $send2 = mail($Email, $subject2, $autoreply, $headers2); 
		if($send) {header( "Location: http://www.earlvillebuyersgroup.net/Thank_You_For_your_Application.html" );} 
						else {print "We encountered an error sending your mail, please notify HCSDroid@gmail.com"; } } } ?> 
This works for just giving you the data in a list. but the format SUX some are centered in the email and others justified right. with out the label of what the field is it is useless.

i get:

john doe
604-555-7874
eat@joes.com
and so on

when i need
Full Name: John Doe
Phone no.: 555-555-5555
Email: Eat@joes.com
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: POST Form data to Email

Post by McInfo »

When you use radio buttons in a form, all related buttons should have the same name. For example:

Code: Select all

<input type="radio" name="Tank_Location" value="Above Ground" />
<input type="radio" name="Tank_Location" value="Below Ground" />
One solution to your formatting problem is to use heredoc syntax, as in this example:

Code: Select all

$body = <<<BODY
We have received the following information:

                        Full Name: {$_POST['Full_Name']}
                            Phone: {$_POST['Phone']}
           Billing Address Line 1: {$_POST['Billing_Address_Line_1']}
                              ...: ...
BODY;
Another solution that is more complex and less flexible, but slightly less tedious, is started in this example:

Code: Select all

<?php
// Optional header
header('Content-Type: text/plain');

// $_POST will be used because the form uses the POST method. You can
// use $_REQUEST, but $_POST makes it clear what method you expect.
if (empty ($_POST)) {
    // $_POST should not be empty. Do something here to alert the user
    // and prevent the continuation of the script.
}

// This array is a list of fields that should have been posted. Note:
// Some field names have been changed (for consistency). Names in the
// form must be changed to match.
$expectedFields = array (
    'Full_Name',
    'Phone',
    'Billing_Address_Line_1',
    'Billing_Address_Line_2',
    'City',
    'State',
    'Zip_Code',
    'Delivery_Address_Line_1',
    'Delivery_Address_Line_2',
    'Delivery_City',
    'Delivery_State',
    'Delivery_Zip_Code',
    'Email',
    'Current_Tank_Size',
    'Tank_Location',
    'Delivery_Preference',
    'How_Often_You_Would_Like_Delivery',
    'Social_Security_Number',
    'DOB',
    'Driver_License',
    'Employer_Info',
    'Mothers_Maiden_Name',
    'Bank_Info',
    'Do_You_Own_Your_Tanks',
    'Delivery_Start_Date'
);

// The width of the first column is based on the longest field name.
// This does not have to be calculated. If you know the width and don't
// expect it to ever change, you can put an integer here.
$firstColumnWidth = strlen(array_reduce($expectedFields,
    create_function('$a, $b', 'return ((strlen($a) > strlen($b)) ? $a : $b);')
));

// This will be used to indent multi-line inputs.
$lineContinuation = "\n" . str_repeat(' ', $firstColumnWidth) . '  ';

// Each field line will use this format string.
$lineFormat = "%{$firstColumnWidth}s: %s\n";

$body = "We have received the following information:\n\n";

foreach ($expectedFields as $name) {
    if (array_key_exists($name, $_POST)) {
        $body .= sprintf(
            $lineFormat,
            // Converts underscores in the field name to spaces
            str_replace('_', ' ', $name),
            // Indents multi-line inputs
            preg_replace('/\v+/', $lineContinuation, $_POST[$name])
        );
    } else {
        // A field is missing. The submission is invalid.
    }
}

echo $body;
Although I didn't show it in the heredoc example, you still should verify that the correct fields are being submitted to your script.
Post Reply