views:

85

answers:

2

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.

+2  A: 

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,...);
}
kguest
Great answer, but I'm not sure I have the bandwidth for this and will be looking at a service like postmarkapp.com ( I have no relation to the service)$1.50 per thousand emails sent sounds like savings to me until my user base grows considerably. When it does, I'll be back to digest this entirely!
robr
A: 

Does anyone know where to find the function get_mail_queue_row()?

mojoka