0

目前我正在直接从输入流中读取邮件,然后对其进行一些操作,然后我需要发送修改后的邮件。

我已经成功阅读并修改了邮件,现在可以将邮件作为原始邮件转发给用户了。

因此,为此我使用了邮件功能,但此邮件功能以原始格式发送消息,因为我正在阅读邮件并在正文中设置变量。如何将其作为看起来像格式的原始邮件(原始格式转换为实际邮件?)

#!/usr/bin/php -q
<?php

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
    $email .= fread($fd, 1024);
}
fclose($fd);


mail('you@yoursite.com','subject of the mail','"' . $email . '"');  // problem

?>

输出:

Return-Path: <user1@abc.com>
Received: from abc.abc.com 
    by abc.abc.com  with ESMTP id r8CHQuwsvl019623
    for <abc@abc.com>; Thu, 12 Sep 2013 10:26:56 -0700
Received: from abc@abc.com ([xx.xx.xx.xx]) by  abc@abc.com ([xx.xx.xx.xx]) Thu, 12 Sep 2013 10:26:55 -0700
From:  <user1@abc.com>
To:  <user2@abc.com>        
Subject: Here is the subject
Thread-Topic: ACTION REQUIRED:  Here is the subject
Thread-Index: Ac6vLqD991sdflXReXHStipZDdjUh0dpgAoQZUQAANQNRA=
Date: Thu, 12 Sep 2013 17:26:55 +0000
Message-ID: <A61C9CD725DF1C4FA94C13E9C8BEE9C538A35785@az18ex3004.abc.com>
References: <53B09D5B548AF94EB5B6C48321C34F2E11CB1621@az18ex3006.abc.com>
In-Reply-To: <53B09D5B548AF93e4EB5B6C48321C34F2E11CB1621@az18ex3006.abc.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: yes
X-MS-TNEF-Correlator:
x-originating-ip: [xx.xx.xx.xx]
Content-Transfer-Encoding: base64
MIME-Version: 1.0

asdfxGSIb3DQEHAqCAMIACAQsdfsdAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAaCAJIAEgge4Q29u
dGVudC1UeXBlOiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tLS09X05leHRQ
YXJ0XzAwMF8wMDkxXzAxQ0VBRkEyLjk5RTBFQUEwIg0KDQpUaGlzIGlzIGEgbsdfsdGlwYXJ0IG1l
c3NhZ2UgaW4gTUlNRSBmb3JtYXQuDQoNeddi0tLS0tLT1fTmV4dFBhcnRfMDAwXzAwOTFfMDFDRUFG
QTIuOTlFMEVBQTANCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsNCgljaGFyc2V0PSJ1cy1hc2Np
4

2 回答 2

2

google code上有一个简单的类——MimeMailParser,你可以看看。有了它,我认为从邮件中获取正文和标题将相当简单。

$Parser = new MimeMailParser();
$fd = fopen("php://stdin", "r");
$Parser->setStream($fd);

$headers = $Parser->getHeaders();

$header = "";
$header .= "From: {$headers['from']}\r\n";
$header .= "Reply-To: {$headers['from']}\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\r\n";

$email = $Parser->getMessageBody('html');

将它们放入单独的变量中后,您可以像这样发送邮件:

mail('you@yoursite.com',$header['subject'],$email, $header);

这个解决方案不是完美的方式,它只会发送电子邮件的 html 部分,但它是你尝试新事物的好开始。祝你好运

编辑:似乎标题存储为数组。您需要将它们放入格式mail函数接受。我已经更正了上面的代码。

于 2013-09-13T13:12:18.783 回答
0

您可能需要在标题中指定电子邮件的格式:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail('you@yoursite.com','subject of the mail','"' . $email . '"',$headers);  
于 2013-09-13T12:53:58.323 回答