开发者社区> 问答> 正文

这样的邮件头部该怎么解析呢 400 请求报错 

Subject: =?UTF-8?B?W0pJUkFdIChTUE1TLTIx?=  =?UTF-8?B?MjUpIOe8lui+keaIkOaenO+8jOS6uuW3peefqemYtemHjeWkjeWQjg==?=  =?UTF-8?B?77yM5Y+W5raI5oiQ5p6c77yM6YCA5Ye6?=  =?UTF-8?B?6aG555uu77yM5YaN5qyh57yW6L6R6aG5?=  =?UTF-8?B?55uu5LiA55u05o+Q56S65Lq65bel5YiG?=  =?UTF-8?B?6Kej55qE6YOo5omA5LiN5Y+v6YeN5aSN?=

这是一封邮件的头部主题,解析的这种类型的时候出错了。请问有碰到类似情况的同学吗?下面这种一行的用下面的php解析没有问题,多行的就会有问题。我手动把多行的连接成一行解析,仍然有乱码。请问有好的解析方法吗,什么编程语言都不限制了。

Subject: =?UTF-8?B?5rWL6K+V5rWL6K+V?=
function decode_mime_string($string)
{
    $en = mb_detect_encoding($string, "JIS, ASCII, EUC-CN, UTF8");
    $string = mb_convert_encoding($string, "utf-8", $en);
    $string = str_replace(array('\r\r\n','\r\n','\r','\n'),'',$string);
    preg_match_all( '/\=\?(.*?)\?([QqBb])\?(.*?)\?\=/is', $string, $arr );
    if(!isset($arr[3][0])){
        return $string;
    } else {
		$arr2 = explode(',', $string);
		$len = count($arr2);
		if ($len != count($arr[0])) {
			unset($arr[0], $arr[3]);
			foreach($arr2 as $key => $val){
				preg_match_all( '/\=\?(.*?)\?([QqBb])\?(.*?)\?\=/is', $val, $arr3 );
				$arr[0][$key] = implode(' ', $arr3[0]);
				$arr[3][$key] = implode(' ', $arr3[3]);
			}
		}
        $chararr = $arr[1];
        $encode = $arr[2];
        $enstr = $arr[3];
        foreach( $enstr as $key=>$val ){
            $encoding = $encode[$key];
            $encoded_text = $val;
            $charset = $chararr[$key];
            switch ($encoding)
            {
                case 'Q':
                case 'q':
                    $encoded_text = str_replace('_', '%20', $encoded_text);
                    $encoded_text = str_replace('=', '%', $encoded_text);
                    $decoded = urldecode($encoded_text);
                    break;

                case 'B':
                case 'b':
                    $decoded = urldecode(base64_decode($encoded_text));
                    break;

                default:
                    $decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';
                    break;
            }
            $charset = strtolower($charset);
            if ( $charset != 'utf8' && $charset != 'utf-8' )
                $decoded = mb_convert_encoding($decoded, "utf-8","gbk,gb2312");
            $string = str_replace($arr[0][$key],$decoded,$string);
        }
        return $string;
    }
}

展开
收起
kun坤 2020-05-30 21:09:08 663 0
1 条回答
写回答
取消 提交回答
  • 结帖
    http://stackoverflow.com/questions/8626786/proper-way-to-decode-incoming-email-subject-utf-8

    2020-05-30 21:10:24
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像