anken
IS-IT--Management
- Aug 5, 2005
- 12
I need some help here.
I created a web form with a lot of field in it. I'm able to attach files and send email with as many attachments as I want, with all kind of format.
The last field is a text area for comment. If I send the form without a comment, I'm able to receive the email w/no problems. But Once I add a comment in the text area and post it, the first file attachment is all asci/or binary code concatenated the content of the comment. I've been trying to fix this for days. I think I need somebody to point out to me what I'm doing wrong.
I'll really appreciate your help.
Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Client Modification Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>.s1{FONT-SIZE:10pt;FONT-FAMILY:Arial,Helvetica,sans-serif}
.s2{}
.s3{font-size:9.0pt;font-family:Arial,Helvetica,sans-serif;}
.s5Gr{BACKGROUND-COLOR:#cccccc; font-size=10pt;font-family:Arial,Helvetica,sans-serif;}
.s5{font-size=10pt;font-family:Arial,Helvetica,sans-serif;}
</style>
<script type="text/javascript">
function frmCheck()
{
function doNotType()
{
event.returnValue=false;
event.srcElement.value="";
}
function addFile(cntName)
{
if (!document.getElementById || !document.createElement) return false;
var newLine = document.createElement("br");
var uplArea = document.getElementById("upl_"+cntName);
var oldInput = document.getElementById(cntName+"0");
var newInput = document.createElement("input"); newInput.type = "file"; newInput.size = oldInput.size;
var filenum = document.getElementById("nbfiles").value;
uplArea.appendChild(newLine);
if (!addFile.lastNum) addFile.lastNum = 0; addFile.lastNum++;
filenum++;
document.getElementById("nbfiles").value = filenum;
newInput.setAttribute("id", cntName + filenum);
newInput.setAttribute("name", cntName + filenum);
newInput.onkeydown=doNotType;
uplArea.appendChild(newInput);
}
function DataCheck()
//Checking the company
if (theform.company.value =="")
{
alert("Please enter a company name");
theform.company.focus();
return(false);
}
//Billing contact
if (theform.billingContact.value =="")
{
alert("Please enter a contact name");
theform.billingContact.focus();
return(false);
}
//Billing street
if (theform.billingStreet.value =="")
{
alert("Please enter a street name");
theform.billingStreet.focus();
return(false);
}
//Billing City
if (theform.billingCity.value =="")
{
alert("Please enter a City");
theform.billingCity.focus();
return(false);
}
if (theform.billingState.value =="")
{
alert("Please enter a State");
theform.billingState.focus();
return(false);
}
// check if numbers field is blank
if (theform.billingZip.value =="")
{
alert("Please enter a value for the \"zip code\" field.");
theform.billingZip.focus();
return (false);
}
if (theform.billingCountry.value =="")
{
alert("Please enter a Country");
theform.billingCountry.focus();
return(false);
}
//end of script
}
</script>
</head>
<body>
<?php
// Sanity check on mandatory fields
$ordDate = date("F j, Y, g:i a");
$body =$body1=$body2="";
$flag = true;
if (isset($_POST['send'])){
$srEmail = $_POST['srEmail'];
$sbEmail = $_POST['sbEmail'];
$billingContact = $_POST['billingContact'];
$billingStreet = $_POST['billingStreet'];
$billingCity = $_POST['billingCity'];
$billingState = $_POST['billingState'];
$billingZip = $_POST['billingZip'];
$billingCountry = $_POST['billingCountry'];
$company = $_POST['company'];
$phone = $_POST['billingPhone'];
$fax = $_POST['billingFax'];
$comment = $_POST['comment'];
$comment = wordwrap($comment);
$headers = "From: $srEmail";
$subject = "MOD: ".$company;
$nbfiles = $_POST['nbfiles'];
//getting the attachments
for ( $i = 0; $i < $nbfiles + 1; $i++ ){
$varfiles = 'fileatt'.$i;
$attachment[$i][0] = $_FILES[$varfiles]['tmp_name'];
$attachment[$i][1] = $_FILES[$varfiles]['name'];
$attachment[$i][2] = $_FILES[$varfiles]['type'];
}
//preparing the variables to be sent
$body = <<<EOD
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=  <br>
                MODIFICATION FORM <br>
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=   <br>
  <br>
********************Senders Info***************************   <br>
Date: $ordDate <br>
Sales Rep: $srEmail <br>
Submitted By: $sbEmail <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
2.COMPANY AND BILLING  <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
Company Name: $company  <br>
Billing Contact:   $billingContact   <br>
Billing Street Address:   $billingStreet , $billingCity , $billingState , $billingZip , $billingCountry  <br>
Phone:$phone <br>
Fax:  $fax  <br>
Email:     <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
3.COMMENTS  <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
$comment  <br>
EOD;
$message= $body;
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message;
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$flag = true;
for ( $i = 0; $i < $nbfiles + 1; $i++){
$upload = is_uploaded_file($attachment[$i][0]);
if ($upload){
$flag = false;
// Read the file to be attached ('rb' = read binary)
$file = fopen($attachment[$i][0],'rb');
$data = fread($file,filesize($attachment[$i][0]));
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$attachment[$i][2]};\n" .
" name=\"{$attachment[$i][1]}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$attachment[$i][1]}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
//"--{$mime_boundary}--\n";
}
}
if (!$flag){
$message .= "--{$mime_boundary}--\n";
}
if ($flag) {
$message .="\n\n".
"--{$mime_boundary}--\n";
}
$to = "you@example.com";
$ok = @mail($to, $subject, $message, $headers);
if ($ok){ ?>
<script type="text/javascript">
alert("Thank you, your order was succesfully submitted");
self.close();
</script>
<?} else { ?>
<script type="text/javascript">
alert("There was a problem with your order. Contact the IT dept. if the problem persists");
</script>
<?}
} //if post
?>
<p align = "center">Client Modification Form</p>
<p align = "center"><?php echo $ordDate;?></p>
<fieldset>
<legend>Client Info</legend>
<form name="theform" method="post" onSubmit="return DataCheck()" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width = "35%" align ="right">
<b>Submitted By (Email):<font color="red">*</font></b>
</td>
<td>
<input id="srEmail" type="text" maxLength="40" size="40" name="srEmail">
</td>
</tr>
<tr>
<td width = "35%" align ="right">
<b>Sales Rep (Full Name):</b>
</td>
<td>
<input type="hidden" id="nbfiles" name="nbfiles" value=0>
<input type="text" maxLength="40" size="40" name="sbEmail">
</td>
</tr>
</table>
<fieldset>
<legend>Company & Billing Info</legend>
<div>
<table>
<tr>
<td>
<p><span class='s1'><strong>Company Name:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="company" type="text" size="75" maxlength="75">
</td>
</tr>
<tr>
<td>
<p><span class='s1'><strong>Billing Contact:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="billingContact" type="text" size="75" maxlength="75">
</td>
</tr>
<tr>
<td>
<p><span class='s1'><strong>Billing Street Address:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="billingStreet" type="text" size="75" maxlength="75">
</td>
</tr>
</table>
<table>
<tr>
<td>City:<font color="red">*</font></td><td><input name="billingCity" type="text" size="30" maxlength="40"></td>
<td>State:<font color="red">*</font> </td><td><input name="billingState" type="text" size="3" maxlength="3"></td>
<td>Zip Code:<font color="red">*</td><td><input name="billingZip" type="text" size="10" maxlength="10"></td>
<td>Country:<font color="red">*</td><td><input name="billingCountry" type="text" size="29" maxlength="40"></td>
</tr>
<tr>
<td>Phone:</td><td><input name="billingPhone" type="text" size="12" maxlength="12"></td>
<td>Fax: </td><td><input name="billingFax" type="text" size="12" maxlength="12"></td>
<td>Email:</td><td><input name="billingEmail" type="text" size="40" maxlength="40"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Attach files</legend>
<table>
<tr>
<td>
</td>
<td>
Attach an Excel file :
</td>
<td id="upl_fileatt">
<input name="fileatt0" id="fileatt0" type="file" size="55" onkeydown="doNotType();" />
</td>
</tr>
<tr>
<td>
</td>
<td>
Another file :
</td>
<td>
<input type="button" value="more files ..." onclick="addFile('fileatt');">
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Comments</legend>
<textarea name="comment" rows="4" cols="50"></textarea>
</fieldset>
<div align="center">
<input type="submit" value="Send" name="send">
<input type="reset" value="Reset" name="reset">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
I created a web form with a lot of field in it. I'm able to attach files and send email with as many attachments as I want, with all kind of format.
The last field is a text area for comment. If I send the form without a comment, I'm able to receive the email w/no problems. But Once I add a comment in the text area and post it, the first file attachment is all asci/or binary code concatenated the content of the comment. I've been trying to fix this for days. I think I need somebody to point out to me what I'm doing wrong.
I'll really appreciate your help.
Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Client Modification Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>.s1{FONT-SIZE:10pt;FONT-FAMILY:Arial,Helvetica,sans-serif}
.s2{}
.s3{font-size:9.0pt;font-family:Arial,Helvetica,sans-serif;}
.s5Gr{BACKGROUND-COLOR:#cccccc; font-size=10pt;font-family:Arial,Helvetica,sans-serif;}
.s5{font-size=10pt;font-family:Arial,Helvetica,sans-serif;}
</style>
<script type="text/javascript">
function frmCheck()
{
function doNotType()
{
event.returnValue=false;
event.srcElement.value="";
}
function addFile(cntName)
{
if (!document.getElementById || !document.createElement) return false;
var newLine = document.createElement("br");
var uplArea = document.getElementById("upl_"+cntName);
var oldInput = document.getElementById(cntName+"0");
var newInput = document.createElement("input"); newInput.type = "file"; newInput.size = oldInput.size;
var filenum = document.getElementById("nbfiles").value;
uplArea.appendChild(newLine);
if (!addFile.lastNum) addFile.lastNum = 0; addFile.lastNum++;
filenum++;
document.getElementById("nbfiles").value = filenum;
newInput.setAttribute("id", cntName + filenum);
newInput.setAttribute("name", cntName + filenum);
newInput.onkeydown=doNotType;
uplArea.appendChild(newInput);
}
function DataCheck()
//Checking the company
if (theform.company.value =="")
{
alert("Please enter a company name");
theform.company.focus();
return(false);
}
//Billing contact
if (theform.billingContact.value =="")
{
alert("Please enter a contact name");
theform.billingContact.focus();
return(false);
}
//Billing street
if (theform.billingStreet.value =="")
{
alert("Please enter a street name");
theform.billingStreet.focus();
return(false);
}
//Billing City
if (theform.billingCity.value =="")
{
alert("Please enter a City");
theform.billingCity.focus();
return(false);
}
if (theform.billingState.value =="")
{
alert("Please enter a State");
theform.billingState.focus();
return(false);
}
// check if numbers field is blank
if (theform.billingZip.value =="")
{
alert("Please enter a value for the \"zip code\" field.");
theform.billingZip.focus();
return (false);
}
if (theform.billingCountry.value =="")
{
alert("Please enter a Country");
theform.billingCountry.focus();
return(false);
}
//end of script
}
</script>
</head>
<body>
<?php
// Sanity check on mandatory fields
$ordDate = date("F j, Y, g:i a");
$body =$body1=$body2="";
$flag = true;
if (isset($_POST['send'])){
$srEmail = $_POST['srEmail'];
$sbEmail = $_POST['sbEmail'];
$billingContact = $_POST['billingContact'];
$billingStreet = $_POST['billingStreet'];
$billingCity = $_POST['billingCity'];
$billingState = $_POST['billingState'];
$billingZip = $_POST['billingZip'];
$billingCountry = $_POST['billingCountry'];
$company = $_POST['company'];
$phone = $_POST['billingPhone'];
$fax = $_POST['billingFax'];
$comment = $_POST['comment'];
$comment = wordwrap($comment);
$headers = "From: $srEmail";
$subject = "MOD: ".$company;
$nbfiles = $_POST['nbfiles'];
//getting the attachments
for ( $i = 0; $i < $nbfiles + 1; $i++ ){
$varfiles = 'fileatt'.$i;
$attachment[$i][0] = $_FILES[$varfiles]['tmp_name'];
$attachment[$i][1] = $_FILES[$varfiles]['name'];
$attachment[$i][2] = $_FILES[$varfiles]['type'];
}
//preparing the variables to be sent
$body = <<<EOD
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=  <br>
                MODIFICATION FORM <br>
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=   <br>
  <br>
********************Senders Info***************************   <br>
Date: $ordDate <br>
Sales Rep: $srEmail <br>
Submitted By: $sbEmail <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
2.COMPANY AND BILLING  <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
Company Name: $company  <br>
Billing Contact:   $billingContact   <br>
Billing Street Address:   $billingStreet , $billingCity , $billingState , $billingZip , $billingCountry  <br>
Phone:$phone <br>
Fax:  $fax  <br>
Email:     <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
3.COMMENTS  <br>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  <br>
$comment  <br>
EOD;
$message= $body;
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message;
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$flag = true;
for ( $i = 0; $i < $nbfiles + 1; $i++){
$upload = is_uploaded_file($attachment[$i][0]);
if ($upload){
$flag = false;
// Read the file to be attached ('rb' = read binary)
$file = fopen($attachment[$i][0],'rb');
$data = fread($file,filesize($attachment[$i][0]));
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$attachment[$i][2]};\n" .
" name=\"{$attachment[$i][1]}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$attachment[$i][1]}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
//"--{$mime_boundary}--\n";
}
}
if (!$flag){
$message .= "--{$mime_boundary}--\n";
}
if ($flag) {
$message .="\n\n".
"--{$mime_boundary}--\n";
}
$to = "you@example.com";
$ok = @mail($to, $subject, $message, $headers);
if ($ok){ ?>
<script type="text/javascript">
alert("Thank you, your order was succesfully submitted");
self.close();
</script>
<?} else { ?>
<script type="text/javascript">
alert("There was a problem with your order. Contact the IT dept. if the problem persists");
</script>
<?}
} //if post
?>
<p align = "center">Client Modification Form</p>
<p align = "center"><?php echo $ordDate;?></p>
<fieldset>
<legend>Client Info</legend>
<form name="theform" method="post" onSubmit="return DataCheck()" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width = "35%" align ="right">
<b>Submitted By (Email):<font color="red">*</font></b>
</td>
<td>
<input id="srEmail" type="text" maxLength="40" size="40" name="srEmail">
</td>
</tr>
<tr>
<td width = "35%" align ="right">
<b>Sales Rep (Full Name):</b>
</td>
<td>
<input type="hidden" id="nbfiles" name="nbfiles" value=0>
<input type="text" maxLength="40" size="40" name="sbEmail">
</td>
</tr>
</table>
<fieldset>
<legend>Company & Billing Info</legend>
<div>
<table>
<tr>
<td>
<p><span class='s1'><strong>Company Name:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="company" type="text" size="75" maxlength="75">
</td>
</tr>
<tr>
<td>
<p><span class='s1'><strong>Billing Contact:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="billingContact" type="text" size="75" maxlength="75">
</td>
</tr>
<tr>
<td>
<p><span class='s1'><strong>Billing Street Address:</strong><font color="red">*</font></span></p>
</td>
<td>
<input name="billingStreet" type="text" size="75" maxlength="75">
</td>
</tr>
</table>
<table>
<tr>
<td>City:<font color="red">*</font></td><td><input name="billingCity" type="text" size="30" maxlength="40"></td>
<td>State:<font color="red">*</font> </td><td><input name="billingState" type="text" size="3" maxlength="3"></td>
<td>Zip Code:<font color="red">*</td><td><input name="billingZip" type="text" size="10" maxlength="10"></td>
<td>Country:<font color="red">*</td><td><input name="billingCountry" type="text" size="29" maxlength="40"></td>
</tr>
<tr>
<td>Phone:</td><td><input name="billingPhone" type="text" size="12" maxlength="12"></td>
<td>Fax: </td><td><input name="billingFax" type="text" size="12" maxlength="12"></td>
<td>Email:</td><td><input name="billingEmail" type="text" size="40" maxlength="40"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Attach files</legend>
<table>
<tr>
<td>
</td>
<td>
Attach an Excel file :
</td>
<td id="upl_fileatt">
<input name="fileatt0" id="fileatt0" type="file" size="55" onkeydown="doNotType();" />
</td>
</tr>
<tr>
<td>
</td>
<td>
Another file :
</td>
<td>
<input type="button" value="more files ..." onclick="addFile('fileatt');">
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Comments</legend>
<textarea name="comment" rows="4" cols="50"></textarea>
</fieldset>
<div align="center">
<input type="submit" value="Send" name="send">
<input type="reset" value="Reset" name="reset">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>