PDF附件显示在Gmail中,但Outlook拒绝打开它

问题描述:

我对PHP比较陌生。PDF附件显示在Gmail中,但Outlook拒绝打开它

我写了一个脚本,用于向用户发送一封包含pdf附件的电子邮件,并向同事发送确认邮件。在基于web的电子邮件客户端(如Gmail)中一切正常,但在Outlook中收到时,pdf附件无法打开。它还添加了另一个名为“ATT0159.txt”的附件,该附件完全空白。

下面的代码:

<?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); 

?> 

不是一个真正的回答你的问题,但你有没有考虑过使用现成的邮件类像SwiftMailer?它会为您创建多部分邮件并添加附件。

+0

SwiftMailer工作出色!非常感谢! – 2010-08-30 12:27:45