簡體   English   中英

FPDF生成的電子郵件附件包含奇怪的字符

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

最近兩年我一直在使用FPDF來生成PDF文件。 生成此文件后,會將其通過電子郵件發送給我。 我最近在新服務器上安裝了完全相同的腳本。 由於一個或其他原因,由於沒有收到錯誤消息,因此生成了PDF。 我在電子郵件中收到的消息是純文本,看起來像:

--4aca5942d8bd7e7d523d8b2d71c6b1ea--或--d7582bf6769dd1fa2ee8f05cb04cf445--

每個消息都是不同的。

剝離的代碼為:

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中缺少參數了嗎? 再一次:相同的代碼在不同的服務器上工作,因此我認為某些設置有誤,或者我忘記安裝某些東西。

:-) 謝謝,

亞歷克斯

$eol = PHP_EOL; 如果您的服務器未運行Windows,很可能會導致問題。

無論操作系統如何,電子郵件中的每一行都必須以CRLF結尾,因此您應該對$eol = "\\r\\n";硬編碼$eol = "\\r\\n";

有時,服務器和客戶端可以應付CR或LF,但這不是標准的,而且實際上不是必須的。


如果在此之后仍然有問題,可以將消息源添加到問題中(為簡便起見,也許將base64位修剪為2行)?

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

您基本上會發送一條空消息,將全部內容以某種方式塞入$ headers中。

嘗試將所有內容放在$headers .= "Content-Transfer-Encoding: 7bit".$eol;下面$headers .= "Content-Transfer-Encoding: 7bit".$eol; 在$ body變量而不是$ headers中,然后發送

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

(也可以$eol = PHP_EOL$eol = "\\r\\n"替換$eol = PHP_EOL PHP_EOL)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM