From 90fe6cbc357d11e044f9a3e6c0be9d81b3c2bdf7 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 25 Jan 2010 07:34:51 -0500
Subject: [PATCH] - Mail_MIME update

---
 program/lib/Mail/mime.php     |   75 +++++++++++++++++++++++++++++++++----
 program/lib/Mail/mimePart.php |   32 ++++++++++------
 2 files changed, 87 insertions(+), 20 deletions(-)

diff --git a/program/lib/Mail/mime.php b/program/lib/Mail/mime.php
index a4f4901..81a2569 100644
--- a/program/lib/Mail/mime.php
+++ b/program/lib/Mail/mime.php
@@ -726,6 +726,22 @@
     }
 
     /**
+     * Returns the complete e-mail body, ready to send using an alternative
+     * mail delivery method.
+     * 
+     * @param array $params The Build parameters passed to the
+     *                      &get() function. See &get for more info.
+     *
+     * @return mixed The e-mail body or PEAR error object
+     * @access public
+     * @since 1.6.0
+     */
+    function getMessageBody($params = null)
+    {
+        return $this->get($params, null, true);
+    }
+
+    /**
      * Writes (appends) the complete e-mail into file.
      * 
      * @param string $filename  Output file location
@@ -738,6 +754,7 @@
      *
      * @return mixed True or PEAR error object
      * @access public
+     * @since 1.6.0
      */
     function saveMessage($filename, $params = null, $headers = null, $overwrite = false)
     {
@@ -777,18 +794,55 @@
     }
 
     /**
+     * Writes (appends) the complete e-mail body into file.
+     * 
+     * @param string $filename Output file location
+     * @param array  $params   The Build parameters passed to the
+     *                         &get() function. See &get for more info.
+     *
+     * @return mixed True or PEAR error object
+     * @access public
+     * @since 1.6.0
+     */
+    function saveMessageBody($filename, $params = null)
+    {
+        // Check state of file and raise an error properly
+        if (file_exists($filename) && !is_writable($filename)) {
+            $err = PEAR::raiseError('File is not writable: ' . $filename);
+            return $err;
+        }
+
+        // Temporarily reset magic_quotes_runtime and read file contents
+        if ($magic_quote_setting = get_magic_quotes_runtime()) {
+            @ini_set('magic_quotes_runtime', 0);
+        }
+
+        if (!($fh = fopen($filename, 'ab'))) {
+            $err = PEAR::raiseError('Unable to open file: ' . $filename);
+            return $err;
+        }
+
+        // Write the rest of the message into file
+        $res = $this->get($params, $filename, true);
+
+        return $res ? $res : true;
+    }
+
+    /**
      * Builds the multipart message from the list ($this->_parts) and
      * returns the mime content.
      *
-     * @param array    $params   Build parameters that change the way the email
-     *                           is built. Should be associative. See $_build_params.
-     * @param resource $filename Output file where to save the message instead of
-     *                           returning it
+     * @param array    $params    Build parameters that change the way the email
+     *                            is built. Should be associative. See $_build_params.
+     * @param resource $filename  Output file where to save the message instead of
+     *                            returning it
+     * @param boolean  $skip_head True if you want to return/save only the message
+     *                            without headers
      *
      * @return mixed The MIME message content string, null or PEAR error object
      * @access public
      */
