<?
$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>