在 php imap 中获取 X-Mailer 属性

发布于 2024-10-31 20:44:48 字数 1321 浏览 3 评论 0原文

如何在 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: <[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

"

how I can get X-Mailer attribute in php imap lib ?

I can't find any fetch function for attribute 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);

output I am getting is

 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

"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-11-07 20:44:48

方法:

  • 使用 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);

Method:

  • Fetch headers with imap_fetchheader()
  • Extract X-Mailer field from headers
  • Extract value from field

Example:

$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);
不知在何时 2024-11-07 20:44:48

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

Peter的解决方案很棒,但我必须使用fred727 的正则表达式以避免获取带有换行符的键。

因为一封电子邮件可以多次具有给定的标头,所以我必须使用 允许重复键的 array_combine() 变体

这是解析 RFC822 headers-part 字符串的最终代码(获取此类部分超出了范围):

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: <[email protected]>' . RFC2822_EOL .
'Subject: Get Gmail on your mobile phone' . RFC2822_EOL .
'From: Gmail Team <[email protected]>' . RFC2822_EOL .
'To: test case2 <[email protected]>' . 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) "<[email protected]>"
  ["Subject"]=>
  string(30) "Get Gmail on your mobile phone"
  ["From"]=>
  string(36) "Gmail Team <[email protected]>"
  ["To"]=>
  string(28) "test case2 <[email protected]>"
  ["Content-Type"]=>
  string(60) "multipart/alternative; boundary=20cf302234f1c34163049c73853c"
}

Disclaimer: The following uses code from other, but I am sure it would help others (in my case I needed this because ddeboer/imap library does not support headers).

Peter's solution is great but I had to use fred727's regular expression to avoid getting keys with newline characters.

Because an e-mail can have a given header multiple times, I had to use an array_combine() variant that allows duplicate keys.

Here is the final code to parse a RFC822 headers-part string (fetching such part is out of scope):

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;
}

Which works great. Here is an example using the same $string (with added Received header):

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: <[email protected]>' . RFC2822_EOL .
'Subject: Get Gmail on your mobile phone' . RFC2822_EOL .
'From: Gmail Team <[email protected]>' . RFC2822_EOL .
'To: test case2 <[email protected]>' . RFC2822_EOL .
'Content-Type: multipart/alternative; boundary=20cf302234f1c34163049c73853c' . RFC2822_EOL .
'' . RFC2822_EOL;

var_dump(parse_rfc822_headers($string));

Which outputs:

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) "<[email protected]>"
  ["Subject"]=>
  string(30) "Get Gmail on your mobile phone"
  ["From"]=>
  string(36) "Gmail Team <[email protected]>"
  ["To"]=>
  string(28) "test case2 <[email protected]>"
  ["Content-Type"]=>
  string(60) "multipart/alternative; boundary=20cf302234f1c34163049c73853c"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文