解码传入电子邮件主题的正确方法(utf 8)

max*_*max 12 php email

我正在尝试将传入的邮件传递给PHP脚本,以便将它们存储在数据库和其他内容中.我正在使用类MIME电子邮件解析器(需要注册),虽然我认为这不重要.

我的电子邮件主题有问题.当标题是英文时,它工作正常,但如果主题使用非拉丁字符我得到类似的东西

=?UTF-8?B?2KLYstmF2KfbjNi0?=
Run Code Online (Sandbox Code Playgroud)

对于像یکدوسه这样的头衔

我像这样解码主题:

  $subject  = str_replace('=?UTF-8?B?' , '' , $subject);
  $subject  = str_replace('?=' , '' , $subject);      
  $subject = base64_decode($subject); 
Run Code Online (Sandbox Code Playgroud)

它适用于10-15个字符的短主题,但标题较长,我最终会得到原始标题的一半,类似于 .

如果标题更长,比如30个字符,我什么也得不到.我这样做了吗?

raz*_*zed 15

尽管这已经差不多一年了 - 我发现了这个并且面临着类似的问题.

我不确定你为什么会得到奇怪的字符,但也许你试图在你的字符集不受支持的某个地方展示它们.

这里是我编写的一些代码,它应该处理除charset转换之外的所有内容,这是一个很大的问题,许多库处理得更好.(例如PHP的MB库)

class mail {
    /**
      * If you change one of these, please check the other for fixes as well
     *
     * @const Pattern to match RFC 2047 charset encodings in mail headers
     */
    const rfc2047header = '/=\?([^ ?]+)\?([BQbq])\?([^ ?]+)\?=/';

    const rfc2047header_spaces = '/(=\?[^ ?]+\?[BQbq]\?[^ ?]+\?=)\s+(=\?[^ ?]+\?[BQbq]\?[^ ?]+\?=)/';

    /**
     * http://www.rfc-archive.org/getrfc.php?rfc=2047
     *
     * =?<charset>?<encoding>?<data>?=
     *
     * @param string $header
     */
    public static function is_encoded_header($header) {
        // e.g. =?utf-8?q?Re=3a=20Support=3a=204D09EE9A=20=2d=20Re=3a=20Support=3a=204D078032=20=2d=20Wordpress=20Plugin?=
        // e.g. =?utf-8?q?Wordpress=20Plugin?=
        return preg_match(self::rfc2047header, $header) !== 0;
    }

    public static function header_charsets($header) {
        $matches = null;
        if (!preg_match_all(self::rfc2047header, $header, $matches, PREG_PATTERN_ORDER)) {
            return array();
        }
        return array_map('strtoupper', $matches[1]);
    }

    public static function decode_header($header) {
        $matches = null;

        /* Repair instances where two encodings are together and separated by a space (strip the spaces) */
        $header = preg_replace(self::rfc2047header_spaces, "$1$2", $header);

        /* Now see if any encodings exist and match them */
        if (!preg_match_all(self::rfc2047header, $header, $matches, PREG_SET_ORDER)) {
            return $header;
        }
        foreach ($matches as $header_match) {
            list($match, $charset, $encoding, $data) = $header_match;
            $encoding = strtoupper($encoding);
            switch ($encoding) {
                case 'B':
                    $data = base64_decode($data);
                    break;
                case 'Q':
                    $data = quoted_printable_decode(str_replace("_", " ", $data));
                    break;
                default:
                    throw new Exception("preg_match_all is busted: didn't find B or Q in encoding $header");
            }
            // This part needs to handle every charset
            switch (strtoupper($charset)) {
                case "UTF-8":
                    break;
                default:
                    /* Here's where you should handle other character sets! */
                    throw new Exception("Unknown charset in header - time to write some code.");
            }
            $header = str_replace($match, $data, $header);
        }
        return $header;
    }
}
Run Code Online (Sandbox Code Playgroud)

当运行脚本并使用UTF-8在浏览器中显示时,结果是:

آزمایش

你会像这样运行它:

$decoded = mail::decode_header("=?UTF-8?B?2KLYstmF2KfbjNi0?=");
Run Code Online (Sandbox Code Playgroud)


TiM*_*TER 15

您可以使用该mb_decode_mimeheader()函数解码您的字符串.


小智 7

使用php本机功能

<?php
mb_decode_mimeheader($text);
?>
Run Code Online (Sandbox Code Playgroud)

此函数可以处理utf8以及iso-8859-1字符串.我测试了它.


小智 5

使用php函数:

<?php
imap_utf8($text);
?>
Run Code Online (Sandbox Code Playgroud)