PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Mail » Mail_Mime » Bug #10232

Gmail creates double line break when \r\n is used

Details

Submitted2007-03-01 15:51 UTC
Fromstephen dot bigelis at gmail dot com
Assignedcipri
StatusClosed
PackageMail_Mime
PHP Version4.4.1
OS4.11-STABLE FreeBSD
Roadmaps1.4.0, 1.4.0RC1

Comments

[2007-03-01 15:51 UTC] stephen dot bigelis at gmail dot com

Description:
------------
Emails come in as attachments. Because the added line break prevents gmail from parsing the boundary description. I don't believe it is necessary for any email clients to put a line break in before the boundary definition, so I just removed it. (I checked a ton of mime newsletters in my own mail box and the majority just used a tab.) Then gmail would recognize the email, but the html-code was being displayed because of added line breaks in the section headers. So I manually changed them to "\n", then any lines of html longer then 76 char were displaying wrong because of the added line break in the html, so I changed the end of line marker in _quotedPrintableEncode();

Please note that you can't just define MAIL_MIMEPART_CRLF as "\n" that will cause a ton of other problems.

The temporary fix I am currently using takes place entirely in mimePart.php.

First in the encode function:

function encode()
{
$encoded =& $this->_encoded;

if (!empty($this->_subparts)) {
srand((double)microtime()*1000000);
$boundary = '=_' . md5(rand() . microtime());
$this->_headers['Content-Type'] .= ';' . "\t" . 'boundary="' . $boundary . '"';

// Add body parts to $subparts
for ($i = 0; $i _subparts); $i++) {
$headers = array();
$tmp = $this->_subparts[$i]->encode();
foreach ($tmp['headers'] as $key => $value) {
$headers[] = $key . ': ' . $value;
}
$subparts[] = implode("\n", $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
}

$encoded['body'] = '--' . $boundary . "\n" .
implode('--' . $boundary . "\n", $subparts) .
'--' . $boundary.'--' . "\n";

} else {
$encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF;
}

// Add headers to $encoded
$encoded['headers'] =& $this->_headers;

return $encoded;
}

and then to make your html properly display you must change:
$eol = "\n"; in _quotedPrintableEncode();

So far I have tested this in gmail, hotmail, yahoo, aol, lotus notes, & thunderbird. I haven't had a problem yet, but I suspect there has got to be client out there that won't like it.

Test script:
---------------
'; // Add a valid email here
$headers= array('From' => $from,
'Subject' => $subject);
$textMessage='This is a text message
thank you';
$htmlMessage='Invite

This is an HTML message
thank you
';
$mime = new Mail_Mime();
$mime->setTxtBody($textMessage);
$mime->setHtmlBody($htmlMessage);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = &Mail::factory('mail');
$mail->send($to, $hdrs, $body);
if($mail){
echo "Note: Your Message has been sent";
} else {
die('FATAL ERROR: ');
} //else
?>

Expected result:
----------------
Gmail header should look like this:

Message-ID:
To: myemail@gmail.com
Subject: test
Mime-Version: 1.0
From: Stephen Bigelis
Content-Type: multipart/alternative;
boundary="=_ce8703e586c1d5b49955824c21e1da92"

--=_ce8703e586c1d5b49955824c21e1da92
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit

This is a text message
thank you

--=_ce8703e586c1d5b49955824c21e1da92
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

Invite

This is an HTML message
thank you

--=_ce8703e586c1d5b49955824c21e1da92--

Actual result:
--------------
Gamil header comes in looking like this:

Message-ID:
To: myemail@gmail.com
Subject: test2
Mime-Version: 1.0
From: Stephen Bigelis
Content-Type: multipart/alternative;

boundary="=_76d9d1e8ab8f2f3a4a8cd53c1616536b"

--=_76d9d1e8ab8f2f3a4a8cd53c1616536b

Content-Type: text/plain; charset="ISO-8859-1"

Content-Transfer-Encoding: 7bit

This is a text message
thank you

--=_76d9d1e8ab8f2f3a4a8cd53c1616536b

Content-Type: text/html; charset="ISO-8859-1"

Content-Transfer-Encoding: quoted-printable

Invite

This is an HTML message

thank you

</body><html>

--=_76d9d1e8ab8f2f3a4a8cd53c1616536b--