简体   繁体   中英

how to forward a mail - php

Currently I am reading mails from the input stream directly and then doing some manipulation it and then I need to send the modified mail.

I have successfully read the mail and modified it, now can I forward the mail as original mail to users.

So, for that I have used mail function, but this mail function sends message as raw format, since I am reading the mail and setting the variable in the body. How do I make it as original mail that looks like a format (the raw format converted to actual messages?)

#!/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

?>

output:

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

There is a simple class - MimeMailParser on google code, you can check it out. With it I think it will be fairly simple to get the body and the headers from the mail.

$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');

After you get them in separate variables you can send the mail like this:

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

This solution isn't the perfect way, it will send only the html part of the e-mail, but it's a good start for you to try new things I thing. Good luck

EDIT : It seems the headers are stored as an array. You need to put them into the format mail function accepts. I already corrected my code above.

You might need to specify the format of your email in the headers:

$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);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM