3 * File containing the ezcMailVirtualFile 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 * Mail part for binary data in memory.
14 * @property string $contents
15 * The contents to be added as an attachment. The mimeType and
16 * contentType are set in the constructor or if not specified they
17 * are extracted with the fileinfo extension if it is available,
18 * otherwise they are set to application/octet-stream.
23 class ezcMailVirtualFile
extends ezcMailFilePart
26 * Constructs a new attachment with $fileName and $contents.
28 * If the $mimeType and $contentType are not specified they are extracted
29 * with the fileinfo extension if it is available, otherwise they are set
30 * to application/octet-stream.
32 * @param string $fileName
33 * @param string $contents
34 * @param string $contentType
35 * @param string $mimeType
37 public function __construct( $fileName, $contents, $contentType = null, $mimeType = null )
39 parent
::__construct( $fileName );
40 $this->contents
= $contents;
42 if ( $contentType != null && $mimeType != null )
44 $this->contentType
= $contentType;
45 $this->mimeType
= $mimeType;
47 elseif ( ezcBaseFeatures
::hasExtensionSupport( 'fileinfo' ) )
49 // get mime and content type
50 $fileInfo = new finfo( FILEINFO_MIME
);
51 $mimeParts = $fileInfo->buffer( $contents );
52 if ( $mimeParts !== false && strpos( $mimeParts, '/' ) !== false )
54 list( $this->contentType
, $this->mimeType
) = explode( '/', $mimeParts );
58 // default to mimetype application/octet-stream
59 $this->contentType
= self
::CONTENT_TYPE_APPLICATION
;
60 $this->mimeType
= "octet-stream";
65 // default to mimetype application/octet-stream
66 $this->contentType
= self
::CONTENT_TYPE_APPLICATION
;
67 $this->mimeType
= "octet-stream";
72 * Sets the property $name to $value.
74 * @throws ezcBasePropertyNotFoundException
75 * if the property does not exist.
80 public function __set( $name, $value )
85 $this->properties
[$name] = $value;
88 return parent
::__set( $name, $value );
94 * Returns the value of property $value.
96 * @throws ezcBasePropertyNotFoundException
97 * if the property does not exist.
102 public function __get( $name )
107 return $this->properties
[$name];
110 return parent
::__get( $name );
116 * Returns true if the property $name is set, otherwise false.
118 * @param string $name
122 public function __isset( $name )
127 return isset( $this->properties
[$name] );
130 return parent
::__isset( $name );
135 * Returns the contents of the file with the correct encoding.
139 public function generateBody()
141 return chunk_split( base64_encode( $this->contents
), 76, ezcMailTools
::lineBreak() );