2013-04-08 97 views
0

我正在尝试使用FPDF并将生成的文件附加到电子邮件中。我看过这篇文章Email PDF Attachment with PHP Using FPDF,给我发电子邮件并在Thunderbird中查看时给出的答案是有效的。下面是给出的实例中的代码:FPDF电子邮件附件和展望

<?php 
require('lib/fpdf/fpdf.php'); 

$pdf = new FPDF('P', 'pt', array(500,233)); 
$pdf->AddFont('Georgiai','','georgiai.php'); 
$pdf->AddPage(); 
$pdf->Image('lib/fpdf/image.jpg',0,0,500); 
$pdf->SetFont('georgiai','',16); 
$pdf->Cell(40,10,'Hello World!'); 

// email stuff (change data below) 
$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>"; 

// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// attachment name 
$filename = "test.pdf"; 

// encode data (puts attachment in proper format) 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

// no more headers after this, we start the body! // 

$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

// message 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$body .= $message.$eol; 

// attachment 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol; 
$body .= "Content-Disposition: attachment".$eol.$eol; 
$body .= $attachment.$eol; 
$body .= "--".$separator."--"; 

// send message 
mail($to, $subject, $body, $headers); 

?> 

然而,发送和前景(2007)观察时它创建的邮件作为附件藏汉,这是做的代码或前景/

任何帮助赞赏。

伊恩

回答

0

IMO它没有意义有这是一个MIME编码的消息。作为零件的内容的;通常这样的句子在第一个mime分隔符前面,以通知没有MIME能力的邮件阅读器的任何用户以下行代表什么。实质上,您有一个邮件,其中包含一个明文(,这是一个MIME编码消息)和一个html(请参阅附件),其中包含一个multipart/mixed容器。如果它是一个multipart/alternative之一,邮件阅读器会选择其中一个来显示,但这样读者可能会怀疑要显示什么。

因此,我建议缩短

$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

$body = "This is a MIME encoded message.".$eol.$eol;