在php imap中获取X-Mailer属性

Viv*_*oel 2 php imap

我怎样才能在php imap lib中获得X-Mailer属性?

我找不到属性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);
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

 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: <AANLkTikj8NgGgkG=Of=V6VvNSt2QZ3WLNKUVZxpcs4tk@mail.gmail.com>
Subject: Get Gmail on your mobile phone
From: Gmail Team <mail-noreply@google.com>
To: test case2 <email@gmail.com>
Content-Type: multipart/alternative; boundary=20cf302234f1c34163049c73853c

"
Run Code Online (Sandbox Code Playgroud)

Pet*_*ist 8

方法:

  • 使用imap_fetchheader()获取标头
  • 从标题中提取X-Mailer字段
  • 从字段中提取值

例:

$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);
Run Code Online (Sandbox Code Playgroud)