I have a form that is submitted which sends an HTML Email & and Attachment if added. However, the script Errors when no file is attached, but will send successfully when a file is attached.
Code:
$to = "name@name.com";
$department = $_REQUEST['department'];
$accNumber = $_REQUEST['accNumber'];
$todaysDate = $_REQUEST['todaysDate'];
$contactName = $_REQUEST['contactName'];
$contactEmail = $_REQUEST['email'];
$contactPhone = $_REQUEST['contactPhone'];
$dateNeeded = $_REQUEST['dateNeeded'];
$colors = $_REQUEST['colors'];
$paper = $_REQUEST['paper'];
$quantity = $_REQUEST['quantity'];
$designNotes = $_REQUEST['designNotes'];
$from = $contactEmail;
$subject = "Graphic Service Request";
$htmlVersion = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<style type='text/css'>
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
color: #000000;
}
.bottom_boarder {
border-bottom-width: thin;
border-bottom-style: solid;
border-bottom-color: #666666;
}
.left_boarder {
border-left-width: thin;
border-left-style: solid;
border-left-color: #999999;
}
h1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
color: #000000;
}
.top_boarder {
border-bottom-width: thin;
border-bottom-style: none;
border-bottom-color: #333333;
border-top-width: thin;
border-top-style: solid;
border-top-color: #333333;
}
.style1 {color: #990000}
.style2 {color: #003366}
-->
</style>
</head>
<body>
<table width='1053' border='0' cellpadding='3'>
<tr>
<td colspan='4'><table width='1001' border='0' cellpadding='3' class='bottom_boarder'>
<tr>
<td width='30'><h1 align='right'>Job#:</h1></td>
<td width='52'></td>
<td width='111'><h1 align='right'>Ministry/Department:</h1></td>
<td width='135'>".$department."</td>
<td width='56'><h1 align='right'>Account#:</h1></td>
<td width='81'>".$accNumber."</td>
<td width='78'><h1 align='right'>Contact:</h1></td>
<td width='121'>".$contactName."</td>
<td width='115'><div align='right'>
<h1>Contact Phone: </h1>
</div></td>
<td width='140'>".$contactPhone."</td>
</tr>
</table></td>
</tr>
<tr>
<td colspan='4'><table width='1003' border='0' cellpadding='3' class='bottom_boarder'>
<tr>
<td width='44'><h1 align='right'>Date in: </h1></td>
<td width='116'>".$todaysDate."</td>
<td width='32'><h1 align='right'>Due:</h1></td>
<td width='130'>".$dateNeeded."</td>
<td width='62'><h1 align='right'>Completed:</h1></td>
<td width='218'> </td>
<td width='77'><h1 align='right'>Charge Date: </h1></td>
<td width='258'> </td>
</tr>
</table></td>
</tr>
<tr>
<td colspan='4'><table width='1004' border='0' cellpadding='3' class='bottom_boarder'>
<tr>
<td width='69'><h1 align='right'>Assigned to: </h1></td>
<td width='220'> </td>
<td width='24'><h1 align='right'>PO#</h1></td>
<td width='257'> </td>
<td width='42'><h1 align='right'>Vendor:</h1></td>
<td width='341'> </td>
</tr>
</table></td>
</tr>
<tr>
<td colspan='3' valign='top'><table width='373' height='425' border='0' cellpadding='3'>
<tr>
<td height='18' colspan='2'><h1>Design Notes: </h1></td>
</tr>
<tr>
<td height='381' colspan='2' valign='top' class='bottom_boarder'>".$designNotes."<br />
<br />
<span class='style1'>".$colors."<br />
<br />
<span class='style2'>".$paper."</span></span></td>
</tr>
<tr>
<td width='116' height='18'><h1>Quantity Requested: </h1></td>
<td width='239'>".$quantity."</td>
</tr>
</table></td>
<td valign='top'><table width='624' border='0' cellpadding='3'>
<tr>
<td width='72'><h1> </h1></td>
<td width='139'><h1 align='center'>Description</h1></td>
<td width='124'><h1 align='center'>Qty</h1></td>
<td width='115'><h1 align='center'>Price</h1></td>
<td width='132'><h1 align='center'>Total</h1></td>
</tr>
<tr>
<td colspan='5' bgcolor='#E9E9E9'> <h1 align='left'>Paper:</h1></td>
</tr>
<tr>
<td height='50'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
</tr>
<tr>
<td colspan='5' bgcolor='#E9E9E9'><div align='right'>
<h1 align='left'>Output:</h1>
</div></td>
</tr>
<tr>
<td height='50'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
</tr>
<tr>
<td colspan='5' bgcolor='#E9E9E9'><h1 align='left'>Mask:</h1></td>
</tr>
<tr>
<td height='50'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
</tr>
<tr>
<td colspan='5' bgcolor='#E9E9E9'><h1 align='left'>Plates:</h1></td>
</tr>
<tr>
<td height='50'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
</tr>
<tr>
<td colspan='5' bgcolor='#E9E9E9'><h1 align='left'>Miscellaneous:</h1></td>
</tr>
<tr>
<td height='50'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
<td height='50' class='left_boarder'> </td>
</tr>
<tr>
<td colspan='5' class='bottom_boarder'> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td><div align='right'>
<h1>Grand Total: </h1>
</div></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>";
$msg="";
$hdr="";
$att = $_FILES['att'];
$att_path = $_FILES['att'] ['tmp_name'];
$att_name = $_FILES['att'] ['name'];
$att_size = $_FILES['att'] ['size'];
$att_type = $_FILES['att'] ['type'];
if (is_uploaded_file($att_path)) {
$fp = fopen($att_path, "rb");
if ($fp==TRUE ) {
$file = fread($fp, filesize($att_path));
fclose($fp);
if ( $file) {
$num = md5(time());
$str = "=-MuLtIpArT_BoUnDaRy_X{$num}x";
$file = chunk_split(base64_encode($file));
$hdr .= "From: $from\n";
//$hdr .= "X-Mailer: \n\n";
$hdr .= "MIME-Version: 1.0\n";
$hdr .= "Content-Type: multipart/alternative; boundary=\"{$str}\"\n";
$msg .= "--{$str}\n";
$msg .= "Content-Type: text/html; \n";
$msg .= "X-Mailer: \n\n";
$msg .= "Content-Transfer-Encoding: 7bit\n";
$msg .= "\n";
$msg .= "$htmlVersion \n";
$msg .= "\n\n\n";
$msg .= "--{$str}\n";
$msg .= "Content-Disposition: attachment; filename=\"{$att_name}\"\n";
$msg .= "Content-Type: {$att_type}; name=\"{$att_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n";
$msg .= "\n";
$msg .= "$file\n\n";
$msg .= "--{$str}\n";
$ok = mail($to, $subject, $msg, $hdr, "-f$from" );
if($ok) {
echo ("The Request Has Been Sent\n" );
}else{
echo("Error:This Request Has Not Been Sent - Error 2");
}
} else {
echo "Error reading upload file. This Request has not been sent";
}
} else {
echo "Error opening upload file. This Request has not been sent";
}
} else{
echo("Error:This Request Has Not Been Sent - Error 1");
}
?>