PHP 8.3.4 Released!

imap_fetchheader

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_fetchheaderBir iletinin başlığını döndürür

Açıklama

imap_fetchheader(IMAP\Connection $imap, int $ileti_num, int $seçenekler = 0): string|false

Belirtilen iletinin » RFC2822 biçemli başlığının tamamını herhangi bir işlemden geçirmeden döndürür.

Bağımsız Değişkenler

imap

IMAP\Connection nesnesi.

ileti_num

İleti numarası.

seçenekler

Olası seçenekler:

  • FT_UID - The iletinum bağımsız değişkeninin bir eşsiz kimlik olarak ele alınmasını sağlar.
  • FT_INTERNAL - Satırları CRLF'lerle meşrulaştırılmadan dizgenin "dahili" biçemiyle döndürülmesini sağlar.
  • FT_PREFETCHTEXT - Aynı anda ileti metninin de alınmasını sağlar. İletinin tam metni istendiği durumda (içeriğin yerel bir dosyaya kaydedilmek istenmesi gibi) IMAP bağlantısında ek bir RTT (gecikme) oluşmasını engeller.

Dönen Değerler

Belirtilen iletinin başlığını içeren bir dizge, başarısızlık durumunda false döner.

Sürüm Bilgisi

Sürüm: Açıklama
8.1.0 imap bağımsız değişkeni artık IMAP\Connection nesnesi kabul ediyor, evvelce resource türünde geçerli bir imap değeri kabul ederdi.

Ayrıca Bakınız

add a note

User Contributed Notes 6 notes

up
7
rgagnon24 dot nospam at gmail dot com
13 years ago
Interesting that imap_headerinfo() does not allow a UID for the $msg_number field like all other fetching functions seem to allow.

If you want to use a UID to fetch the headers, use this two-step process:

<?php
/*
* assumes $mbox is your stream, and $uid is set
* properly. Proper error checking is up to you.
*/
$hText = imap_fetchbody($mbox, $uid, '0', FT_UID);
$headers = imap_rfc822_parse_headers($hText);
?>

The result is the same as the output of imap_headerinfo(), but you get to use the UID.
up
3
Jille at nomorecrap dot quis dot cx
15 years ago
<?PHP
$headers
=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>

Works quite well for splitting headers,
$matches will contain 3 arrays:
$matches[0] are the full-lines (To: Jille@devnull.quis.cx\r\n)
$matches[1] will be the header (To)
$matches[2] will be the value (Jille@devnull.quis.cx)

In multi-line values the 'multilining is not stripped!'
this could be achieved with something like:
<?PHP
preg_replace
('/\r\n\s+/m', '', $matches[2]);
?>
up
3
onofabio at gmail dot com
16 years ago
This function ignore that some header value have multiple lines...

<?php
// connecting to imap mailserver
$connection = @imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");

// get imap_fetch header and put single lines into array
$header = explode("\n", imap_fetchheader($connection, 1));

// browse array for additional headers
if (is_array($header) && count($header)) {
$head = array();
foreach(
$header as $line) {
// is line with additional header?
if (eregi("^X-", $line)) {
// separate name and value
eregi("^([^:]*): (.*)", $line, $arg);
$head[$arg[1]] = $arg[2];
}
}
}

// now are all contained additional headers in array $head
?>

I write this simple function....

$mbox = imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");
$mid=1 // Message id

// Get headers
$header = imap_fetchheader($mbox, $mid);

// Split on \n
$h_array=split("\n",$header);

foreach ( $h_array as $h ) {

// Check if row start with a char
if ( preg_match("/^[A-Z]/i", $h )) {

$tmp = split(":",$h);
$header_name = $tmp[0];
$header_value = $tmp[1];

$headers[$header_name] = $header_value;

} else {
// Append row to previous field
$headers[$header_name] = $header_value . $h;
}

}
up
0
dj_doktor at upskirt dot cz
18 years ago
I spend lot of time find out how I can detect messages with
different priorities. When I read users notes I remember
function imap_fetchheader which show header of message
and additional headers too. Because I'm so lazy to work with
regular expressions I required my frend for writing code
- thanx Znouza.
And there is... :)

<?php
// connecting to imap mailserver
$connection = @imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");

// get imap_fetch header and put single lines into array
$header = explode("\n", imap_fetchheader($connection, 1));

// browse array for additional headers
if (is_array($header) && count($header)) {
$head = array();
foreach(
$header as $line) {
// is line with additional header?
if (eregi("^X-", $line)) {
// separate name and value
eregi("^([^:]*): (.*)", $line, $arg);
$head[$arg[1]] = $arg[2];
}
}
}

// now are all contained additional headers in array $head
?>
up
-5
Max Geiger
17 years ago
If you call the function with the bitmask-parameter FT_PREFETCHTEXT the /Seen flag of the message will be set.
up
-4
george dot who at gmail dot com
8 years ago
Here is a simple function to get an array with all headers.

<?php
function mail_header_parse($mbox,$msg){
$header=imap_fetchheader($mbox,$msg);
preg_match_all("/^([^\r\n:]+)\s*[:]\s*([^\r\n:]+(?:[\r]?[\n][ \t][^\r\n]+)*)/m",$header,$matches,PREG_SET_ORDER);
foreach(
$matches as $match){
$match[2]=iconv_mime_decode($match[2],ICONV_MIME_DECODE_CONTINUE_ON_ERROR,'utf-8');
if(
is_array($headers[$match[1]])){
$headers[$match[1]][]=$match[2];
}elseif(isset(
$headers[$match[1]])){
$headers[$match[1]]=array($headers[$match[1]],$match[2]);
}else{
$headers[$match[1]]=$match[2];
}
}
return
$headers;
}
?>
To Top