0

我想用一封电子邮件发送 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);

我搜索了许多主题,并尝试将这些解决方案集成到我的代码中,如您在上面看到的,但收效甚微。

4

1 回答 1

0

发送邮件并不像第一眼看上去那么容易。我建议使用 swiftmailer 之类的成熟库,而不是自己实现它:

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

于 2013-04-18T06:46:21.003 回答