PEAR Mail - duplicates body of MIME emails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

PEAR Mail - duplicates body of MIME emails

Post by shawngoldw »

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):
this does not work.
It will come out the other end to the users as:
this does not work.

this does not work.
Now here is the offending code:

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'];
}
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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PEAR Mail - duplicates body of MIME emails

Post by Jade »

Have you checked to make sure you don't have malformed headers? I had a similar problem when I didn't have boundaries. http://code.web-max.ca/misc_htmlemail.php
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: PEAR Mail - duplicates body of MIME emails

Post by shawngoldw »

Thanks, this looks very promising.

But I don't quite understand what's going on.
What is the boundary?
$boundary = uniqid("HTMLEMAIL");
What does that mean?

And they seem to use it all over the place, where is the boundary necessary?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PEAR Mail - duplicates body of MIME emails

Post by Jade »

I'm *pretty* sure most of the email clients use the boundaries to figure out where the text email starts and the html email ends -- or vice versa. If you don't have those it'll think the whole thing (text and email) are the entire email contents instead of only displaying per the user's html/text only preferences.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: PEAR Mail - duplicates body of MIME emails

Post by shawngoldw »

Ya, I get it now. This sounds like it was exactly my problem.
http://en.wikipedia.org/wiki/MIME describes it pretty well. Thanks for your help, I'll let you know if it works
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: PEAR Mail - duplicates body of MIME emails

Post by shawngoldw »

I had to send myself 18 test email to figure it out, but I got it!

Ironically, it was kinda the opposite of what you suggested. There were already boundaries in the emails, without me explicitly adding them. The problem was that the first part which is not supposed to have a boundary did in fact have one. It seems PEAR adds them with the addSubPart function.

Thank you for your help


edit* eerrrr my fix broke attachments. I'll figure it out though :D
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PEAR Mail - duplicates body of MIME emails

Post by Jade »

Lol well all I remembered was that it had to do with the boundaries.... at least you're pointed in the right direction now. I've never user PEAR mailer that's about all my 2 cents is gonna buy ya.
Post Reply