imap_headerinfo
(PHP 4, PHP 5)
imap_headerinfo — Read the header of the message
Description
object imap_headerinfo
( resource
$imap_stream
, int $msg_number
[, int $fromlength
= 0
[, int $subjectlength
= 0
[, string $defaulthost
= NULL
]]] )Gets information about the given message number by reading its headers.
Parameters
-
imap_stream
-
An IMAP stream returned by imap_open().
-
msg_number
-
The message number
-
fromlength
-
Number of characters for the fetchfrom property. Must be greater than or equal to zero.
-
subjectlength
-
Number of characters for the fetchsubject property Must be greater than or equal to zero.
-
defaulthost
-
Return Values
Returns the information in an object with following properties:
- toaddress - full to: line, up to 1024 characters
- to - an array of objects from the To: line, with the following properties: personal, adl, mailbox, and host
- fromaddress - full from: line, up to 1024 characters
- from - an array of objects from the From: line, with the following properties: personal, adl, mailbox, and host
- ccaddress - full cc: line, up to 1024 characters
- cc - an array of objects from the Cc: line, with the following properties: personal, adl, mailbox, and host
- bccaddress - full bcc: line, up to 1024 characters
- bcc - an array of objects from the Bcc: line, with the following properties: personal, adl, mailbox, and host
- reply_toaddress - full Reply-To: line, up to 1024 characters
- reply_to - an array of objects from the Reply-To: line, with the following properties: personal, adl, mailbox, and host
- senderaddress - full sender: line, up to 1024 characters
- sender - an array of objects from the Sender: line, with the following properties: personal, adl, mailbox, and host
- return_pathaddress - full Return-Path: line, up to 1024 characters
- return_path - an array of objects from the Return-Path: line, with the following properties: personal, adl, mailbox, and host
- remail -
- date - The message date as found in its headers
- Date - Same as date
- subject - The message subject
- Subject - Same as subject
- in_reply_to -
- message_id -
- newsgroups -
- followup_to -
- references -
- Recent - R if recent and seen, N if recent and not seen, ' ' if not recent.
- Unseen - U if not seen AND not recent, ' ' if seen OR not seen and recent
- Flagged - F if flagged, ' ' if not flagged
- Answered - A if answered, ' ' if unanswered
- Deleted - D if deleted, ' ' if not deleted
- Draft - X if draft, ' ' if not draft
- Msgno - The message number
- MailDate -
- Size - The message size
- udate - mail message date in Unix time
-
fetchfrom - from line formatted to fit
fromlength
characters -
fetchsubject - subject line formatted to fit
subjectlength
characters
See Also
- imap_fetch_overview() - Read an overview of the information in the headers of the given message
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с почтой
- IMAP, POP3 и NNTP
- imap_8bit
- imap_alerts
- imap_append
- imap_base64
- imap_binary
- imap_body
- imap_bodystruct
- imap_check
- imap_clearflag_full
- imap_close
- imap_create
- imap_createmailbox
- imap_delete
- imap_deletemailbox
- imap_errors
- imap_expunge
- imap_fetch_overview
- imap_fetchbody
- imap_fetchheader
- imap_fetchmime
- imap_fetchstructure
- imap_fetchtext
- imap_gc
- imap_get_quota
- imap_get_quotaroot
- imap_getacl
- imap_getmailboxes
- imap_getsubscribed
- imap_header
- imap_headerinfo
- imap_headers
- imap_last_error
- imap_list
- imap_listmailbox
- imap_listscan
- imap_listsubscribed
- imap_lsub
- imap_mail_compose
- imap_mail_copy
- imap_mail_move
- imap_mail
- imap_mailboxmsginfo
- imap_mime_header_decode
- imap_msgno
- imap_num_msg
- imap_num_recent
- imap_open
- imap_ping
- imap_qprint
- imap_rename
- imap_renamemailbox
- imap_reopen
- imap_rfc822_parse_adrlist
- imap_rfc822_parse_headers
- imap_rfc822_write_address
- imap_savebody
- imap_scan
- imap_scanmailbox
- imap_search
- imap_set_quota
- imap_setacl
- imap_setflag_full
- imap_sort
- imap_status
- imap_subscribe
- imap_thread
- imap_timeout
- imap_uid
- imap_undelete
- imap_unsubscribe
- imap_utf7_decode
- imap_utf7_encode
- imap_utf8
Коментарии
If you want to extract values from to, from, or other header elements, they are an object and you need to loop over them i.e.
$header = imap_header($mbox, $message_id);
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
$fromaddress = $object->mailbox . "@" . $object->host;
}
Would give you two variables for the friendly from and the smtp from address
Thanks to www.natrak.net for help with this
I'm not entirely sure why this is, but if you loop through all of the messages in a mailbox, calling imap_header() each time, you can significantly increase performance by calling imap_headers() first.
Compare this:
<?php
$imap = imap_open("{my.server.com:143}INBOX", "user", "pass");
$n_msgs = imap_num_msg($imap);
$s = microtime(true);
for ($i=0; $i<$n_msgs; $i++) {
$header = imap_header($imap, $i);
}
$e = microtime(true);
echo ($e - $s);
imap_close($imap);
?>
With this:
<?php
$imap = imap_open("{my.server.com:143}INBOX", "user", "pass");
$n_msgs = imap_num_msg($imap);
/****** adding this line: ******/
imap_headers($imap)
/***************************/
$s = microtime(true);
for ($i=0; $i<$n_msgs; $i++) {
$header = imap_header($imap, $i);
}
$e = microtime(true);
echo ($e - $s);
imap_close($imap);
?>
The performance difference, as I have tested on several boxes, connecting to several different servers, is that the second code snippet ALWAYS takes 1/2 the time, if not less.
Perhaps it is because imap_headers() retrieves all of the messages on one connection, whereas imap_header() has to make a new fetch request for each message?? I'm not sure WHY it is faster if imap_headers() is called first, but I do know that it is, so I thought I'd pass on the knowledge. If anyone knows why this is, please let me know....
When I was testing imap_headerinfo() with an e-mail that had multiple recipients (multiple e-mails in to to: and/or cc: field), I noticed that imap_headerinfo() was failing hard for me on PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli).
Rather than providing me an array with each and every e-mail address listed in the to and/or cc fields, it was only providing me the first listed. This was disappointing.
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => game
[host] => blunts.com
)
)
Luckily, there was a cool workaround to this problem:
imap_rfc822_parse_headers(imap_fetchheader( string ); which subsequentally worked like a nice little pet would:
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => game
[host] => blunts.com
)
[1] => stdClass Object
(
[mailbox] => dutch
[host] => masters.com
)
)
TL;DR:
So in other words, instead of imap_headerinfo() use imap_rfc822_parse_headers(imap_fetchheader()).
Hope this helps anyone else that runs into this issue from now into the future. This post was suggested by people in #PHP on FreeNode IRC.
An email without a subject line will not generate the 'subject' property.
Before using the 'subject' property you should test for it.
if (property_exists($header, 'subject')) echo $header->subject;
Please Note : imap_headerinfo only returns a subset of the headers, rather than the entire thing.
Among other things, this means it only shows the first recipient from the "to" section of the email, rather than all recipients.
If you're not seeing something you expected to, you'll be better off using
$hdr_raw = imap_fetchheader($mbox, $mailid);
$hdr = imap_rfc822_parse_headers($hdr_raw);
then you'll see the full set of headers, and be able to do more with it.