phpimapimap-open

Extract Email from Headers


Can't seem to get my code to work. Warning: preg_match_all() expects parameter 2 to be string, array given in /script.php on line 23. I am looking to retrieve a list of emails addresses:

<?php
$mbox = imap_open("{******.com:143/notls}", "******@******.com", "******");

echo "<h1>Email Addresses</h1>\n";
$headers = imap_headers($mbox);

if ($headers == false) {
echo "Call failed<br />\n";
} else {
foreach ($headers as $val) {
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
echo $maches[2] . "<br />\n";
}
}

imap_close($mbox);
?>

Solution

  • preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
    

    should be

    preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $val, $matches);
    

    You were looping through the array, but feeding the entire array into the preg_match_all function each loop.

    Also, you have a typo:

    echo $maches[2] . "<br />\n";
    

    should be

    echo $matches[2] . "<br />\n";