Permalink
Browse files

Add possibility to set additional parameters of message part header e…

….g. attachment size

Added automatic setting of attachment size via Content-Disposition size parameter
  • Loading branch information...
1 parent fbe36e8 commit 8975966c0b3c6342f4c2da7b6a1db81e242b7c69 @alecpl alecpl committed Jun 9, 2012
Showing with 38 additions and 19 deletions.
  1. +2 −1 Mail/Mime2.php
  2. +34 −12 Mail/MimePart2.php
  3. +0 −5 tests/test_Bug_12411.phpt
  4. +1 −1 tests/test_Bug_14529.phpt
  5. +1 −0 tests/test_Bug_3513_3.phpt
View
@@ -386,7 +386,8 @@ public function addHTMLImage($file,
* @param string $description Content-Description header
* @param string $h_charset The character set of the headers e.g. filename
* If not specified, $charset will be used
- * @param array $add_headers Additional part headers
+ * @param array $add_headers Additional part headers. Array keys can be in form
+ * of <header_name>:<parameter_name>
*
* @return mixed True on success or PEAR_Error object
* @access public
View
@@ -156,7 +156,8 @@ class Mail_MimePart2
* headers_charset - Charset of the headers e.g. filename, description.
* If not set, 'charset' will be used
* eol - End of line sequence. Default: "\r\n"
- * headers - Hash array with additional part headers
+ * headers - Hash array with additional part headers. Array keys can be
+ * in form of <header_name>:<parameter_name>
* body_file - Location of file with part's body (instead of $body)
*/
public function __construct($body = '', $params = array())
@@ -218,13 +219,17 @@ public function __construct($body = '', $params = array())
$params['headers_charset'] = $params['charset'];
}
}
+
+ // header values encoding parameters
+ $h_charset = !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII';
+ $h_language = !empty($params['language']) ? $params['language'] : null;
+ $h_encoding = !empty($params['name_encoding']) ? $params['name_encoding'] : null;
+
+
if (!empty($params['filename'])) {
$headers['Content-Type'] .= ';' . $this->_eol;
$headers['Content-Type'] .= $this->_buildHeaderParam(
- 'name', $params['filename'],
- !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
- !empty($params['language']) ? $params['language'] : null,
- !empty($params['name_encoding']) ? $params['name_encoding'] : null
+ 'name', $params['filename'], $h_charset, $h_language, $h_encoding
);
}
@@ -234,23 +239,40 @@ public function __construct($body = '', $params = array())
if (!empty($params['filename'])) {
$headers['Content-Disposition'] .= ';' . $this->_eol;
$headers['Content-Disposition'] .= $this->_buildHeaderParam(
- 'filename', $params['filename'],
- !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
- !empty($params['language']) ? $params['language'] : null,
+ 'filename', $params['filename'], $h_charset, $h_language,
!empty($params['filename_encoding']) ? $params['filename_encoding'] : null
);
}
+
+ // add attachment size
+ $size = $this->_body_file ? filesize($this->_body_file) : strlen($body);
+ if ($size) {
+ $headers['Content-Disposition'] .= ';' . $this->_eol . ' size=' . $size;
+ }
}
if (!empty($params['description'])) {
$headers['Content-Description'] = self::encodeHeader(
- 'Content-Description', $params['description'],
- !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
- !empty($params['name_encoding']) ? $params['name_encoding'] : 'quoted-printable',
- $this->_eol
+ 'Content-Description', $params['description'], $h_charset, $h_encoding, $this->_eol
);
}
+ // Search and add existing headers' parameters
+ foreach ($headers as $key => $value) {
+ $items = explode(':', $key);
+ if (count($items) == 2) {
+ $header = $items[0];
+ $param = $items[1];
+ if (isset($headers[$header])) {
+ $headers[$header] .= ';' . $this->_eol;
+ }
+ $headers[$header] .= $this->_buildHeaderParam(
+ $param, $value, $h_charset, $h_language, $h_encoding
+ );
+ unset($headers[$key]);
+ }
+ }
+
// Default encoding
if (!isset($this->_encoding)) {
$this->_encoding = '7bit';
@@ -7,11 +7,6 @@ include "Mail/MimePart2.php";
// some text with polish Unicode letter at the beginning
$filename = base64_decode("xZtjaWVtYQ==");
-/*
-$m->addAttachment('testfile', "text/plain", $filename, FALSE,
- 'base64', 'attachment', 'ISO-8859-1', 'pl', '',
- 'quoted-printable', 'base64');
-*/
$part = new Mail_MimePart2('', array(
'filename' => $filename,
'disposition' => 'attachment',
@@ -11,7 +11,7 @@ $filename = $path . base64_decode("xZtjaWVtYQ==");
$m->addAttachment('testfile', "text/plain", $filename, FALSE, 'base64', 'attachment', 'ISO-8859-1');
$msg = $m->get();
-if (strpos($msg, "filename*=ISO-8859-1''%C5%9Bciema")) {
+if (strpos($msg, "filename*=ISO-8859-1''%C5%9Bciema;")) {
echo "OK";
}
?>
@@ -24,3 +24,4 @@ print $msg['headers']['Content-Disposition'];
--EXPECT--
attachment;
filename*=iso-2022-jp''%1B$BF|K%5C8l%1B%28B.txt
+

0 comments on commit 8975966

Please sign in to comment.