PEAR Mail - duplicates body of MIME emails
Posted: Mon Jul 12, 2010 9:37 am
First, some background: I have set up my server to pipe all incoming emails into a php script which then decodes them, does some work with them and then re-encodes them and sends them out to a list of people from a database based on the incoming address. I am using PEAR mail to decode, re-encode, and send the emails because our site is on a shared server which imposes some pretty strict email limits, and PEAR lets us send through our gmail account.
My problem is that when the incoming email is of MIME type the body gets gets repeated twice. I'll post an example and then the offending code.
Say I send an email with this body (the quote symbol is not part of the email):
In the offending code, we have:
if(strcmp($structure->ctype_primary, "text") == 0) {
this goes when the email is plain text and emails work properly. The else handles MIME emails and this is where the problem occurs.
I have found that the loop:
foreach ($structure->parts as $part) {
goes through 2 iterations, signifying 2 parts. The first iteration adds the plain text to the body while the second part adds the proper stylized text.
Any input or advice would be appreciated.
Thanks,
Shawn
My problem is that when the incoming email is of MIME type the body gets gets repeated twice. I'll post an example and then the offending code.
Say I send an email with this body (the quote symbol is not part of the email):
It will come out the other end to the users as:this does not work.
Now here is the offending code:this does not work.
this does not work.
Code: Select all
$file = 'php://stdin';
$fd = fopen($file, 'r') or die("could not open file");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $email;
$structure = Mail_mimeDecode::decode($params);
...
above is relevant setup
there is some irrelevant stuff here between these two blocks
below is the offending code
...
$message = new Mail_mime();
$message->setTXTBody("");
if(strcmp($structure->ctype_primary, "text") == 0) {
$message->setTXTBody($structure->body);
$body = $message->get();
$headers = $message->headers($extraheaders);
} else {
$paras['content_type'] = 'multipart/mixed';
$email = new Mail_mimePart('', $paras);
foreach ($structure->parts as $part) {
$paras["content_type"] = $part->headers['content-type'];
$paras["encoding"] = $part->headers['content-transfer-encoding'];
$paras["disposition"] = $part->disposition;
if(strcmp($paras["disposition"], "inline")==0)
$paras["disposition"] = "attachment";
$paras["dfilename"] = $part->d_parameters['filename'];
$paras["charset"] = $part->ctype_parameters['charset'];
$email->addSubPart($part->body, $paras);
}
$message = $email->encode();
$body = $message['body'];
$headers = $message['headers'];
}
if(strcmp($structure->ctype_primary, "text") == 0) {
this goes when the email is plain text and emails work properly. The else handles MIME emails and this is where the problem occurs.
I have found that the loop:
foreach ($structure->parts as $part) {
goes through 2 iterations, signifying 2 parts. The first iteration adds the plain text to the body while the second part adds the proper stylized text.
Any input or advice would be appreciated.
Thanks,
Shawn