PDF作为邮件附件 - mail()返回false,没有错误

时间:2010-07-10 19:28:47

标签: php email pdf

我有很好的代码,它用来做工作。但现在我无法使其发挥作用。普通邮件()有效,但此代码不起作用:

 <?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('misiur66@gmail.com', 'pdf', $message, $headers))
  {
   echo 'Wysłano';
  }
  else
  {
   echo 'Błąd';
  }
 }
    }
    else
    {
 $dompdf->stream('nazwa_pliku.pdf');
    }

    ob_end_flush();

    ?>

此代码适用于一台服务器,但不适用于其他服务器。可能是什么原因造成的?如果需要,我可以附上phpinfo或其他东西。

1 个答案:

答案 0 :(得分:1)

您自己构建MIME邮件的原因是什么?使用类似PHPMailer之类的东西可以减少麻烦,并且如果邮件末端发生错误,也可以提供更好的反馈。

我在PHP和/或Apache错误日志中没有显示任何内容?服务器突然减少了内存限制,安装了新版本的PDF库等......?