【问题标题】:PHP - Dompdf and PHPMailer unexpected behaviorPHP - Dompdf 和 PHPMailer 意外行为
【发布时间】:2016-02-13 16:34:44
【问题描述】:

我正在开发一个使用 PHP 作为服务器端和 jQuery 框架作为客户端的 Web 应用程序。

该应用程序的一个场景是发送附有 pdf 文件的电子邮件,我正在使用 PHPMailer 和 Dompdf 库进行此操作。

我在文件名send.php 中创建了一个名为sendMail 的函数,接受4 个参数(收件人电子邮件、主题、数据、电子邮件号码)最后一个参数是因为我有5 个不同的电子邮件可能会根据情况发送data 参数是将数据呈现在 html 电子邮件正文中。

问题是当我从 send.php 调用函数时,它按预期工作,发送的电子邮件和创建并附加到电子邮件的 pdf 文件。

但是当我在任何其他文件中需要 send.php 并调用 sendMail 函数时,我只收到没有任何 pdf 文件的电子邮件,而且该文件甚至没有生成或保存在服务器上。

send.php

<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
require 'PHPMailerAutoload.php';
$body = "test message";
sendMail('peter@w34.co','MLS mail',$body,5);


function sendMail($email,$subject,$body,$index){

    $mail = new PHPMailer;

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth   = true; // SMTP authentication
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->Host       = "smtp.gmail.com"; // SMTP server
    $mail->Port       = 587; // SMTP Port
    $mail->Username   = "peterwilson.intake34@gmail.com"; // SMTP account username
    $mail->Password   = "Password";  // SMTP account password                                  // TCP port to connect to

    $mail->From = 'peter@example.com';
    $mail->FromName = 'Wilson';


    $mail->addAddress($email);     // Add a recipient

    $mail->isHTML(true);   // Set email format to HTML

    $mail->Subject = $subject;    
    $mail->Body=$body;

    // instantiate and use the dompdf class
    $dompdf = new Dompdf();
    $dompdf->loadHtml($body);
    // Render the HTML as PDF
    $dompdf->render();

    $output = $dompdf->output();
    $file_to_save= "../work-orderes/file.pdf";       
    file_put_contents($file_to_save, $output);

    $mail->addAttachment('../work-orderes/file.pdf');  


    if(!$mail->send()) {

    //    echo  $mail->ErrorInfo;
    } else {
        return 1;
    }
}
?>

save.php

<?php
 require("sendSmtp/send.php");
 session_start();
 $body = "Hello World!";        

 sendMail('peter@w34.co','MLS mail',$body,5);


 ?>

关于为什么从任何其他文件调用 Dompdf 时为什么不起作用的任何建议??

我的尝试

  • 删除 dompdf 并重新安装最新的稳定版本 here
  • 清理所有代码,然后离开 sendMail 函数并调用它
  • 尝试从头开始编写代码

【问题讨论】:

  • 一次做一件事,不要试图一次完成所有事情。您没有检查返回值(例如在 file_put_contents 上),并且您的代码基于过时的 PHPMailer 示例,因此请确保您使用的是 GitHub 的最新版本。这个问题也与昨天发布的几乎相同 - 这是某种任务吗?
  • @Synchro 感谢您的回复,我发布的代码是我的应用程序和邮件服务的真实代码,运行良好,我没有看到您所说的问题,请发布链接, 我会尝试你的想法先检查file_put_contents
  • 没有错误或警告?可能是自动加载器或文件保存位置的路径问题。 save.php 是否与 send.php 位于同一位置?

标签: php phpmailer dompdf


【解决方案1】:

我终于解决了!

调试后我发现一切正常,直到$output = $dompdf-&gt;output(); 之后我在使用$file_to_save= "../work-orderes/file.pdf"; 保存文件时遇到错误

当我从 send.php 调用函数 sendMail() 时,这可以正常工作,但是当我从另一个文件调用它时,我收到有关访问路径的错误。

这里的答案是我应该使用绝对路径而不是像下面这样的相对路径

$file_to_save= $_SERVER['DOCUMENT_ROOT']."\work-orderes/file.pdf";

现在文件已成功保存并按预期附加到电子邮件中!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多