2016-03-29 89 views
0

我似乎無法獲取通過PHPMailer發送的附件。以下是我對代碼發送電子郵件:PHPMailer&Dompdf附件問題

修訂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)。附件,但是拒絕工作......

+0

那麼你保存你的服務器上的文件發送前它與phpmailer? – Naruto

+0

@Naruto首先,很酷的名字; D。其次,如果我不需要,我不想將文件保存到服務器,如果需要,我只想將它保存到臨時文件中。第三,我對PHPAutoMailer非常新鮮,並且不確定如何做到這一點:D最好的評論是,我會盡最大努力,即使這意味着我必須保存到服務器。 – Steven

+0

'$ dompdf-> stream()'方法用於在用戶瀏覽器上打開一個下載對話框。所以你不能用它來附件。下面使用火影忍者的方法。 – ThinkTank

回答

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