tags:

views:

36

answers:

1

Hi. I've got good code, it used do work. But now I can't make it work. Normal mail() works, but this code doesn't:

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

    ?>

This code works on one server, but doesn't on other. What can be cause of it? If needed I can attach phpinfo or something.

+1  A: 

Any reason you're building the MIME message yourself? Using something like PHPMailer does it all for you with much less hassle, and will also give much better feedback if an error occurs in the mail end of things.

I take it nothing's showing up in the PHP and/or Apache error logs? Server suddenly has a reduced memory limit, new version of the PDF library installed, etc...?

Marc B
Well, it doesn't work on this server earlier. On my server same code works. I just want to know, which settings can make it work. I'll try with PHPMailer, however I'm doing just little plugin for CMS.
Misiur
PHPMailer Lite solved my problem ;)
Misiur