phpreportpearmail-queue

pear mail queue report script?


Is there a pear mail queue reporting script that builds pretty charts and graphs from your MQ database? I have MQ set up on a cron job and I want to tie some reporting into my admin console.


Solution

  • Not 'natively', but you could use the new callback support in version 1.2.3 to populate a log table in your database and from that generate your reports. The callback function is called before the relevant entry is deleted from the mail_queue table in the database so if necessary you could add extra fields to it for inserting into your log/report table.

    You'll need to use recent releases of the Mail and Net_SMTP PEAR packages to be able to retrieve the esmtp id and greeting details if you need them for your reports. Also, if you want to decode the body of the email and store that for your report you'll need to install the Mail_mimeDecode PEAR package.

    Provide the name of the callback function like so:

    $dn = $mail_queue->sendMailsInQueue(
        MAX_AMOUNT_MAILS,
        MAILQUEUE_START,  
        MAILQUEUE_MAX_RETRY,
        "callback_fn");
    
    function callback_fn($args) {
        $row = get_mail_queue_row($args['id']);
        $headers = unserialize($row['headers']);
        $subject = $headers['Subject'];
        $body = unserialize($row['body']);
    
        $mh = '';
        foreach($headers as $key=>$value) {
            $mh .= "$key:$value\n";
        }
        $mail = $mh . "\n" . $body;
        $decoder = new Mail_mimeDecode($mail);
        $decoded = $decoder->decode(array(
            'include_bodies' => TRUE,
            'decode_bodies'  => TRUE,
            'decode_headers'  => TRUE,
        ));
        $body = $decoded->body;
    
        if (isset($args['greeting'])) {
            $greeting = $args['greeting'];
            $greets = explode(" ", $greeting);
            $detail =  "esmtp id: {$args['queued_as']}; server: {$greets[0]}";
        } else {
            $detail =  "esmtp id: {$args['queued_as']}; server: localhost";
        }
    
        insert_to_log($detail, $subject,...);
    }