在PHP中将电子邮件正文输出转换为TEXT文件

时间:2015-03-02 14:47:14

标签: php email base64 mime rfc822

我想使用PHP脚本将以下电子邮件转换为txt文件。即时通讯使用管道来发送邮件功能,但在运行脚本时会出现乱码。

#!/usr/bin/php -q
<?
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */  
$fdw = fopen("/home/mrsirajali/pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
?>

获得以下输出

From ****@arbtradersignals.com Sun Mar 01 13:00:44 2015
Received: from [84.19.34.200] (port=54307 helo=wintest2)
    by **********.prod.phx3.secureserver.net with esmtpsa (TLSv1:AES256-SHA:256)
    (Exim 4.84)
    (envelope-from <******@arbtradersignals.com>)
    id 1YSA2e-0002E3-Gc
    for *******@arbtradersignals.com; Sun, 01 Mar 2015 13:00:44 -0700
Message-ID: <c76708cf$20150301200044@wintest2>
User-Agent: MetaTrader 4 Terminal
MIME-Version: 1.0
From: "=?UTF-8?B?RXhwZXJ0IEFkdmlzb3I=?="
 <*****@arbtradersignals.com>
To: "=?UTF-8?B?QXJidHJhZGVyIFNpZ25hbHM=?="
 <*****@arbtradersignals.com>
Subject: =?UTF8?B?QXJidHJhZGVyU2lnbmFscyBDVVJSRU5UIE9QRU4gUE9TSVRJT05TICAjU0lHMQ==?=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64

ClBhaXIgfCBEaXJlY3Rpb24gfCBFbnRyeSB8IFNMIHwgVFAKClVTREpQWSB8IEJVWSB8IDExOS40
MCB8IDExNy43NiB8IDEyMC40OApVU0RDSEYgfCBCVVkgfCAwLjk1MTMgfCAwLjkzNjUgfCAwLjk2
MDEKVVNEQ0FEIHwgQlVZIHwgMS4yNTAxIHwgMS4yMzAwIHwgMS4yNjQ0CkdCUEpQWSB8IEJVWSB8
IDE4My43MiB8IDE4Mi42MCB8IDE4Ni4wOApFVVJKUFkgfCBTRUxMIHwgMTM0LjUyIHwgMTM2LjIy
IHwgMTMyLjk5CgpUb3RhbCBPcGVuIFBvc2l0aW9ucyA9IDU=

原始电子邮件的内容。

新待处理订单

配对|方向|进入| SL | TP |期满

USDCHF | BUYSTOP | 0.9513 | 0.9365 | 0.9581 | 2015.02.26 23:59

USDCAD | BUYSTOP | 1.2501 | 1.2300 | 1.2638 | 2015.02.26 23:59

EURUSD |卖家| 1.1330 | 1.1488 | 1.1222 | 2015.02.26 23:59

EURJPY |卖家| 134.52 | 136.22 | 133.42 | 2015.02.26 23:59

总待定订单数= 4

1 个答案:

答案 0 :(得分:0)

这不是胡言乱语,这是电子邮件的内容。如果要解码内容传输编码,只需将正文提供给base64解码器。在一般情况下,您需要(一个库)对MIME结构有很好的理解。