0

我对php比较陌生。

我编写了一个脚本,该脚本向用户发送带有 pdf 附件的电子邮件并向同事发送确认信息。在 gmail 等基于 Web 的电子邮件客户端中一切正常,但在 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 = "specifile@gmail.com"; # 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<specifile@infixion.co.za> \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<specifile@infixion.co.za> \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);

?>
4

1 回答 1

0

不是您问题的真正答案,但是您是否考虑过使用像SwiftMailer这样的现成邮件类?它会为您构建多部分消息并添加附件。

于 2010-08-30T10:06:25.350 回答