I am trying to setup a form that will output html to the email recipient so the format of the email doesn't look all jacked while at the same time making sure the person is human and doesn't allow bad characters. (so it has a verification script and it strips out the html from the form input).

The form I've been using doesn't allow me to format it this way, even though I strip the format before inputting it into the email it forwards on, but will let me put a break into the form using /n (for whatever reason that is). Is there a way for me to format the content of the email so it will appear lined up side by side in something similar to a table so when they receive it, it isn't a really super long list??

Any assistance in this direction would be appreciated, I'm going nuuuttttssss!!! (didn't have far to go, I know.)

Recommended Answers

All 14 Replies

Send you data to a function right before you email it.  assuming you have validation step 1, and then email step 2.  try this:
// create your function
function format_html_email($_POST){
	$html ='';
	$i=0;
	// the following creates a two column table;
	$html .= "<table> \n";
	foreach($_POST as $key => $value) {
		$i++;

		if ($i % 2) {
			$html .= "<tr>";
		} else {
		}

		$html .= "<td>".$key." = ".$value ."</td> \n";

		if ($i % 2) {		
		} else {								
			$html .= "</tr> \n";
		}	

	}
	$html .= "</table> \n";
	return $html;
}
// in your processing code....   
$validated = false;
$validated = validate($_POST);  (or however you validate);
// assuming you are gettine a true back if validated, false if not.
if ($value) {
	$formatted_data = format_html_email($_POST);
	$to = "email@email.com";
	$subject = "some subject line";
	$headers = "nobody@mydomain.com";
	mail($to,$subject,$formatted_data,$headers);	
} else {
	// form did not validate what to do now?  
}

