2010-07-10 71 views
-1

我有很好的代碼,它用來做工作。但現在我無法使它工作。普通郵件()的作品,但是這個代碼不:PDF作爲郵件附件 - 郵件()返回false,沒有錯誤

<?php 
    ob_start(); 
    error_reporting(E_ALL|E_STRICT); 
    ini_set('display_errors', 1); 
    require_once('dompdf_config.inc.php'); 


    $tpl = '<html> 
<style type="text/css"> 
.bg1 {background-color:#8FE6F9; font-weight:bold;} 
body {font-family: Arial; font-face: Arial;} 
</style> 
<body> 

<table width="98%" border="0" align="center"> 
<tr><td colspan="3" align="center"></td></tr> 
<tr> 
    <td width="35%">&nbsp;</td> 
    <td align="center" width="28%" nowrap="nowrap" style="font-size: 18px;"> <b>TYTUL</b><BR></td> 
    <td align="right" width="35%">Faktura nr: <b>S-1000 ąśżźć󳥌ŻŹĆŁÓ</b> <br>Data: <b>data</b></td> 
</tr> 
</table> 
    </body>'; 

    $tpl .= '</body></html>'; 

    $encoding = 'Windows-1250'; 

    $dompdf = new DOMPDF(); 
    $dompdf->load_html(iconv('UTF-8', $encoding.'//IGNORE', $tpl)); 
    $dompdf->render(); 


    $mode = 'send'; 

    if($mode == 'send') 
    { 
$name = 'raport.pdf'; 
$fullpath_invoice = $name; 

if(file_put_contents($fullpath_invoice, $dompdf->output())) 
{ 
    $message = 'jakas tresc w HTML'; 

    $BOUNDARY_1 = '------------3020208030402090901'.rand(10000, 99999); 
    $BOUNDARY_2 = '------------3020208030402090902'.rand(10000, 99999); 

    $rand_1 = md5(rand(0, 99999).microtime()); 
    $rand_2 = md5(rand(0, 99999).microtime()); 

    $output = "This is a multi-part message in MIME format.\n"; 
    $output .= "--$BOUNDARY_1\n"; 
    $output .= "Content-Type: multipart/alternative;\n boundary=\"$BOUNDARY_2\"\n\n"; 

    $output .= "--$BOUNDARY_2\n"; 
    $output .= "Content-Type: text/plain; charset=UTF-8; format=flowed\n"; 
    $output .= "Content-Transfer-Encoding: 7bit\n\n"; 

    $message_unhtml = preg_replace('#<a href="(.*?)">(.*?)</a>#', '\\1', str_replace(array("\n", "\r"), '', $message)); 
    $message_unhtml = str_replace('<br>', "\n", $message_unhtml); 
    $message_unhtml = strip_tags($message_unhtml); 
    $message_HTML = $message; 

    $message = $output; 
    $message .= $message_unhtml."\n\n"; 

    $message .= "--$BOUNDARY_2\n"; 

    $message .= "Content-Type: text/html; charset=UTF-8\n"; 
    $message .= "Content-Transfer-Encoding: 7bit\n\n"; 
    $message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> 
    <html> 
    <head> 

    <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"> 
    <style type=\"text/css\"> 
    body{ 
    background:#FFFFFFF; 
    font-family: Tahoma, Helvetica, Verdana, Arial, sans-serif; 
    font-size:13px; 
    color:#2D3235; 
    }; 
    </style> 
    </head> 
    <body> 
    ".$message_HTML." 
    </body> 

    </html>\n\n"; 

    $message .= "--$BOUNDARY_2--\n\n"; 

    if(file_exists($fullpath_invoice)) 
    { 
    $file = fopen($fullpath_invoice, "rb"); 
    $data = fread($file, filesize($fullpath_invoice)); 
    fclose($file); 
    $data = chunk_split(base64_encode($data)); 

    $message .= "--$BOUNDARY_1\n"; 
    $message .= "Content-Type: application/pdf;\n name=\"$name.pdf\"\n"; 
    $message .= "Content-Disposition: attachment;\n filename=\"$name.pdf\"\n"; 
    $message .= "Content-Transfer-Encoding: base64\n\n"; 
    $message .= "$data\n\n"; 

    unset($data); 
    } 
    $message .= "--$BOUNDARY_1--\n"; 

    $headers = "Content-Type: multipart/mixed;\n boundary=\"$BOUNDARY_1\"\n\n"; 

    if(mail('[email protected]', 'pdf', $message, $headers)) 
    { 
    echo 'Wysłano'; 
    } 
    else 
    { 
    echo 'Błąd'; 
    } 
} 
    } 
    else 
    { 
$dompdf->stream('nazwa_pliku.pdf'); 
    } 

    ob_end_flush(); 

    ?> 

此代碼的工作在一臺服務器上,但沒有其他。可能是什麼原因呢?如果需要,我可以附上phpinfo或其他東西。

+0

這是很難閱讀,格式化這樣。你能清理它嗎? – Borealid 2010-07-10 19:32:36

+0

也許DOMPDF將錯誤報告返回關閉?你可以嘗試把線放在下面嗎? – 2010-07-10 19:35:08

回答

1

你自己構建MIME消息的任何原因?使用諸如PHPMailer之類的東西對你來說只需要少得多的麻煩,而且如果在郵件結尾發生錯誤,它也會給出更好的反饋。

我認爲它沒有顯示在PHP和/或Apache錯誤日誌中?服務器突然降低了內存限制,安裝了新版本的PDF庫等...?

+0

那麼,它不能在此服務器上工作。在我的服務器上相同的代碼工作我只想知道,哪些設置可以使其工作。我會嘗試使用PHPMailer,但是我只是爲CMS做一些小插件。 – Misiur 2010-07-11 11:22:49

+0

PHPMailer Lite解決了我的問題;) – Misiur 2010-07-11 13:13:36