- Mail_MIME update

release-0.6
alecpl 15 years ago
parent 34b65924b9
commit 90fe6cbc35

@ -725,6 +725,22 @@ class Mail_mime
return $mail; return $mail;
} }
/**
* 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. * Writes (appends) the complete e-mail into file.
* *
@ -738,6 +754,7 @@ class Mail_mime
* *
* @return mixed True or PEAR error object * @return mixed True or PEAR error object
* @access public * @access public
* @since 1.6.0
*/ */
function saveMessage($filename, $params = null, $headers = null, $overwrite = false) function saveMessage($filename, $params = null, $headers = null, $overwrite = false)
{ {
@ -776,19 +793,56 @@ class Mail_mime
return $res ? $res : true; return $res ? $res : true;
} }
/**
* 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 * Builds the multipart message from the list ($this->_parts) and
* returns the mime content. * returns the mime content.
* *
* @param array $params Build parameters that change the way the email * @param array $params Build parameters that change the way the email
* is built. Should be associative. See $_build_params. * is built. Should be associative. See $_build_params.
* @param resource $filename Output file where to save the message instead of * @param resource $filename Output file where to save the message instead of
* returning it * 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 * @return mixed The MIME message content string, null or PEAR error object
* @access public * @access public
*/ */
function &get($params = null, $filename = null) function &get($params = null, $filename = null, $skip_head = false)
{ {
if (isset($params)) { if (isset($params)) {
while (list($key, $value) = each($params)) { while (list($key, $value) = each($params)) {
@ -958,14 +1012,16 @@ class Mail_mime
// Write output to file // Write output to file
if ($filename) { if ($filename) {
// Append mimePart message headers and body into file // 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; return $headers;
} }
$this->_headers = array_merge($this->_headers, $headers); $this->_headers = array_merge($this->_headers, $headers);
$ret = null; $ret = null;
return $ret; return $ret;
} else { } else {
if (PEAR::isError($output = $message->encode($boundary))) { $output = $message->encode($boundary, $skip_head);
if (PEAR::isError($output)) {
return $output; return $output;
} }
$this->_headers = array_merge($this->_headers, $output['headers']); $this->_headers = array_merge($this->_headers, $output['headers']);
@ -1283,7 +1339,10 @@ class Mail_mime
$value = wordwrap($value, 76, $eol . ' '); $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 { } else {
// Unstructured header // Unstructured header

@ -330,15 +330,16 @@ class Mail_mimePart
* Encodes and saves the email into file. File must exist. * Encodes and saves the email into file. File must exist.
* Data will be appended to the file. * Data will be appended to the file.
* *
* @param string $filename Output file location * @param string $filename Output file location
* @param string $boundary Pre-defined boundary string * @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 * @return array An associative array containing message headers
* or PEAR error object * or PEAR error object
* @access public * @access public
* @since 1.6.0 * @since 1.6.0
*/ */
function encodeToFile($filename, $boundary=null) function encodeToFile($filename, $boundary=null, $skip_head=false)
{ {
if (file_exists($filename) && !is_writable($filename)) { if (file_exists($filename) && !is_writable($filename)) {
$err = PEAR::raiseError('File is not writeable: ' . $filename); $err = PEAR::raiseError('File is not writeable: ' . $filename);
@ -355,7 +356,7 @@ class Mail_mimePart
@ini_set('magic_quotes_runtime', 0); @ini_set('magic_quotes_runtime', 0);
} }
$res = $this->_encodePartToFile($fh, $boundary); $res = $this->_encodePartToFile($fh, $boundary, $skip_head);
fclose($fh); fclose($fh);
@ -369,13 +370,14 @@ class Mail_mimePart
/** /**
* Encodes given email part into file * Encodes given email part into file
* *
* @param string $fh Output file handle * @param string $fh Output file handle
* @param string $boundary Pre-defined boundary string * @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 * @return array True on sucess or PEAR error object
* @access private * @access private
*/ */
function _encodePartToFile($fh, $boundary=null) function _encodePartToFile($fh, $boundary=null, $skip_head=false)
{ {
$eol = $this->_eol; $eol = $this->_eol;
@ -384,25 +386,31 @@ class Mail_mimePart
$this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\""; $this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
} }
foreach ($this->_headers as $key => $value) { if (!$skip_head) {
fwrite($fh, $key . ': ' . $value . $eol); foreach ($this->_headers as $key => $value) {
fwrite($fh, $key . ': ' . $value . $eol);
}
$f_eol = $eol;
} else {
$f_eol = '';
} }
if (count($this->_subparts)) { if (count($this->_subparts)) {
for ($i = 0; $i < count($this->_subparts); $i++) { 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); $res = $this->_subparts[$i]->_encodePartToFile($fh);
if (PEAR::isError($res)) { if (PEAR::isError($res)) {
return $res; return $res;
} }
$f_eol = $eol;
} }
fwrite($fh, $eol . '--' . $boundary . '--' . $eol); fwrite($fh, $eol . '--' . $boundary . '--' . $eol);
} else if ($this->_body) { } 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) { } else if ($this->_body_file) {
fwrite($fh, $eol); fwrite($fh, $f_eol);
$res = $this->_getEncodedDataFromFile( $res = $this->_getEncodedDataFromFile(
$this->_body_file, $this->_encoding, $fh $this->_body_file, $this->_encoding, $fh
); );

Loading…
Cancel
Save