PHPMailer& Dompdf附件问题

时间:2016-03-29 14:26:18

标签: php dompdf

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

修改后的Automailer内容:

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)。然而,附件拒绝工作......

1 个答案:

答案 0 :(得分:0)

工作代码:

服务器代码:

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);