读取并解析邮件文件的头部

时间:2012-03-18 03:16:39

标签: php

我有一个邮件文件

我想要读取/解析邮件文件的头部(仅限头部)

我想读到这一行(Content-Type :)这是头部的结尾

这是文件内容

的示例
Received: from [41.43.66.49] by web121206.mail.ne1.yahoo.com via HTTP; Thu, 15 Mar 2012 03:01:18 PDT
X-Mailer: YahooMailWebService/0.8.117.340979
References: <20120315074428.615964901A4@sat51-alie.secureinternetnews.net>
Message-ID: <1331805678.39182.YahooMailNeo@web121206.mail.ne1.yahoo.com>
Date: Thu, 15 Mar 2012 03:01:18 -0700 (PDT)
From: Black Dream <more_ache@ymail.com>
Reply-To: Black Dream <more_ache@ymail.com>
Subject: Need help installing your certificate?
To: "admin@emiddy-hosting.com" <admin@emiddy-hosting.com>
In-Reply-To: <20120315074428.615964901A4@sat51-alie.secureinternetnews.net>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="1735753853-309369511-1331805678=:39182"

--1735753853-309369511-1331805678=:39182
Content-Type: multipart/alternative; boundary="1735753853-307665870-1331805678=:39182"

--1735753853-307665870-1331805678=:39182
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=0A=0A=0A----- Forwarded Message -----=0AComodo Group, Inc. <e-mail= .....
.... hello hello hello ... the message body here

这是我正在处理的代码

$the_mail_data = array ();
$handle = fopen('mail.txt', 'r');
while (($buffer = fgets($handle)) !== false) {
        $break = explode ( ':' , $buffer );
        $the_mail_data[$break[0]] = $break[1];
        if ( $break[0] == 'Content-Type' )
        break;
}
fclose($handle);
    //Now i've the data in this array $the_mail_data();
    //print_r ( $the_mail_data );
    //echo $the_mail_data['subject'];

代码工作得很好,但我需要更快的东西!

是否有人有更快的功能或更快的代码?

我正在尝试(file_get_contents)它看起来更快,但是它读取了所有文件,我无法读取文件的一部分

1 个答案:

答案 0 :(得分:0)

您可以使用shell中的sed根据Content-Type字符串将文件分成两部分,然后从系统命令中获取结果,从而获得最快的结果:

$head = system("sed '1,/^Content-Type$/!d' < mail.txt");

您可能需要mail.txt文件的完整路径。尝试一下并比较速度。