【问题标题】:PHPMailer & Dompdf Attachment IssuesPHPMailer & Dompdf 附件问题
【发布时间】:2016-07-17 04:18:02
【问题描述】:

我似乎无法通过 PHPMailer 发送附件。这是我发送电子邮件的代码:

修改后的自动邮件内容:

if(isset($_POST['type']) && $_POST['type'] == 'sendEmail' ){
    require_once 'phpMail/PHPMailerAutoload.php';
    $email = $_POST['email'];
    $uuid = $_POST['uuid'];
    $file_content = file_get_contents(BASE_PATH.'email.php?uuid='.$uuid);
    $pdf_content = file_get_contents(BASE_PATH.'pdf.php?uuid='.$uuid);
    $mail = new PHPMailer;
    $mail->From = ADMIN_EMAIL;
    $mail->FromName = SENDER_NAME;
    $mail->addAddress( $email );               // Name is optional
    $mail->addReplyTo(REPLY_EMAIL, 'Reply Mail');
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Email From '.User;
    $mail->Body    = $file_content;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->AddStringAttachment($output, attach.pdf);
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo "true";
    }

    exit;
}

修订的 Dompdf 代码(pdf.php)

    $html .= //Content Here
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
  if ($PAGE_NUM > 1) {
    $font = Font_Metrics::get_font("helvetica", "bold");
    $current_page = $PAGE_NUM-1;
    $total_pages = $PAGE_COUNT-1;
    $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
  }
');
$output = $dompdf->output();
file_put_contents('Attachment.pdf', $output);

如果我删除有关附件的代码,电子邮件发送就好了(正文中有 email.php)。附件,但是拒绝工作...

【问题讨论】:

  • 在使用 phpmailer 发送之前,您是否将文件保存在服务器上?
  • @Naruto 首先,很酷的名字;D。其次,如果不需要,我不想将文件保存到服务器,如果必须,我希望它只保存到临时文件,第三,我对 PHPAutoMailer 很陌生,不确定如何做到这一点:D 最好的评论是我会采用最佳实践,即使这意味着我必须保存到服务器。
  • $dompdf->stream() 方法用于在用户浏览器上打开下载对话框。因此,您不能将其用于附件。使用下面的火影忍者方法。
  • 嗯,把它保存到服务器,邮寄,然后从服务器中删除?我认为(不是 100% 肯定),该文件需要存在才能邮寄。和thx :)
  • @Naruto 假装我是一只大猩猩(或什么)。是否有在线指南/教程/任何东西可以告诉我如何将其保存到服务器,然后使用 phpmailer 检索它?

标签: php dompdf


【解决方案1】:

工作代码:

服务器代码:

if(isset($_POST['type']) && $_POST['type'] == 'sendEmail' ){
    require_once 'phpMail/PHPMailerAutoload.php';
    $email = $_POST['email'];
    $uuid = $_POST['uuid'];
    $file_content = file_get_contents(BASE_PATH.'email.php?uuid='.$uuid);
    $pdf_content = file_get_contents(BASE_PATH.'pdfemail.php?uuid='.$uuid);
    $mail = new PHPMailer;
    $mail->From = ADMIN_EMAIL;
    $mail->FromName = SENDER_NAME;
    $mail->addAddress( $email );               // Name is optional
    $mail->addReplyTo(REPLY_EMAIL, 'Reply Mail');
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Project From '.COMPANY_NAME;
    $mail->Body    = $file_content;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->AddAttachment('./attachments/attach.pdf', "Attachment.pdf");
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo "true";
    }

    exit;
}

Dompdf 文件:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
  if ($PAGE_NUM > 1) {
    $font = Font_Metrics::get_font("helvetica", "bold");
    $current_page = $PAGE_NUM-1;
    $total_pages = $PAGE_COUNT-1;
    $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
  }
');
$output = $dompdf->output();
$file_to_save = './attachments/attach.pdf';
file_put_contents($file_to_save, $output);

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多