尝试从 thunderbird/outlook 转发邮件时 pdf 文件损坏

pdf file corrupted when try to forward mail from thunderbird/outlook

发送邮件的示例代码是

$fileName = 'output.pdf';
$fileatt = $pdfObj->Output('output.pdf', 'E'); 
$attachment = chunk_split($fileatt);

$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Sender <test@testmail.com>'.$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= 'Content-Type: multipart/mixed; boundary="'.$separator.'"';

$mailMessage .= '--'.$separator.$eol;
$mailMessage .= 'Content-Type:text/html;Charset=ISO-8859-1'.$eol;
$mailMessage .= 'Content-Transfer-Encoding: 8bit'.$eol.$eol;
$mailMessage .= 'message content text'.$eol;

$eol = "\r\n";
$mailMessage .= '--'.$separator.$eol;
$mailMessage .= 'Content-Type: application/pdf; name="'.$fileName.'"'.$eol;
$mailMessage .= 'Content-Transfer-Encoding: base64'.$eol;
$mailMessage .= 'Content-Disposition: attachment; filename='.$fileName.$eol;
$mailMessage .= $attachment.$eol;
$mailMessage .= '--'.$separator.'--';
mail('test@testmail.com', $toEmailid, $subject, $mailMessage, $headers);

谁能告诉我上面代码哪里错了? 或其 thunderbird 或 outlook 相关问题?

  • 使用替代解决方案在邮件中发送 pdf 文件,即将文件存储在服务器上,发送带有附件的电子邮件,在邮件发送后从服务器中删除文件。

示例代码

$fileName = 'output.pdf';
$fileatt = $pdfObj->Output(SERVERPATH.'/output.pdf', 'F');

$filename = 'output.pdf';
$file = $naTmpPath.'/'.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$attachment = chunk_split(base64_encode($content));

$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Sender <test@testmail.com>'.$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= 'Content-Type: multipart/mixed; boundary="'.$separator.'"';

$mailMessage .= '--'.$separator.$eol;
$mailMessage .= 'Content-Type:text/html;Charset=ISO-8859-1'.$eol;
$mailMessage .= 'Content-Transfer-Encoding: 8bit'.$eol.$eol;
$mailMessage .= 'message content text'.$eol;

$eol = "\r\n";
$mailMessage .= '--'.$separator.$eol;
$mailMessage .= 'Content-Type: application/pdf; name="'.$fileName.'"'.$eol;
$mailMessage .= 'Content-Transfer-Encoding: base64'.$eol;
$mailMessage .= 'Content-Disposition: attachment; filename='.$fileName.$eol;
$mailMessage .= $attachment.$eol;
$mailMessage .= '--'.$separator.'--';
mail('test@testmail.com', $toEmailid, $subject, $mailMessage, $headers);