-    function &get($params = null, $filename = null)
+    function &get($params = null, $filename = null, $skip_head = false)
     {
         if (isset($params)) {
             while (list($key, $value) = each($params)) {
@@ -958,14 +1012,16 @@
         // Write output to file
         if ($filename) {
             // Append mimePart message headers and body into file
-            if (PEAR::isError($headers = $message->encodeToFile($filename, $boundary))) {
+            $headers = $message->encodeToFile($filename, $boundary, $skip_head);
+            if (PEAR::isError($headers)) {
                 return $headers;
             }
             $this->_headers = array_merge($this->_headers, $headers);
             $ret = null;
             return $ret;
         } else {
-            if (PEAR::isError($output = $message->encode($boundary))) {
+            $output = $message->encode($boundary, $skip_head);
+            if (PEAR::isError($output)) {
                 return $output;
             }
             $this->_headers = array_merge($this->_headers, $output['headers']);
@@ -1283,7 +1339,10 @@
                 $value = wordwrap($value, 76, $eol . ' ');
             }
 
-            $value = preg_replace('/^'.$name.': /', '', $value);
+            // remove header name prefix (there could be EOL too)
+            $value = preg_replace(
+                '/^'.$name.':('.preg_quote($eol, '/').')* /', '', $value
+            );
 
         } else {
             // Unstructured header
diff --git a/program/lib/Mail/mimePart.php b/program/lib/Mail/mimePart.php
index 644b2b6..ce37deb 100644
--- a/program/lib/Mail/mimePart.php
+++ b/program/lib/Mail/mimePart.php
@@ -330,15 +330,16 @@
      * Encodes and saves the email into file. File must exist.
      * Data will be appended to the file.
      *
-     * @param string $filename Output file location
-     * @param string $boundary Pre-defined boundary string
+     * @param string  $filename  Output file location
+     * @param string  $boundary  Pre-defined boundary string
+     * @param boolean $skip_head True if you don't want to save headers
      *
      * @return array An associative array containing message headers
      *               or PEAR error object
      * @access public
      * @since 1.6.0
      */
-    function encodeToFile($filename, $boundary=null)
+    function encodeToFile($filename, $boundary=null, $skip_head=false)
     {
         if (file_exists($filename) && !is_writable($filename)) {
             $err = PEAR::raiseError('File is not writeable: ' . $filename);
@@ -355,7 +356,7 @@
             @ini_set('magic_quotes_runtime', 0);
         }
 
-        $res = $this->_encodePartToFile($fh, $boundary);
+        $res = $this->_encodePartToFile($fh, $boundary, $skip_head);
 
         fclose($fh);
 
@@ -369,13 +370,14 @@
     /**
      * Encodes given email part into file
      *
-     * @param string $fh       Output file handle
-     * @param string $boundary Pre-defined boundary string
+     * @param string  $fh        Output file handle
+     * @param string  $boundary  Pre-defined boundary string
+     * @param boolean $skip_head True if you don't want to save headers
      *
      * @return array True on sucess or PEAR error object
      * @access private
      */
-    function _encodePartToFile($fh, $boundary=null)
+    function _encodePartToFile($fh, $boundary=null, $skip_head=false)
     {
         $eol = $this->_eol;
 
@@ -384,25 +386,31 @@
             $this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
         }
 
-        foreach ($this->_headers as $key => $value) {
-            fwrite($fh, $key . ': ' . $value . $eol);
+        if (!$skip_head) {
+            foreach ($this->_headers as $key => $value) {
+                fwrite($fh, $key . ': ' . $value . $eol);
+            }
+            $f_eol = $eol;
+        } else {
+            $f_eol = '';
         }
 
         if (count($this->_subparts)) {
             for ($i = 0; $i < count($this->_subparts); $i++) {
-                fwrite($fh, $eol . '--' . $boundary . $eol);
+                fwrite($fh, $f_eol . '--' . $boundary . $eol);
                 $res = $this->_subparts[$i]->_encodePartToFile($fh);
                 if (PEAR::isError($res)) {
                     return $res;
                 }
+                $f_eol = $eol;
             }
 
             fwrite($fh, $eol . '--' . $boundary . '--' . $eol);
 
         } else if ($this->_body) {
-            fwrite($fh, $eol . $this->_getEncodedData($this->_body, $this->_encoding));
+            fwrite($fh, $f_eol . $this->_getEncodedData($this->_body, $this->_encoding));
         } else if ($this->_body_file) {
-            fwrite($fh, $eol);
+            fwrite($fh, $f_eol);
             $res = $this->_getEncodedDataFromFile(
                 $this->_body_file, $this->_encoding, $fh
             );

--
Gitblit v1.9.1