Source for file class.mimePart.php
Documentation is available at class.mimePart.php
* @author Richard Heyes <richard@phpguru.org>
* @copyright Copyright (c) 2005
* @version $Revision: 1.3 $
* This file is part of the htmlMimeMail5 package (http://www.phpguru.org/)
* htmlMimeMail5 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* htmlMimeMail5 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with htmlMimeMail5; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* C Copyright 2005 Richard Heyes
* Raw mime encoding class
* This class enables you to manipulate and build
* a mime email from the ground up.
* Why use this instead of mime.php?
* mime.php is a userfriendly api to this class for
* people who aren't interested in the internals of
* mime mail. This class however allows full control
* // Since multipart/mixed has no real body, (the body is
* // the subpart), we set the body argument to blank.
* $params['content_type'] = 'multipart/mixed';
* $email = new src_mail_MIMEPart('', $params);
* // Here we add a text part to the multipart we have
* // already. Assume $body contains plain text.
* $params['content_type'] = 'text/plain';
* $params['encoding'] = '7bit';
* $text = $email->addSubPart($body, $params);
* // Now add an attachment. Assume $attach is
* the contents of the attachment
* $params['content_type'] = 'application/zip';
* $params['encoding'] = 'base64';
* $params['disposition'] = 'attachment';
* $params['dfilename'] = 'example.zip';
* $attach =& $email->addSubPart($body, $params);
* // Now build the email. Note that the encode
* // function returns an associative array containing two
* // elements, body and headers. You will need to add extra
* // headers, (eg. Mime-Version) before sending.
* $email = $message->encode();
* $email['headers'][] = 'Mime-Version: 1.0';
* Further examples are available at http://www.phpguru.org
* - Set encode() to return the $obj->encoded if encode()
* has already been run. Unless a flag is passed to specifically
* Short description of class src_foundation_admin_ADMINARCHIV
* @author Richard Heyes <richard@phpguru.org>
* @copyright Copyright (c) 2005
* @version $Revision: 1.3 $
* The encoding type of this part
* The output of this part after being built
* The body of this part (not encoded)
* @param $body - The body of the mime part if any.
* @param $params - An associative array of parameters:
* content_type - The content type for this part eg multipart/mixed
* encoding - The encoding to use, 7bit, 8bit, base64, or quoted-printable
* cid - Content ID to apply
* disposition - Content disposition, inline or attachment
* dfilename - Optional filename parameter for content disposition
* description - Content description
* charset - Character set to use
public function __construct($body =
'', $params =
array ()) {
if (!defined('MAIL_MIMEPART_CRLF')) {
define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ?
MAIL_MIME_CRLF :
"\r\n", true);
foreach ($params as $key =>
$value) {
$headers['Content-Type'] =
$value .
(isset
($charset) ?
'; charset="' .
$charset .
'"' :
'');
$this->encoding =
$value;
$headers['Content-Transfer-Encoding'] =
$value;
$headers['Content-ID'] =
'<' .
$value .
'>';
$headers['Content-Disposition'] =
$value .
(isset
($dfilename) ?
'; filename="' .
$dfilename .
'"' :
'');
if (isset
($headers['Content-Disposition'])) {
$headers['Content-Disposition'] .=
'; filename="' .
$value .
'"';
$headers['Content-Description'] =
$value;
if (isset
($headers['Content-Type'])) {
$headers['Content-Type'] .=
'; charset="' .
$value .
'"';
if (!isset
($headers['Content-Type'])) {
$headers['Content-Type'] =
'text/plain';
if (!isset
($this->encoding)) {
$this->encoding =
'7bit';
// Assign stuff to member variables
$this->encoded =
array ();
$this->headers =
$headers;
* Encodes and returns the email. Also stores
* it in the encoded member variable
* @return An associative array containing two elements,
* body and headers. The headers element is itself
$encoded =
& $this->encoded;
if (!empty ($this->subparts)) {
$this->headers['Content-Type'] .=
';' .
MAIL_MIMEPART_CRLF .
"\t" .
'boundary="' .
$boundary .
'"';
// Add body parts to $subparts
for ($i =
0; $i <
count($this->subparts); $i++
) {
$tmp =
$this->subparts[$i]->encode();
foreach ($tmp['headers'] as $key =>
$value) {
$headers[] =
$key .
': ' .
$value;
$subparts[] =
implode(MAIL_MIMEPART_CRLF, $headers) .
MAIL_MIMEPART_CRLF .
MAIL_MIMEPART_CRLF .
$tmp['body'];
$encoded['body'] =
'--' .
$boundary .
MAIL_MIMEPART_CRLF .
implode('--' .
$boundary .
MAIL_MIMEPART_CRLF, $subparts) .
'--' .
$boundary .
'--' .
MAIL_MIMEPART_CRLF;
$encoded['body'] =
$this->getEncodedData($this->body, $this->encoding) .
MAIL_MIMEPART_CRLF;
// Add headers to $encoded
$encoded['headers'] =
& $this->headers;
* Adds a subpart to current mime part and returns
* @param $body The body of the subpart, if any.
* @param $params The parameters for the subpart, same
* as the $params argument for constructor.
* @return A reference to the part you just added.
return $this->subparts[count($this->subparts) -
1];
* Returns encoded data based upon encoding passed to it
* @param $data The data to encode.
* @param $encoding The encoding type to use, 7bit, base64,
private function getEncodedData($data, $encoding) {
case 'quoted-printable' :
return $this->quotedPrintableEncode($data);
* Encodes data to quoted-printable standard.
* @param $input The data to encode
* @param $line_max Optional max line length. Should
* not be more than 76 chars
private function quotedPrintableEncode($input, $line_max =
76) {
$eol =
MAIL_MIMEPART_CRLF;
while (list
(, $line) =
each($lines)) {
for ($i =
0; $i <
$linlen; $i++
) {
if (($dec ==
32) AND ($i ==
($linlen -
1))) { // convert space at eol only
; // Do nothing if a tab.
elseif (($dec ==
61) OR ($dec <
32) OR ($dec >
126)) {
if ((strlen($newline) +
strlen($char)) >=
$line_max) { // MAIL_MIMEPART_CRLF is not counted
$output .=
$newline .
$escape .
$eol; // soft line break; " =\r\n" is okay
$output .=
$newline .
$eol;
$output =
substr($output, 0, -
1 *
strlen($eol)); // Don't want last crlf
Documentation generated on Sat, 24 Mar 2007 09:59:39 +0100 by phpDocumentor 1.3.1