从服务器发送pdf附件+附件

时间:2013-04-15 17:37:38

标签: php email attachment fpdf

我想通过一封电子邮件发送2个附件。第一个是来自fpdf类。这非常有效。现在第二个附件需要来自我服务器上的文件夹。但我似乎无法让它正确发送。

这是我的代码

$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);

我搜索了很多主题,我尝试将这些解决方案集成到我的代码中,如上所示,但收效甚微。

1 个答案:

答案 0 :(得分:0)

发送邮件并不像第一眼看到的那样简单。我建议使用像swiftmailer这样完全成熟的库,而不是自己实现它:

查看:http://swiftmailer.org/docs/messages.html