3 * File containing the ezcMailFile 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 from the file system.
17 class ezcMailFile
extends ezcMailFilePart
20 * Constructs a new attachment with $fileName.
22 * If the $mimeType and $contentType are not specified they are extracted
23 * with the fileinfo extension if it is available, otherwise they are set
24 * to application/octet-stream.
26 * @param string $fileName
27 * @param string $contentType
28 * @param string $mimeType
30 public function __construct( $fileName, $contentType = null, $mimeType = null )
32 parent
::__construct( $fileName );
34 if ( $contentType != null && $mimeType != null )
36 $this->contentType
= $contentType;
37 $this->mimeType
= $mimeType;
39 elseif ( ezcBaseFeatures
::hasExtensionSupport( 'fileinfo' ) )
41 // get mime and content type
42 $fileInfo = finfo_open( FILEINFO_MIME
);
43 $mimeParts = finfo_file( $fileInfo, $fileName );
44 if ( $mimeParts !== false && strpos( $mimeParts, '/' ) !== false )
46 list( $this->contentType
, $this->mimeType
) = explode( '/', $mimeParts );
50 // default to mimetype application/octet-stream
51 $this->contentType
= self
::CONTENT_TYPE_APPLICATION
;
52 $this->mimeType
= "octet-stream";
54 finfo_close( $fileInfo );
58 // default to mimetype application/octet-stream
59 $this->contentType
= self
::CONTENT_TYPE_APPLICATION
;
60 $this->mimeType
= "octet-stream";
65 * Sets the property $name to $value.
67 * @throws ezcBasePropertyNotFoundException
68 * if the property does not exist.
69 * @throws ezcBaseFileNotFoundException
70 * when setting the property with an invalid filename.
75 public function __set( $name, $value )
80 if ( is_readable( $value ) )
82 parent
::__set( $name, $value );
86 throw new ezcBaseFileNotFoundException( $value );
90 return parent
::__set( $name, $value );
96 * Returns the value of property $value.
98 * @throws ezcBasePropertyNotFoundException
99 * if the property does not exist.
100 * @param string $name
104 public function __get( $name )
109 return parent
::__get( $name );
115 * Returns true if the property $name is set, otherwise false.
117 * @param string $name
121 public function __isset( $name )
126 return parent
::__isset( $name );
131 * Returns the contents of the file with the correct encoding.
135 public function generateBody()
137 return chunk_split( base64_encode( file_get_contents( $this->fileName
) ), 76, ezcMailTools
::lineBreak() );