在php imap中获取X-Mailer属性

时间:2011-04-12 06:12:23

标签: 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);

我得到的输出是

 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

"

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

免责声明:以下内容使用了其他代码,但我确信它会帮助其他人(在我的情况下,我需要这样做,因为ddeboer/imap库不支持标头)。

Peter's solution很棒,但是我不得不使用fred727's regular expression来避免获取带有换行符的键。

由于电子邮件可以多次具有给定的标题,所以我不得不使用允许重复密钥的an array_combine() variant

这是解析RFC822标头部分字符串的最终代码(获取此部分超出范围):

function parse_rfc822_headers(string $header_string): array {
    // Reference:
    // * Base: https://stackoverflow.com/questions/5631086/getting-x-mailer-attribute-in-php-imap/5631445#5631445
    // * Improved regex: https://stackoverflow.com/questions/5631086/getting-x-mailer-attribute-in-php-imap#comment61912182_5631445
    preg_match_all(
        '/([^:\s]+): (.*?(?:\r\n\s(?:.+?))*)\r\n/m',
        $header_string,
        $matches
    );
    $headers = array_combine_groupkeys($matches[1], $matches[2]);
    return $headers;
}

function array_combine_groupkeys(array $keys, array $values): array {
    // Reference: "welcome at el hyphen mustafa" on 2015-11-29 09:46 (http://php.net/manual/fr/function.array-combine.php#118397)
    $result = [];
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk(
        $result, 
        function (&$v) {
            $v = (count($v) === 1) ? array_pop($v): $v;
        }
    );
    return $result;
}

哪个效果很好。这是一个使用相同的$string(添加了Received标头)的示例:

const RFC2822_EOL = "\r\n";

$string = 
'MIME-Version: 1.0' . RFC2822_EOL .
'Received: by 10.42.228.195; Wed, 16 Feb 2011 21:18:09 -0800 (PST)' . RFC2822_EOL .
'Received: by 127.0.0.1; Wed, 16 Feb 2011 21:18:06 -0800 (PST)' . RFC2822_EOL .
'Date: Wed, 16 Feb 2011 21:18:06 -0800' . RFC2822_EOL .
'Message-ID: <AANLkTikj8NgGgkG=Of=V6VvNSt2QZ3WLNKUVZxpcs4tk@mail.gmail.com>' . RFC2822_EOL .
'Subject: Get Gmail on your mobile phone' . RFC2822_EOL .
'From: Gmail Team <mail-noreply@google.com>' . RFC2822_EOL .
'To: test case2 <email@gmail.com>' . RFC2822_EOL .
'Content-Type: multipart/alternative; boundary=20cf302234f1c34163049c73853c' . RFC2822_EOL .
'' . RFC2822_EOL;

var_dump(parse_rfc822_headers($string));

哪个输出:

array(8) {
  ["MIME-Version"]=>
  string(3) "1.0"
  ["Received"]=>
  array(2) {
    [0]=>
    string(55) "by 10.42.228.195; Wed, 16 Feb 2011 21:18:09 -0800 (PST)"
    [1]=>
    string(51) "by 127.0.0.1; Wed, 16 Feb 2011 21:18:06 -0800 (PST)"
  }
  ["Date"]=>
  string(31) "Wed, 16 Feb 2011 21:18:06 -0800"
  ["Message-ID"]=>
  string(62) "<AANLkTikj8NgGgkG=Of=V6VvNSt2QZ3WLNKUVZxpcs4tk@mail.gmail.com>"
  ["Subject"]=>
  string(30) "Get Gmail on your mobile phone"
  ["From"]=>
  string(36) "Gmail Team <mail-noreply@google.com>"
  ["To"]=>
  string(28) "test case2 <email@gmail.com>"
  ["Content-Type"]=>
  string(60) "multipart/alternative; boundary=20cf302234f1c34163049c73853c"
}