views:

19

answers:

1

I am using Zend_Mail to send an email of a log file, but it is sending dodgy characters that the mail clients don't like.

My code:

<?php
$sBody = "errors.log (>= WARN):\n\n";
$rFile = fopen($sErrorsLog, "r");
while (!feof($rFile))
{
    $sLine = fgets($rFile);
    $sBody.= $sLine;
}

$oMail = new Zend_Mail();
$oMail->addTo($sTo)
      ->setFrom($sFrom)
      ->setSubject($sSubject)
      ->setBodyText($sMessage);

The email body I receive:

errors.log (>= WARN):

timestamp|2010-07-05T09:48:03+10:00 message|O:11:"ArrayObject":3:{s:9:"exception";O:32:"Zend_Controller_Action_Exception":7:{s:25:"

The error log contains (this is an extract from the top)

timestamp|2010-07-05T09:48:03+10:00 message|O:11:"ArrayObject":3:{s:9:"exception";O:32:"Zend_Controller_Action_Exception":7:{s:25:"Zend_Exception_previous";N;s:10:"*message";s:64:"Action "crgdtgdf" does not exist and was not trapped in __call()";s:17:"Exceptionstring";s:0:"";s:7:"*code";i:404;s:7:"*file";s:73:"/var/www/development/workspaces/s.rees/library/Zend/Controller/Action.php";s:7:"*line";i:485;s:16:"Exceptiontrace";a:4:{i:0;a:6:{s:4:"file";s:73:"/var/www/development/workspaces/s.rees/library/Zend/Controller/Action.php";s:4:"line";i:515;s:8:"function";s:6:"__call";s:5:"class";s:22:"Zend_Controller_Action";s:4:"type";s:2:"->";s:4:"args";a:2:{i:0;s:14:"crgdtgdfAction";i:1;a:0:{}}}i:1;a:6:

Any ideas how I can filter/encode the email so all of the log output is shown? I really need it emailed out with all the details.

+1  A: 

PHP's serialized data includes a null byte before the private or protected members of any serialized object.

You are going to need to either unserialize and reprocess the output, or you're going to need to strip the null bytes before sending them out in an email.

Charles
Ah, that makes sense. Is there a simple way to strip out null bytes?
Valorin
I just did str_replace("\0", "", $sLine); and it appears to work :)Thanks!
Valorin
That is pretty much the canonical way to do it, yeah. :)
Charles