2013-04-15 66 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

回答