views:

14

answers:

1

Hi, I'm relatively new to php.

I wrote a script that sends an email with a pdf attachment to a user and a confirmation to a co-worker. Everything works fine in web-based email clients like gmail but the pdf attachment does not open when it is received in Outlook. It also adds another attachment called "ATT0159.txt" which is completely blank.

Here's the code:

<?php 
$pdf = $_GET['pdf'];
$product = $_GET['product'];

$username = $_POST['Name']; #get name
$useraddress = $_POST['Email']; #get user email address

$subjectUser = $product . " PDF brochure from Specifile on-line";

$to = "[email protected]"; # recipient
$subject = "Confirmation: PDF Request - " . $product; #subject
$message = "The following person has requested the brochure - " . $product . "\n<br/>" . $username . " [ " . $useraddress . " ] "; #message

$attachment = $_SERVER['DOCUMENT_ROOT'] . "\\" . $pdf;
$attachment_type = "application/pdf";
$attachment_name = "brochure.pdf";

#open, read, then close the file
$fp = fopen( $attachment, 'rb');
$file = fread($fp, filesize($attachment));
fclose($fp);

#create boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";

#encode data for safe transit
$file = chunk_split(base64_encode($file));

#define user header
$headers = "MIME-version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"{$str}\"\r\n";
$headers .= "From: Specifile on-line<[email protected]> \r\n";

#define confirmation header
$Confirmheaders = "MIME-version: 1.0\r\n";
$Confirmheaders .= "Content-type: text/html;";
$Confirmheaders .= " charset=\"UTF-8\"\r\n";
$Confirmheaders .= "From: Specifile on-line<[email protected]> \r\n";

#create message for user
$messageUser = "This is a multi-part message in MIME format\r\n";
$messageUser .= "--{$str}\r\n";
$messageUser .= "Content-type: text/html; charset=\"UTF-8\"\r\n";
$messageUser .= "Content-Transfer-Encoding: 8bit\r\n";
$messageUser .= "Hi " . $username . ", <p>The brochure you requested is attatched.</p> \r\n\n";
$messageUser .= "--{$str}\r\n";

#define non text attachment
$messageUser .= "Content-Type: {$attachment_type}; ";
$messageUser .= "name=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Disposition: attachment; ";
$messageUser .= "filename=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Transfer-Encoding: base64\r\n";
$messageUser .= "$file\r\n\n";
$messageUser .= "--{$str}";

mail($useraddress,$subjectUser,$messageUser,$headers);
mail($to,$subject,$message,$Confirmheaders);

?>
A: 

Not a real answer to your question, but have you considered using a ready-made mailing class like SwiftMailer? It does building the multipart message, and adding the attachments, for you.

Pekka
SwiftMailer worked brilliantly! Thanks a lot!
Ruan vH