简体   繁体   中英

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);
?>
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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM