Mr. Developer Mr. Developer - 1 year ago 94
PHP Question

How to fetch email, subject and name separately using imap_headers() in php?

I'm trying to fetch the email address from email account. Following script is working fine for me.

$mbox = imap_open("{mail.b******}", "myEmail@b******", "myPassWord");
$headers = imap_headers($mbox);
if ($headers == false) {
echo "Call failed<br />\n";
} else {
foreach ($headers as $val) {
echo $val . "<br />\n";

Above code is returning date, email address (
if no name
) and subject.

Example :
[1118] => 1119)13-May-2016 Facebook You have more friends on (16765 chars)

[1192] => 1193)25-May-2016 John Re: B****c Website Feedba (27152 chars)

[1224] => 1225)30-May-2016 k****n@b***n.c DSR is not submitted prop (83005 chars)

Means above code returning the email if email has no
From: Name
. So is there any way to fetch the email addresses only? I would like to appreciate if someone guide me regarding this. Thank You

Answer Source

You might want to fetch message information with imap_fetch_overview:

$MC = imap_check($mbox);

$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
    echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download