Mr. Developer Mr. Developer - 4 months ago 23
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******n.com:143/novalidate-cert}", "myEmail@b******n.com", "myPassWord");
$headers = imap_headers($mbox);
print_r($headers);
if ($headers == false) {
echo "Call failed<br />\n";
} else {
foreach ($headers as $val) {
echo $val . "<br />\n";
}
}
imap_close($mbox);


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

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}
    {$overview->subject}\n";
}
imap_close($mbox);