Page 1 of 1

Problem with PHP mail with attachments

Posted: Sun Oct 10, 2010 12:33 pm
by toose
Hey all, it's my first time here I was looking for a helping hand with a PHP mail script I've built from various pieces I've seen about the web.

The problem I'm having is the script works fine in GMail and Apple Mail app but when I view the email in Outlook it displays both the plain text and the HTML (tags displaying also) email. The attachment is also corrupt in Outlook but works fine in GMail and Apple Mail. The email output works fine viewing it on my blackberry but again the image is corrupt.

Anyway here's the code (apologies if it's a bit messy), does anybody have any ideas what I've done wrong?

Code: Select all

	$qName = cleanString($_POST['q_name']);
	$qCompany = cleanString($_POST['q_company']);
	$qEmail = cleanString($_POST['q_email']);
	$qPhone = cleanString($_POST['q_phone']);
	$qAddress = cleanString($_POST['q_address']);
	$qSize = cleanString($_POST['q_size']);
	$qQuantity = $_POST['q_quantity'];
	$qPurpose = cleanString($_POST['q_purpose']);
	$qInfo = cleanString($_POST['q_info']);
	
	if($_FILES["q_file"]["error"] == 0) {
		if(
		(
		($_FILES["q_file"]["type"] == "image/gif") || 
		($_FILES["q_file"]["type"] == "application/pdf") || 
		($_FILES["q_file"]["type"] == "image/png") || 
		($_FILES["q_file"]["type"] == "application/x-photoshop") || 
		($_FILES["q_file"]["type"] == "image/jpeg") || 
		($_FILES["q_file"]["type"] == "image/pjpeg")
		) 
		&& ($_FILES["q_file"]["size"] < 2000000))
		{
			if($_FILES["q_file"]["error"] > 0) {
				echo "<p>There was a problem uploading your image: image is corrupt.</p>";
			} else {
				$fileName = $_FILES["q_file"]["name"];
				$fileType = $_FILES["q_file"]["type"];
				$fileSize = ($_FILES["q_file"]["size"] / 1024)." Kb";
				$attachment = $_FILES['q_file']['tmp_name'];
				$attachment_name = $_FILES['q_file']['name']; 

				if(is_uploaded_file($attachment)) {
					$fp = fopen($attachment, "rb");
					$data = fread($fp, filesize($attachment));
					$data = chunk_split(base64_encode($data));
					fclose($fp);
			    }
			}
		} else {
			echo "<p>There was a problem uploading your design. Please ensure that the file type is accepted and that the file size is under <strong>2Mb</strong>.</p>";
			echo "<p><a href=\"/quote/\" title=\"Back\">Back</a></p>";
		}
	} else {
		$attachment = "";
	}
	

	$to = "blah@blah.com"; 
	$subject = "PHP Formmail"; 

	$random_hash = md5(date('r', time()));

	$headers = "From: Quick Quote Request <$contactFromEmail>\r\n"; 
	$headers .= "Reply-To: $qName <$qEmail>\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: multipart/mixed; boundary=\"$siteName-mixed-".$random_hash."\"";

	$attachment = chunk_split(base64_encode(file_get_contents($attachment)));
 
  $message = "
--$siteName-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"$siteName-alt-$random_hash\"
--$siteName-alt-$random_hash
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit\n\n
 
You have received the following quick quote request.<br><br>\n\n
Name:<br><b>$qName</b><br><br>\n\n
Company Name:<br><b>$qCompany</b><br><br>\n\n
Contact Email:<br><b>$qEmail</b><br><br>\n\n 
Contact Number:<br><b>$qPhone</b><br><br>\n\n
Delivery Address:<br><b>$qAddress</b><br><br>\n\n
Product Size:<br><b>$qSize</b><br><br>\n\n
Product Quantity:<br><b>$qQuantity</b><br><br>\n\n
Product Purpose:<br><b>$qPurpose</b><br><br>\n\n
Additional Information:<br><b>$qInfo</b><br><br>\n\n
Attachment:<br>\n\n
Name: <b>$fileName</b><br>\n\n
Type: <b>$fileType</b><br>\n\n
Size: <b>$fileSize</b><br><br>\n\n
Powered by Impentio.com<br>\n\n 

--$siteName-alt-$random_hash
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit\n\n
 
<html>\n
<body style=\"font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n
<h3>You have received the following quick quote request.</h3>\n\n
Name:<br><b>$qName</b><br><br>\n\n
Company Name:<br><b>$qCompany</b><br><br>\n\n
Contact Email:<br><b>$qEmail</b><br><br>\n\n 
Contact Number:<br><b>$qPhone</b><br><br>\n\n
Delivery Address:<br><b>$qAddress</b><br><br>\n\n
Product Size:<br><b>$qSize</b><br><br>\n\n
Product Quantity:<br><b>$qQuantity</b><br><br>\n\n
Product Purpose:<br><b>$qPurpose</b><br><br>\n\n
Additional Information:<br><b>$qInfo</b><br><br>\n\n
Attachment:<br>\n\n
Name: <b>$fileName</b><br>\n\n
Type: <b>$fileType</b><br>\n\n
Size: <b>$fileSize</b><br><br>\n\n
<small>Powered by Impentio.com</small><br>\n\n
</body>\n
</html>\n 
 
--$siteName-alt-$random_hash--
 
--$siteName-mixed-$random_hash
Content-Type: application/octet-stream; name=$attachment_name
Content-Transfer-Encoding: base64
Content-Disposition: attachment 
$data
--$siteName-mixed-$random_hash--";

	$mail_sent = @mail($to,$subject,$message,$headers);

	if($mail_sent) {
		echo "<p>Email sent!</p>";
	} else {
		echo "<p>Email not sent!</p>";
	}	


Re: Problem with PHP mail with attachments

Posted: Mon Oct 11, 2010 12:52 am
by Christopher
Honestly, getting email messages right on all clients is very difficult. You might want to try SwiftMailer instead.