if ($value) was supposed to be if ($validated) { ... (line 31)

Thank you ddymacek.
When I receive this message though, it is still sending it with the structure all around it without actually giving it structure.
For instance, it looks like:

<table>
<tr>
<td>app_date = </td>
<td>first_name = </td>
<tr>
<td>last_name = </td>
<td>ssn = </td>
</tr>
<tr>
<td>address = </td>
<td>city = </td>
<tr>
<td>state = </td>
<td>zip = </td>
</tr>
<tr>
<td>phone = </td>
<td>email = </td>
<tr>
<td>position_applying_for = </td>
<td>location_applying_for = </td>
</tr>
</table>

So obviously it isn't actually formatting it with html, just giving it html tags.

I am going to post my script that I have used to set this up, because, as you will notice as soon as you see it... I am about as good with php as a blind person at playing catch.

Thank you for the feedback and attempt at helping me out. My one brain cell must be fighting for dominance because man - my head hurts!
Thanks!
Seth

<?php
// load the variables from address bar
$compname = $_POST["compname"];
$name = $_POST["name"];
$from = $_POST["from"];
$streetaddress = $_POST["streetaddress"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$contactperson = $_POST["contactperson"];
$phone = $_POST["phone"];
$num_plant_locations = $_POST["num_plant_locations"];
$employees = $_POST["employees"];
$Currently_Has_Food_Service = $_POST["Currently_Has_Food_Service"];
$Current_Vending = $_POST["Current_Vending"];
$shifts = $_POST["shifts"];
$sqfoot = $_POST["sqfoot"];
$timeofday = $_POST["timeofday"];
$Contact_Time = $_POST["Contact_Time"];
$verif_box = $_POST["verif_box"];

// remove the backslashes that normally appears when entering " or '
$compname = stripslashes($compname);
$name = stripslashes($name);
$from = stripslashes($from);
$streetaddress = stripslashes($streetaddress);
$city = stripslashes($city);
$state = stripslashes($state);
$zip = stripslashes($zip);
$contactperson = stripslashes($contactperson);
$phone = stripslashes($phone);
$num_plant_locations = stripslashes($num_plant_locations);
$employees = stripslashes($employees);
$Currently_Has_Food_Service = stripslashes($Currently_Has_Food_Service);
$Current_Vending = stripslashes($Current_Vending);
$shifts = stripslashes($shifts);
$sqfoot = stripslashes($sqfoot);
$timeofday = stripslashes($timeofday);
$Contact_Time = stripslashes($Contact_Time);

// create your function
function format_html_email($_POST){
	$html ='';
	$i=0;
	// the following creates a two column table;
	$html .= "<table> \n";
	foreach($_POST as $key => $value) {
		$i++;

		if ($i % 2) {
			$html .= "<tr> \n";
		} else {
		}

		$html .= "<td>".$key." = ".$value ."</td> \n";

		if ($i % 2) {
		} else {								
			$html .= "</tr> \n";
		}	

	}
	$html .= "</table> \n";
	return $html;
}
// in your processing code....   
$validated = false;
$validated = (md5($verif_box).'a4xn' == $_COOKIE['tntcon']);
// assuming you are gettine a true back if validated, false if not.
if ($validated) {
	$formatted_data = format_html_email($_POST);
	$to = "myemail@email.com";
	$subject = "some subject line";
	$headers = "noreply@email.com";
	mail($to,$subject,$formatted_data,$headers);
	setcookie('tntcon','');
} else if (isset($verif_box) and $verif_box!=""){
	// if verification code was incorrect then return to contact page and show error
	header("Location:".$_SERVER['HTTP_REFERER']."?compname=$compname&from=$from&name=$name&streetaddress=$streetaddress&city=$city&state=$state&zip=$zip&contactperson=$contactperson&phone=$phone&num_plant_locations=$num_plant_locations&employees=$employees&Currently_Has_Food_Service=$Currently_Has_Food_Service&Current_Vending=$Current_Vending&shifts=$shifts&sqfoot=$sqfoot&timeofday=$timeofday&Contact_Time=$Contact_Time&wrong_code=true");
	exit;
} else {
	echo "no variables received, this page cannot be accessed directly";
	exit;
}
?>

also, wanted to point out that yes, I am aware that the content at the top isn't getting used for my custom titles and all, which I would like help with as well. But quite honestly functionality and layout is a little more important.

I would also really like the script to layout the table with 4 columns, since currently it isn't. However, every time I change the numbers to 4 it just adds a table row between every table data cell and doesn't close the table row until the 4th table data cell has been created and closed...so I'm not really sure what that is all about either. Only one that seems to work is when you use the number 2 in both spots.

Thanks again!

Member Avatar for diafol

$_POST is gloabl so you don't have to pass it to the function. ALso you clean the data outside the function - any reason?

Not really - I guess I could clean it in the function itself, couldn't I? I'm just not thinking too clearly, and like I said - not exactly a php guru. Okay, not exactly a php anything to be honest. Just struggling through it.

Okay, instead of making it automatically create the table data, I am going to restructure that code to post it with real content... please hold while I revise.
Thanks!
Seth

Okay, this form is really super extensive, I apologize.

I only setup the first form information. The following is the code I have used:

<?php

// create your function
function format_html_email($_POST){
	// load the variables from address bar
	$first_name = $_POST["first_name"];
	$last_name = $_POST["last_name"];
	$ssn = $_POST["ssn"];
	$streetaddress = $_POST["address"];
	$city = $_POST["city"];
	$state = $_POST["state"];
	$zip = $_POST["zip"];
	$phone = $_POST["phone"];
	$from = $_POST["email"];
	$preferred_contact_method = $_POST["preferred_contact_method"];
	$eighteen = $_POST["eighteen"];
	$veterans = $_POST["veterans"];
	$legal = $_POST["legal"];
	$position_applying_for = $_POST["position_applying_for"];
	$location_applying_for = $_POST["location_applying_for"];
	$start_date = $_POST["start_date"];
	$desired_wage = $_POST["desired_wage"];
	$applied_to_us_before = $_POST["applied_to_us_before"];
	$applied_where = $_POST["applied_where"];
	$applied_when = $_POST["applied_when"];
	$employed_currently = $_POST["employed_currently"];
	$inquire_employer = $_POST["inquire_employer"];
	$shifts_available_to_work = $_POST["shifts_available_to_work"];
	$diploma_ged = $_POST["diploma_ged"];
	$name_of_school = $_POST["name_of_school"];
	$school_city = $_POST["school_city"];
	$school_state = $_POST["school_state"];
	$highest_level_of_schooling = $_POST["highest_level_of_schooling"];
	$degree_earned = $_POST["degree_earned"];
	$degree_concentration_licenses = $_POST["degree_concentration_licenses"];
	$other_training_education = $_POST["other_training_education"];
	$eh_company_name = $_POST["eh_company_name"];
	$eh_job_title = $_POST["eh_job_title"];
	$eh_address = $_POST["eh_address"];
	$eh_city = $_POST["eh_city"];
	$eh_state = $_POST["eh_state"];
	$eh_zip = $_POST["eh_zip"];
	$eh_start_date = $_POST["eh_start_date"];
	$eh_end_date = $_POST["eh_end_date"];
	$eh_job_duties = $_POST["eh_job_duties"];
	$eh_reason_for_leaving = $_POST["eh_reason_for_leaving"];
	$eh_company_name2 = $_POST["eh_company_name2"];
	$eh_job_title2 = $_POST["eh_job_title2"];
	$eh_address2 = $_POST["eh_address2"];
	$eh_city2 = $_POST["eh_city2"];
	$eh_state2 = $_POST["eh_state2"];
	$eh_zip2 = $_POST["eh_zip2"];
	$eh_start_date2 = $_POST["eh_start_date2"];
	$eh_end_date2 = $_POST["eh_end_date2"];
	$eh_job_duties2 = $_POST["eh_job_duties2"];
	$eh_reason_for_leaving2 = $_POST["eh_reason_for_leaving2"];
	$eh_company_name3 = $_POST["eh_company_name3"];
	$eh_job_title3 = $_POST["eh_job_title3"];
	$eh_address3 = $_POST["eh_address3"];
	$eh_city3 = $_POST["eh_city3"];
	$eh_state3 = $_POST["eh_state3"];
	$eh_zip3 = $_POST["eh_zip3"];
	$eh_start_date3 = $_POST["eh_start_date3"];
	$eh_end_date3 = $_POST["eh_end_date3"];
	$eh_job_duties3 = $_POST["eh_job_duties3"];
	$eh_reason_for_leaving3 = $_POST["eh_reason_for_leaving3"];
	$ref_name = $_POST["ref_name"];
	$ref_add_phone = $_POST["ref_add_phone"];
	$ref_bus_per = $_POST["ref_bus_per"];
	$ref_yrs_known = $_POST["ref_yrs_known"];
	$ref_name2 = $_POST["ref_name2"];
	$ref_add_phone2 = $_POST["ref_add_phone2"];
	$ref_bus_per2 = $_POST["ref_bus_per2"];
	$ref_yrs_known2 = $_POST["ref_yrs_known2"];
	$ref_name3 = $_POST["ref_name3"];
	$ref_add_phone3 = $_POST["ref_add_phone3"];
	$ref_bus_per3 = $_POST["ref_bus_per3"];
	$ref_yrs_known3 = $_POST["ref_yrs_known3"];
	$applicant_sig = $_POST["applicant_sig"];
	$today_date = $_POST["today_date"];
	$verif_box = $_POST["verif_box"];
	
	// remove the backslashes that normally appears when entering " or '
	$first_name = stripslashes($first_name);
	$last_name = stripslashes($last_name);
	$ssn = stripslashes($ssn);
	$streetaddress = stripslashes($address);
	$city = stripslashes($city);
	$state = stripslashes($state);
	$zip = stripslashes($zip);
	$phone = stripslashes($phone);
	$from = stripslashes($email);
	$preferred_contact_method = stripslashes($preferred_contact_method);
	$eighteen = stripslashes($eighteen);
	$veterans = stripslashes($veterans);
	$legal = stripslashes($legal);
	$position_applying_for = stripslashes($position_applying_for);
	$location_applying_for = stripslashes($location_applying_for);
	$start_date = stripslashes($start_date);
	$desired_wage = stripslashes($desired_wage);
	$applied_to_us_before = stripslashes($applied_to_us_before);
	$applied_where = stripslashes($applied_where);
	$applied_when = stripslashes($applied_when);
	$employed_currently = stripslashes($employed_currently);
	$inquire_employer = stripslashes($inquire_employer);
	$shifts_available_to_work = stripslashes($shifts_available_to_work);
	$diploma_ged = stripslashes($diploma_ged);
	$name_of_school = stripslashes($name_of_school);
	$school_city = stripslashes($school_city);
	$school_state = stripslashes($school_state);
	$highest_level_of_schooling = stripslashes($highest_level_of_schooling);
	$degree_earned = stripslashes($degree_earned);
	$degree_concentration_licenses = stripslashes($degree_concentration_licenses);
	$other_training_education = stripslashes($other_training_education);
	$eh_company_name = stripslashes($eh_company_name);
	$eh_job_title = stripslashes($eh_job_title);
	$eh_address = stripslashes($eh_address);
	$eh_city = stripslashes($eh_city);
	$eh_state = stripslashes($eh_state);
	$eh_zip = stripslashes($eh_zip);
	$eh_start_date = stripslashes($eh_start_date);
	$eh_end_date = stripslashes($eh_end_date);
	$eh_job_duties = stripslashes($eh_job_duties);
	$eh_reason_for_leaving = stripslashes($eh_reason_for_leaving);
	$eh_company_name2 = stripslashes($eh_company_name2);
	$eh_job_title2 = stripslashes($eh_job_title2);
	$eh_address2 = stripslashes($eh_address2);
	$eh_city2 = stripslashes($eh_city2);
	$eh_state2 = stripslashes($eh_state2);
	$eh_zip2 = stripslashes($eh_zip2);
	$eh_start_date2 = stripslashes($eh_start_date2);
	$eh_end_date2 = stripslashes($eh_end_date2);
	$eh_job_duties2 = stripslashes($eh_job_duties2);
	$eh_reason_for_leaving2 = stripslashes($eh_reason_for_leaving2);
	$eh_company_name3 = stripslashes($eh_company_name3);
	$eh_job_title3 = stripslashes($eh_job_title3);
	$eh_address3 = stripslashes($eh_address3);
	$eh_city3 = stripslashes($eh_city3);
	$eh_state3 = stripslashes($eh_state3);
	$eh_zip3 = stripslashes($eh_zip3);
	$eh_start_date3 = stripslashes($eh_start_date3);
	$eh_end_date3 = stripslashes($eh_end_date3);
	$eh_job_duties3 = stripslashes($eh_job_duties3);
	$eh_reason_for_leaving3 = stripslashes($eh_reason_for_leaving3);
	$ref_name = stripslashes($ref_name);
	$ref_add_phone = stripslashes($ref_add_phone);
	$ref_bus_per = stripslashes($ref_bus_per);
	$ref_yrs_known = stripslashes($ref_yrs_known);
	$ref_name2 = stripslashes($ref_name2);
	$ref_add_phone2 = stripslashes($ref_add_phone2);
	$ref_bus_per2 = stripslashes($ref_bus_per2);
	$ref_yrs_known2 = stripslashes($ref_yrs_known2);
	$ref_name3 = stripslashes($ref_name3);
	$ref_add_phone3 = stripslashes($ref_add_phone3);
	$ref_bus_per3 = stripslashes($ref_bus_per3);
	$ref_yrs_known3 = stripslashes($ref_yrs_known3);
	$applicant_sig = stripslashes($applicant_sig);
	$today_date = stripslashes($today_date);
	
	$html ='';
	$i=0;
	// the following creates a two column table;
	$html .= "<table> \n<tr> \n <td>Applicant Name: ".$first_name." = ".$last_name."</td>\n</tr>\n</table>\n";
	return $html;
}
// in your processing code....   
$validated = false;
$validated = (md5($verif_box).'a4xn' == $_COOKIE['tntcon']);
// assuming you are gettine a true back if validated, false if not.
if ($validated) {
	$formatted_data = format_html_email($_POST);
	$to = "email@mydomain.com";
	$subject = "some subject line";
	$headers = "noreply@email.com";
	mail($to,$subject,$formatted_data,$headers);
	setcookie('tntcon','');
} else if (isset($verif_box) and $verif_box!=""){
	// if verification code was incorrect then return to contact page and show error
	header("Location:".$_SERVER['HTTP_REFERER']."?compname=$compname&from=$from&name=$name&streetaddress=$streetaddress&city=$city&state=$state&zip=$zip&contactperson=$contactperson&phone=$phone&num_plant_locations=$num_plant_locations&employees=$employees&Currently_Has_Food_Service=$Currently_Has_Food_Service&Current_Vending=$Current_Vending&shifts=$shifts&sqfoot=$sqfoot&timeofday=$timeofday&Contact_Time=$Contact_Time&wrong_code=true");
	exit;
} else {
	echo "no variables received, this page cannot be accessed directly";
	exit;
}
?>

The following results are what I receive:

noreply@email.com

<table>
<tr>
<td>Applicant Name:moooooooo = oink</td>
</tr>
</table>


Any more suggestions?

Member Avatar for diafol

instead of all those lists, you could do this:

function format_html_email(){
   extract(array_map("stripslashes",$_POST));
   $html = "<table>\n<tr>\n<td>Applicant Name: $first_name $last_name</td>\n</tr>\n</table>\n";
   return $html;
}
//that's a single col, single row table with just the full name

Although, it's not always a good idea to use extract with $_POST. :(
Check out the manual.

Well, that would help reduce the size of the code, but that isn't the issue at the moment.

My issue is with the fact that when it sends me an email it doesn't send it as an actual html email. It sends the code as text to the email address and it doesn't actually show correctly. Is there anything else anyone would be able to suggest?

Plus the way I have it set up currently I can control which post I am pulling in a specific area when rendering the code, I don't know enough with php to make an array pull things specifically without doing it this way. So thank you for the advice. Perhaps I will have time to look into doing this after I get it functioning correctly, but the way it is displayed is more important to me at the moment.
Thanks,
Seth

Member Avatar for diafol

OK, good luck.

yes: set your html headers:
change your $headers line.

$from = "noreply@email.com";
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";       // THIS line makes it 'html'
commented: Very helpful, well written answers! +2

ddymacek! That solved my issue! You're a life saver! Truly! Thank you sooo much!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.