3 * File containing the ezcMailMultipartRelated class
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
12 * ezcMailMultipartRelated is intended for mail parts consisting of
13 * several inter-related body parts.
15 * A typical example is an HTML mail with embedded images.
16 * When you want to refer to a related part you can use content id's
17 * (cid). Set the 'Content-ID' header of the related part to a valid
18 * unique url-addr-spec (specified by RFC 822) and refer to it through
19 * the form cid:unique-string.
22 * This example shows how you can use ezcMailMultipartRelated to create an
23 * HTML mail with an inline image.
25 * $mail = new ezcMail();
26 * $mail->from = new ezcMailAddress( 'sender@example.com', 'Adrian Ripburger' );
27 * $mail->addTo( new ezcMailAddress( 'receiver@example.com', 'Maureen Corley' ) );
28 * $mail->subject = "Example of an HTML email with attachments";
29 * $htmlText = new ezcMailText( "<html>Image <img src='cid:image@12345' /></html>" );
30 * $htmlText->subType = 'html';
31 * $image = new ezcMailFile( "path_to_my_image.jpg" );
32 * $image->contentId = 'image@12345';
33 * $mail->body = new ezcMailMultipartRelated( $htmlText, $image );
39 class ezcMailMultipartRelated
extends ezcMailMultipart
42 * Constructs a new ezcMailMultipartRelated.
44 * The constructor accepts an arbitrary number of ezcMailParts or arrays with ezcMailparts.
45 * Parts are added in the order provided and the first part will be recognized
46 * as the main body. Parameters of the wrong type are ignored.
48 * @param ezcMailPart|array(ezcMailPart) $...
50 public function __construct()
52 $args = func_get_args();
53 parent
::__construct( $args );
57 * Sets the main part $part of this alternative multipart.
59 * @param ezcMailPart $part
61 public function setMainPart( ezcMailPart
$part )
63 $this->parts
[0] = $part;
67 * Adds $part to the list of parts and returns the Content-ID of the part.
69 * @param ezcMailPart $part
72 public function addRelatedPart( ezcMailPart
$part )
74 // it doesn't have a Content-ID, we must set one.
76 if ( $part->getHeader( 'Content-ID' ) == '' )
78 if ( $part instanceof ezcMailFile
)
80 $part->contentId
= ezcMailTools
::generateContentId( basename( $part->fileName
) );
84 $part->setHeader( 'Content-ID', ezcMailTools
::generateContentId( 'part' ) );
87 $contentId = trim( $part->getHeader( 'Content-ID' ), '<>' );
89 // Set the content ID property of the ezcMailFile if one was found
90 if ( $part instanceof ezcMailFile
)
92 $part->contentId
= $contentId;
95 if ( count( $this->parts
) > 0 )
97 $this->parts
[] = $part;
101 $this->parts
[1] = $part;
107 * Returns the main part of this multipart or null if there is no such part.
109 * @return array(ezcMailPart)
111 public function getMainPart()
113 if ( isset( $this->parts
[0] ) )
115 return $this->parts
[0];
121 * Returns the mail parts associated with this multipart.
123 * @return array(ezcMailPart)
125 public function getRelatedParts()
127 if ( is_null( $this->getMainPart() ) )
129 return array_slice( $this->parts
, 0 );
131 return array_slice( $this->parts
, 1 );
135 * Returns the part associated with the passed Content-ID.
138 * @return ezcMailPart
140 public function getRelatedPartByID( $cid )
142 $parts = $this->getRelatedParts();
143 foreach ( $parts as $part )
145 if ( ( $part->getHeader( 'Content-ID' ) !== '' ) &&
146 ( $part->getHeader( 'Content-ID' ) == "<$cid>" ) )
159 public function multipartType()
165 * Substitutes links in all ezcMailText parts with the subType HTML in this multipart/alternative.
167 * This method will perform substitution of CID's and absolute and relative links as specified by
168 * RFC 2557 for links that resolve to files. Links to other message parts must be resolved when
169 * displaying the message.
171 * - provide methods for substitution of these as well (inclusive listing of which they are)
172 * @todo Move to separate class.
174 private function resolveHtmlLinks()
176 // 1. Check that the main part is a html part
177 // 2. Go through the related parts and build up a structure of available
178 // CIDS and locations
179 // 3. Substitute in this message.