0

I'm having trouble with the executing the command mailq and turning it's output into values in a multidimensional array.

$results = array();

    exec('/usr/bin/mailq', $mailQresult);
    foreach ($mailQresult as $key => $mailqLine) {

        $res = preg_match('/^([A-Z0-9]{1,20})\s+([1-9][0-9]*)\s+((Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+[A-Za-z]{3}\s+[0-9]{2}\s+[0-9]{2}\:[0-9]{2}\:[0-9]{2})\s*$/', $mailqLine, $matches);
        if ($res) {
        // Insert message ID and other data into the object.
        // It will be competed afterwards.
        $mailqArray = array(
            'id' => $matches[1],
            'size' => intval($matches[2]),
            'date' => strftime($matches[3]),
            'sender' => $matches[5],
            'failed' => false,
            'recipients' => $matches[4]
            );
        }
    } 

The output of mailq includes white space and returns and looks a bit like this:

ID    SIZE DAYNAME MONTH DAY HOUR:MINUTES:SECONDS  sender@address.com [new line here]
(Host or domain name not found. Name service error for name=address.com type=MX: Host not found, try again) [new line here]
recipient@address.com

so the the error message and the recipient end up in different keys.

I know that I could use shell_exec to get the output of mailq as one string but I have no idea then how to write the regular expression that will create a multidimensional array.

I might be approaching this wrong. Any advice is extremely welcome!

4

1 回答 1

0

You could check the next item in the array to see if it contains a new messageid and other indicators. Probably a for loop instead of a foreach.

于 2013-01-17T13:17:47.247 回答