From ac08cc4af5f483f735f2aa6ad9181410dd49ba34 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Wed, 7 Jul 2010 18:20:17 +0000 Subject: [PATCH] - #17573: Place charset parameter in first line of Content-Type header (if possible) git-svn-id: http://svn.php.net/repository/pear/packages/Mail_Mime/trunk@301061 c90b9560-bf6c-de11-be94-00142212c4b1 --- mimePart.php | 40 ++++++++++++++++++++++----------------- package.xml | 1 + tests/test_Bug_12411.phpt | 5 ++--- tests/test_Bug_13444.phpt | 2 +- tests/test_Bug_15320.phpt | 6 +++--- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/mimePart.php b/mimePart.php index bbfdd19..55ea7a6 100755 --- a/mimePart.php +++ b/mimePart.php @@ -215,32 +215,43 @@ function Mail_mimePart($body = '', $params = array()) } } + // Default content-type + if (empty($c_type['type'])) { + $c_type['type'] = 'text/plain'; + } + // Content-Type - if (isset($c_type['type'])) { + if (!empty($c_type['type'])) { $headers['Content-Type'] = $c_type['type']; - if (isset($c_type['name'])) { + if (!empty($c_type['charset'])) { + $charset = "charset={$c_type['charset']}"; + // place charset parameter in the same line, if possible + if ((strlen($headers['Content-Type']) + strlen($charset) + 16) <= 76) { + $headers['Content-Type'] .= '; '; + } else { + $headers['Content-Type'] .= ';' . $this->_eol . ' '; + } + $headers['Content-Type'] .= $charset; + } + if (!empty($c_type['name'])) { $headers['Content-Type'] .= ';' . $this->_eol; $headers['Content-Type'] .= $this->_buildHeaderParam( - 'name', $c_type['name'], - isset($c_type['charset']) ? $c_type['charset'] : 'US-ASCII', + 'name', $c_type['name'], + isset($c_type['charset']) ? $c_type['charset'] : 'US-ASCII', isset($c_type['language']) ? $c_type['language'] : null, isset($params['name_encoding']) ? $params['name_encoding'] : null ); } - if (isset($c_type['charset'])) { - $headers['Content-Type'] - .= ';' . $this->_eol . " charset={$c_type['charset']}"; - } } // Content-Disposition - if (isset($c_disp['disp'])) { + if (!empty($c_disp['disp'])) { $headers['Content-Disposition'] = $c_disp['disp']; - if (isset($c_disp['filename'])) { + if (!empty($c_disp['filename'])) { $headers['Content-Disposition'] .= ';' . $this->_eol; $headers['Content-Disposition'] .= $this->_buildHeaderParam( - 'filename', $c_disp['filename'], - isset($c_disp['charset']) ? $c_disp['charset'] : 'US-ASCII', + 'filename', $c_disp['filename'], + isset($c_disp['charset']) ? $c_disp['charset'] : 'US-ASCII', isset($c_disp['language']) ? $c_disp['language'] : null, isset($params['filename_encoding']) ? $params['filename_encoding'] : null ); @@ -256,11 +267,6 @@ function Mail_mimePart($body = '', $params = array()) ); } - // Default content-type - if (!isset($headers['Content-Type'])) { - $headers['Content-Type'] = 'text/plain'; - } - // Default encoding if (!isset($this->_encoding)) { $this->_encoding = '7bit'; diff --git a/package.xml b/package.xml index d012349..09d729d 100755 --- a/package.xml +++ b/package.xml @@ -44,6 +44,7 @@ using RFC2047 and/or RFC2231. Bugs Fixed: * Fix double-addition of e-mail domain to content ID in HTML images [alec] * #17311: Fix multi-octet characters are split across adjacent 'encoded-word's [alec] + * #17573: Place charset parameter in first line of Content-Type header (if possible) [alec] Implemented Features: * #17518: Added addTo() method [alec] diff --git a/tests/test_Bug_12411.phpt b/tests/test_Bug_12411.phpt index 9916588..cdcce48 100644 --- a/tests/test_Bug_12411.phpt +++ b/tests/test_Bug_12411.phpt @@ -22,8 +22,7 @@ echo "\n"; echo $enc->_headers['Content-Disposition']; ?> --EXPECT-- -text/plain; - name="=?ISO-8859-1?Q?=C5=9Bciema?="; - charset=ISO-8859-1 +text/plain; charset=ISO-8859-1; + name="=?ISO-8859-1?Q?=C5=9Bciema?=" attachment; filename="=?ISO-8859-1?B?xZtjaWVtYQ==?=" diff --git a/tests/test_Bug_13444.phpt b/tests/test_Bug_13444.phpt index c7af4b0..af453ef 100644 --- a/tests/test_Bug_13444.phpt +++ b/tests/test_Bug_13444.phpt @@ -16,7 +16,7 @@ $headCT = $head['Content-Type']; $headCT = explode(";", $headCT); $headCT = $headCT[0]; -$ct = preg_match_all('|Content-Type: (.*);|', $body, $matches); +$ct = preg_match_all('|Content-Type: ([^;\r\n]+)|', $body, $matches); print($headCT); print("\n"); foreach ($matches[1] as $match){ diff --git a/tests/test_Bug_15320.phpt b/tests/test_Bug_15320.phpt index 3abe906..4653ede 100644 --- a/tests/test_Bug_15320.phpt +++ b/tests/test_Bug_15320.phpt @@ -12,6 +12,6 @@ $enc = $m->_addAttachmentPart($root, $m->_parts[0]); print_r($enc->_headers['Content-Type']); ?> --EXPECT-- -text/plain; - name=file.txt; - charset=ISO-8859-1 +text/plain; charset=ISO-8859-1; + name=file.txt +