2011-04-12 54 views
2

如何在php imap lib中獲得X-Mailer屬性?在php中獲取X-Mailer屬性imap

我找不到任何屬性取功能http://php.net/manual/en/function.imap-fetchheader.php

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

    $header = imap_fetchheader($inbox, 1); 
    var_dump($header); 


/* close the connection */ 
imap_close($inbox); 

輸出我得到的是

string(405) "MIME-Version: 1.0 
Received: by 10.42.228.195; Wed, 16 Feb 2011 21:18:06 -0800 (PST) 
Date: Wed, 16 Feb 2011 21:18:06 -0800 
Message-ID: <[email protected]> 
Subject: Get Gmail on your mobile phone 
From: Gmail Team <[email protected]> 
To: test case2 <[email protected]> 
Content-Type: multipart/alternative; boundary=20cf302234f1c34163049c73853c 

" 

回答

8

方法:

  • 獲取與imap_fetchheader頭()
  • 從集流管10
  • 提取物X-梅勒場從場
  • 提取值

實施例:

$inbox = imap_open($hostname,$username,$password); 
if (!$inbox) { 
    echo('Cannot connect to Gmail: ' . imap_last_error()); 
    exit; 
} 
$header_string = imap_fetchheader($inbox, 1); 
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', 
     $header_string, $matches); 
$headers = array_combine($matches[1], $matches[2]); 
$xmailer = $headers['X-Mailer']; 
imap_close($inbox); 
+0

@Peter我添加代碼和O/P。 – 2011-04-12 07:03:15

+0

在您的示例中,郵件沒有X-Mailer標題字段。 – 2011-04-12 07:30:16

+0

@Vivek,我添加了一個例子,我測試了它,它對我來說工作正常。 – 2011-04-12 07:39:00