2013-04-15 70 views
0

我想用一封電子郵件發送2封附件。第一個來自fpdf類。這工作完美。現在,第二個附件需要來自服務器上的文件夾。但我似乎無法讓它正確發送。電子郵件pdf附件+服務器附件

這是我的代碼

$to = $email; 
$from = $user_data['email']; 
$subject = $onderwerp; 
$message = $body; 

$separator = md5(time()); 

$eol = PHP_EOL; 

$fileatt = $user_data['verklaring']; // post variable path to the file 
$fileatt_type = "application/pdf"; // File Type 
$fileatt_name = 'VAR verklaring ' . $user_data['voornaam'] . ' ' . $user_data['achternaam']; 
$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 
$data = chunk_split(base64_encode($data)); 

$filename = $factuur_data['factuur_nr'] . '.pdf'; 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 

$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol; 
$headers .= "This is a MIME encoded message.".$eol.$eol; 

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

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

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

$headers .= "--".$separator."--"; 

mail($to, $subject, "", $headers); 

我搜索了很多話題,我曾嘗試這些解決方案在我的代碼整合,你可以在上面看到,但收效甚微。

+0

考慮使用Swiftmailer - 支持附件,本領域組件的狀態。 http://swiftmailer.org/docs/messages.html – herrjeh42

+0

謝謝你這是一個很好的解決方案1 ​​ – Jaap115

+0

我會將其作爲普通答案添加進一步參考... :-) – herrjeh42

回答