【问题标题】:Sending printer an attachment with phpmailer causes blank first page使用 phpmailer 向打印机发送附件会导致首页空白
【发布时间】:2019-05-03 20:03:44
【问题描述】:

我一直致力于使用 phpmailer 将 pdf 附件发送到打印机。一切似乎都正常工作,只是每次将 pdf 发送到打印机时,它都会打印一个空白页,然后是附件。

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.******.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '****@****.com';                 // SMTP username
$mail->Password = '***';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->Subject = 'PDF Attached'
$mail->AltBody = 'A pdf has been attached.';
$mail->addAttachment($filename); // attachment
$mail->AllowEmpty = true;
$mail->addAddress("***************@hpeprint.com");
$mail->msgHTML("");
$mail->send();

如果我通过自己的电子邮件手动发送电子邮件,它不会打印额外的页面。

任何帮助将不胜感激

【问题讨论】:

  • 我怀疑 phpmailer 正在修改您的 PDF 文件。如果不是邮寄,而是将其输出到浏览器,会发生什么? header("Content-Disposition: attachment; filename='test.pdf'"); readfile($filename);
  • 我的猜测是打印机打印一页带有电子邮件正文,另一页带有附件。删除 $mail->msgHTML$mail->AltBody 会发生什么?

标签: php printing phpmailer


【解决方案1】:

msgHTML 还设置了AltBody,并且由于您传递的是一个空字符串,因此您将得到一个空的消息正文(您已经为其抑制了错误)。由于您没有将Body 设置为任何内容,也没有调用isHTML,因此默认情况下它不会发送multipart/alternative 消息。我建议将其还原为基础:

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.******.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '****@****.com';                 // SMTP username
$mail->Password = '***';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->Subject = 'PDF Attached'
$mail->addAttachment($filename); // attachment
$mail->AllowEmpty = true;
$mail->addAddress("***************@hpeprint.com");
$mail->send();

我希望您对所有这些都进行了一些错误处理...

【讨论】:

  • 这是正确的。我不想发送任何正文消息。只是打印机的附件。我假设设置 body =“” 会使 body 留空。这不正确吗?
  • 是的,没错,但这不是你所做的——你设置了AltBody
猜你喜欢
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
相关资源
最近更新 更多