2014-10-20 111 views
2

我使用Mailparse解析和存储电子邮件在MySQL数据库。电子邮件直接传送到PHP脚本。超过99%的电子邮件被正确解析。但是,我注意到一些电子邮件正在被截断。看来问题是邮件的标题和正文之间的Unicode字符...对非ASCII字符PHP Mailparse扼流圈

Delivered-To: [email protected] 
Received: by 10.152.1.193 with SMTP id 1csp311490lao; 
     Mon, 20 Oct 2014 05:33:31 -0700 (PDT) 
Return-Path: <[email protected]> 
Received: from vps4596.inmotionhosting.com (vps4596.inmotionhosting.com. [74.124.217.238]) 
     by mx.google.com with ESMTPS id fb7si7786786pab.30.2014.10.20.05.33.30 
     for <[email protected]> 
     (version=TLSv1 cipher=RC4-SHA bits=128/128); 
     Mon, 20 Oct 2014 05:33:30 -0700 (PDT) 
Message-ID: <[email protected]> 
From: =?utf-8?Q?Annelen_geretschl=C3=A4ger?= <[email protected]> 
To: "neokio" <[email protected]> 
References: <[email protected]om> 
In-Reply-To: <[email protected]om> 
Subject: This message will be broken 
Date: Mon, 20 Oct 2014 14:33:24 +0200 
MIME-Version: 1.0 
Content-Type: multipart/alternative; 
    boundary="----=_NextPart_000_0018_01CFEC72.CE424470" 
X-Priority: 3 
X-MSMail-Priority: Normal 
Importance: Normal 
X-Mailer: Microsoft Windows Live Mail 14.0.8117.416 
X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8117.416 
X-Source: 
X-Source-Args: 
X-Source-Dir: 

Det här är ett flerdelat meddelande i MIME-format. 

------=_NextPart_000_0018_01CFEC72.CE424470 
Content-Type: text/plain; 
    charset="utf-8" 
Content-Transfer-Encoding: quoted-printable 

This is a test ... the above "Det här är" chunk will be cut off at "Det h", and nothing else will arrive. 

------=_NextPart_000_0018_01CFEC72.CE424470 

上面只是头后,将获得裁剪,和所有的到来是“挪威H”。不知何故,非ASCII字符(ü)导致mailparse在头文件或多部分包装器外部窒息。这可能是客户端使用的Microsoft Windows Live Mail的5年历史的瑞典版本,弄乱了标题等,但这不是理由,我需要能够接收它。

我正在运行PHP 5.4.30,它在php.ini中有default_charset = "utf-8"。但我注意到phpinfo()默认为mailparse.def_charset = "us-ascii",即使php.ini中没有配置它。添加该行并将其设置为“utf8”后,phpinfo()正确显示utf-8。但是,错误仍然存​​在。我没有想法。

有关如何处理此错误的任何建议?

+0

嗯,有趣。我还在编写邮件列表引擎,并使用'mailparse'解析源代码。是的,它在那个地方窒息..也会尝试找到解决方案) – Cheery 2014-10-20 21:50:01

+0

我有一个问题 - 你从哪里得到这个消息?它不符合标准,因为引用的文本版本应该是这是一个测试......上面的“Det h = C3 = A4r = C3 = A4r”块将在“Det h “,没有别的东西会到达。” - 试试它,而不是你的字符串。 – Cheery 2014-10-20 21:58:10

+0

是的,输出告诉相同的 - 警告:mailparse_msg_extract_part() - 过滤器转换失败。输入消息可能是错误编码的。您可以根据每个部分的'mailparse_msg_get_part_data'标题中的数据直接从文件中提取主体,但是您必须自己对其进行解码。 – Cheery 2014-10-20 23:33:10

回答

3

只是我在评论中提到的想法......这部分与消息部分有关。如果由于某种原因解码失败,则内容按原样返回。您可以尝试基于$headers['transfer-encoding'];对其进行解码,或者保持原样。 $email是带有标题的完整消息源。 $sectionmailparse_msg_get_part(手动,例子,谷歌)获得的数据

$headers = mailparse_msg_get_part_data($section); 
$content = ''; 

set_error_handler(function() use(&$content, $headers, $email){ 
    $start = $headers['starting-pos-body']; 
    $end  = $headers['ending-pos-body']; 
    $content = substr($email, $start, $end - $start); 
}); 

ob_start(); 
mailparse_msg_extract_part($section, $email); 
$body = ob_get_clean(); 

restore_error_handler(); 

if (!empty($content)) $body = $content; 

结果(一些操作后,我只能留下,我确实需要头)

["charset"]=> 
string(5) "utf-8" 
["content-charset"]=> 
string(5) "utf-8" 
["content-type"]=> 
string(10) "text/plain" 
["content"]=> 
string(108) "This is a test ... the above "Det här är" chunk will be cut off at "Det h", and nothing else will arrive. " 
+0

伟大的解决方案,谢谢:) – neokio 2014-10-21 09:48:28