2013-04-08 167 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;