【问题标题】:phpmailer attaching two pdf filesphpmailer附加两个pdf文件
【发布时间】:2018-05-12 20:11:25
【问题描述】:

我正在使用 phpmailer 附加 pdf 文件和发送带有 pdf 附件的电子邮件。正在附加一个 pdf 文件,而另一个没有附加。 我将代码用作

$attachedfile = $_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf';
$mail->addAttachment($attachedfile, 'Visa Application'); 
$attachedfile2 = $_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf';
$mail->addAttachment($attachedfile2, 'Visa Application 2'); 

只附加了一个 pdf 文件,另外一个正在附加。 它也适用于单个 pdf 文件附件。 我也使用了以下代码

$attachedfile = array($_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf',$_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_test.pdf'); foreach($attachedfile as $attachment){ $mail->AddAttachment($attachment); }

但它又附加了一个 pdf 文件

请帮忙

【问题讨论】:

  • 检查每次调用 addAttachment 的返回值,以确保您的脚本能够读取文件。
  • 一个文件返回 1,另一个什么都没有

标签: php pdf phpmailer


【解决方案1】:

您提到对于addAttachment 的两次调用,PHP 返回 1 并且什么都不返回。这就是 PHP 用作 truefalse 的文本表示的内容:PHP 无法读取您的文件之一,因为该文件丢失、路径错误或缺少足够的所有权或权限。仔细检查您的路径和权限。

当我说不要构建你的路径时,我的意思是使用 only 文字字符串作为路径。您可以编写一个独立的 PHP 脚本来检查它们:

<?php
$path1 = '/var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf';
$path2 = '/var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf';
var_dump($path1, is_file($path1), $path2, is_file($path2));

也在你的 shell 中检查它们:

ls -al /var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf /var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf

如果没有问题,请返回您的原始脚本并var_dump 您生成的路径,并比较它们 - 包括长度,以防您不小心包含了一些非打印字符或零宽度字符。

【讨论】:

  • 我检查了这两个文件是否在同一目录和同一路径中。两者具有相同的路径和权限:(
  • 我还重命名并复制粘贴了同一个文件。它仍然显示单个附件。
  • Look at the codeaddAttachment 返回 false 的唯一方法是 is_file() 返回 false。再次检查您的路径 - 使用硬编码的路径字符串进行测试,不要构建它们。如果你设置$mail-&gt;exceptions = true,你应该得到一个例外。
  • 当pdf文件在目录中时。
  • 权限设置为 0644
猜你喜欢
  • 2014-08-14
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2013-03-15
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多