FPDF生成的电子邮件附件由奇怪的字符组成


FPDF generated attachment for e-mail consists out of weird characters

在过去的两年里,我一直在使用FPDF生成PDF文件。生成此文件后,它会通过电子邮件发送给我。我最近在新服务器上安装了完全相同的脚本。出于这样或那样的原因,PDF的生成是有效的,因为我没有收到错误消息。我在电子邮件中收到的消息是纯文本的,看起来像:

--每个--或--d7582bf6769dd1faee8f05cb04cf445——

每条消息都不一样。

剥离后的代码是:

require('class.phpmailer.php');
require('fpdf.php');
define('FPDF_FONTPATH','font/');
//Create new PDF
$pdf=new PDF();
$pdf->AliasNbPages(); 
$pdf->company = $business;
$pdf->SetFont('Arial','',12);
$pdf->SetAutoPageBreak(false);
$pdf->AddPage('P');
// email stuff
$tijd = time();
$datum = date('j-m-Y', $tijd);
$bestandsnaam = $usernameinlog."-".$datum;
$from = "magazijnbeheer@".$website;
$subject = "Voorraad mutatie door ".$usernameinlog; 
$message = "<p>Zie bijlage voor een mutatieoverzicht.</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 = $bestandsnaam.".pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$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;

// The actual message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset='"iso-8859-1'"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// Bijlage
$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($emailemployee, $subject, "", $headers);

有人知道出了什么问题吗?php.ini中是否缺少一个参数?再一次:同样的代码在不同的服务器上工作,所以我认为有些设置是错误的,或者我忘记安装了一些东西。

:-)谢谢,

Alex

如果服务器未运行Windows,

$eol = PHP_EOL;可能会导致问题。

无论操作系统如何,电子邮件中的每一行都必须以CRLF结尾,因此您应该硬编码$eol = "'r'n";

有时,服务器和客户端可以处理CR或LF,但这是非标准的,他们真的不必这样做


如果您在这之后仍然有问题,请将消息源添加到问题中(为了简洁,可能会将base64位修剪为2行)?

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

你基本上是在发送一条空消息,其中以某种方式填充了全部内容$headers。。。。

尝试将$headers .= "Content-Transfer-Encoding: 7bit".$eol;以下的所有内容放在$body变量中,而不是$headers中,然后使用发送

mail($emailemployee, $subject, $body, $headers);

(也可以用SimonMayer建议的$eol = "'r'n"代替$eol = PHP_EOL