Pipe Email with Attachement to PHP & Resend with new e-mail address in "to" header field

584 Views Asked by At

I am a newb trying to do something that seems simple but has turned out to be pretty complicated. I'm trying use the hostgator forwarding e-mail feature to forward emails with attachments directed to one e-mail address and forward the email, attachment and all, to another e-mail address while removing the original email address in the "to" header section and replacing it with the e-mail address to which it is being forwarded.

With the standalone feature, cpanel in hostgator allows forwarding the e-mail with the attachment but the original email is visible when forwarded to the final receiving e-mail. It's kind of like hostgator is sending it to the final recipient as a bcc.

cpanel has another feature which allows piping the email to a php script and I was able to forward the raw e-mail using the mail function and a few other lines of code to the php but it looks awful and the attachment is just a bunch of code in base64. It also takes hours to receive by the final recipient.

I've scoured the internet looking for the final solution piecing together different pieces of code and settled with this thing below, but it doesn't seem to be working so I'm hoping some of you more experienced developers can impart some wisdom upon this poor newb. My humble apologies for the long winded prompt.

#!/usr/bin/php -q
<?php
ini_set("include_path", '/home#/username/php:' . ini_get("include_path") );
require_once 'Mail/mimeDecode.php';
var $raw = '';
var $decoded;
$src = 'php://stdin';
$fd = fopen($src,'r');
while(!feof($fd)){ $this->raw .= fread($fd,1024); }
fclose($fd);
$decoder = new Mail_mimeDecode($this->raw);
        $this->decoded = $decoder->decode(
            Array(
                'decode_headers' => TRUE,
                'include_bodies' => TRUE,
                'decode_bodies' => TRUE,
            )
        );
$this->subject = $this->decoded->headers['subject'];
$getHead[] = $this->decoded->headers['Received'];
$getHead[] = $this->decoded->headers['From'];
$getHead[] = $this->decoded->headers['Reply-To'];
$getHead[] = $this->decoded->headers['X-Mailer'];
$getHead[] = $this->decoded->headers['Date'];
$getHead[] = $this->decoded->headers['MIME-Version'];
$getHead[] = $this->decoded->headers['Content-Type'];
$getHead[] = $this->decoded->headers['Content-Transfer-Encoding'];
$getHead[] = $this->decoded->headers['Return-Path'];
$getHead[] = $this->decoded->headers['X-OriginalArrivalTime'];
$getHead[] = $this->decoded->headers['Thread-Topic'];
$getHead[] = $this->decoded->headers['Thread-Index'];
$getHead[] = $this->decoded->headers['Message-ID'];
$getHead[] = $this->decoded->headers['Accept-Language'];
$getHead[] = $this->decoded->headers['Content-Language'];
$getHead[] = $this->decoded->headers['X-MS-Has-Attach'];
$getHead[] = $this->decoded->headers['X-MS-TNEF-Correlator'];
$getHead[] = $this->decoded->headers['x-ms-exchange-transport-fromentityheader'];
$getHead[] = $this->decoded->headers['x-originating-ip'];
$getHead[] = $this->decoded->headers['Content-Disposition'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthSource'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthAs'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthMechanism'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-Network-Message-Id'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AVStamp-Enterprise'];
$this->body = $this->decoded->body;
$email_to = "[email protected]";
mail($email_to, $this->subject, $this->body, implode("\r\n", $getHead));
?>
0

There are 0 best solutions below