简体   繁体   English

如何使用php中的imap_headers()分别获取电子邮件,主题和名称?

[英]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. 上面的代码是返回日期,电子邮件地址( if no name )和主题。

Example : [1118] => 1119)13-May-2016 Facebook You have more friends on (16765 chars) 示例: [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 . 如果电子邮件中没有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 谢谢

You might want to fetch message information with imap_fetch_overview : 您可能要使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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