Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending a form using mail() 2

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have a form that once it is filled out and submitted needs to be emailed. I know I can send HTML via the 'mail()' function but I don't know how to capture the filled out form without repeating all the tags a second time in the code that sets up the massage body.
Code:
<html>
   .
   .
   .
<form ...>
blah
blah
blah
</form>
<?php
   .
   .
   .
$message="<html>\n...\n<form ...>\n".$data1."\n".data2."\n".data3."\n</form>\n...\n</html>"
   .
   .
   .
?>
   .
   .
   .
</html>
Is there way to create the form so that it can be both displayed and filled in by a user then assigned, in tact to the message body? TIA
 
if you assemble the form in php rather than as hard coded html, and you include the value attribute in the html form control tags, then you can either output the assembled form to the mail message or to the screen or both!
 
Thanks for the reply.

You mean set the varaiable message to the form tags then either 'echo' the variable or use it in the mail function? I kind of thought that was one way to do it but I was hoping there was a way to capture the completed form and send that. My form is also going to have some javascript in it and I don't think I want to send that in the email. The form is really a job application and I want to send what appears to be a completed application to an administrator that looks the same as a hard copy.
 
here is a little something i cooked up earlier.

this only mails the form itself. so long as you put the js outside of the form variable you have no problems.

i haven't tested this form as have no smtp on current system. it should work though (famous last words).

Code:
<?
$textfield1="";  //these preset the default values for the form fields
$textfield2=""; 

//now create the form

$form = "<form method=\"post\" action=\"#\" name=\"formmailer\">\r\n";
$form .= "<fieldset>\r\n";
$form .= "<legend>Fill these in</legend>\r\n";
$form .= "<input name=\"textfield1\" type=\"text\" value=\"<?=$textfield1?>\" />\r\n";
$form .= "<input name=\"textfield2\" type=\"text\" value=\"<?=$textfield2?>\" />\r\n";
$form .= "<input name=\"submit\" type=\"submit\" value=\"submit\" />\r\n";
$form .= "</fieldset>\r\n";
$form .= "</form>\r\n";

if (isset($_POST['submit'])) //if the submit button is pressed it will mail the form
{
	mailfunction();
}

function mailfunction()
{

//adapted from the FAQ 
$notice_text = "This is a multi-part message in MIME format.";
//construct message body for plain text email
$plain_text = "";

foreach ($_POST as $key=>$val) //$_POST is a superglobal so don't need to pass it in
{
  $key = addslashes($key); //escape the strings
  $val = addslashes($val);
  
  if ($key != "submit") //don't want to do anything with the button!
  { 
  	$plain_text .= "The field $key had the value $val \r\n";
  }
}

$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$to = "";//to address
$bcc = "";
$from = ""; //from address
$subject = "My Email";

$body = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$form 

--$mime_boundary--";

if (@mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "bcc: " . $bcc . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/alternative;\n" . 
    "     boundary=" . $mime_boundary_header))
    echo "Email sent successfully.";
else
    echo "Email NOT sent successfully!";

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<?=$form //this displays the form?>
</body>
</html>
 
i have just set up an smtp server to test this script. there are a number of school boy errors in the form creation part and i'm finding the smtp server does not like the formulation of the mime headers. I'm not yet sure whether this is a fault with the script in the FAQ, with my changes to it or with the server interaction.

i will have a bash at debugging and post revised code when done.

in the meantime, apologies for posting buggy code.
 
code now debugged. Apologies once again

Code:
<?
if (!isset($_POST))
{
	$textfield1="";  //these preset the default values for the form fields
	$textfield2=""; 
}
else
{
	$textfield1=$_POST['textfield1'];
	$textfield2=$_POST['textfield2'];
}
//now create the form

$form = "<form method=\"post\" action=\"#\" name=\"formmailer\" style=\"width:40%;\">\r\n";
$form .= "<fieldset>\r\n";
$form .= "<legend>Fill these in</legend>\r\n";
$form .= "<input name=\"textfield1\" type=\"text\" value=\"$textfield1\" /><br />\r\n";
$form .= "<input name=\"textfield2\" type=\"text\" value=\"$textfield2\" /><br />\r\n";
$button = "<input name=\"submit\" type=\"submit\" value=\"submit\" />\r\n";  //separate to send the form niceld
$form2 = "</fieldset>\r\n";
$form2 .= "</form>\r\n";

// if you want the form to reset each time uncomment these next two lines
//$textfield1="";
//$textfield2="";


if (isset($_POST['submit'])) //if the submit button is pressed it will mail the form
{	
	mailfunction();
}

function mailfunction()
{
	global $form;
	global $form2;
	
	$message ="";
	$plain_text = "";
	$boundary = md5(time());
		
	//assemble the plain text part of the message
	foreach ($_POST as $key=>$val) //$_POST is a superglobal so don't need to pass it in
	{
		  $key = addslashes($key); //escape the strings
		  $val = addslashes($val);
		  
		  if ($key != "submit") //don't want to do anything with the button!
		  { 
			$plain_text .= "The field $key had the value $val \r\n";
		  }
	}
	
	$to = "name <email@domain.com>";//to address
	$bcc = "";
	$from = "name <sender@domain.com"; //from address
	$subject = "My Email";
	
	$header = "From: $from 
MIME_Version: 1.0 
Content-Type: multipart/mixed; boundary=\"MAIN-$boundary\"

This is a multi-part message in MIME format.
--MAIN-$boundary
Content-Type: multipart/alternative; boundary=\"PART-$boundary\"\r\n";
		
	$message = "--PART-$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
	
$plain_text
	
\r\n\r\n";
	
	$message .= "--PART-$boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit\r\n\r\n";

	$message .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">[/URL]
<html><head></head><body>$form$form2</body></html>\r\n\r\n";

	
	$message .= "--PART-$boundary--\r\n\r\n"; 

	if (mail($to, $subject, $message, $header))
	{  
		echo "Email sent successfully.";}
	else
	{   
		echo "Email NOT sent successfully!";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Form to be filled in</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<?=$form.$button.$form2 //this displays the form?>
</body>
</html>
 
Wow! Thanks for going to all that effort. It looks great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top