TYPO3 API  SVNRelease
Attachment.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Mime/SimpleMimeEntity.php';
00012 //@require 'Swift/Mime/ContentEncoder.php';
00013 //@require 'Swift/Mime/HeaderSet.php';
00014 //@require 'Swift/FileStream.php';
00015 //@require 'Swift/KeyCache.php';
00016 
00017 /**
00018  * An attachment, in a multipart message.
00019  * @package Swift
00020  * @subpackage Mime
00021  * @author Chris Corbyn
00022  */
00023 class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
00024 {
00025 
00026   /** Recognized MIME types */
00027   private $_mimeTypes = array();
00028 
00029   /**
00030    * Create a new Attachment with $headers, $encoder and $cache.
00031    * @param Swift_Mime_HeaderSet $headers
00032    * @param Swift_Mime_ContentEncoder $encoder
00033    * @param Swift_KeyCache $cache
00034    * @param array $mimeTypes optional
00035    */
00036   public function __construct(Swift_Mime_HeaderSet $headers,
00037     Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache,
00038     $mimeTypes = array())
00039   {
00040     parent::__construct($headers, $encoder, $cache);
00041     $this->setDisposition('attachment');
00042     $this->setContentType('application/octet-stream');
00043     $this->_mimeTypes = $mimeTypes;
00044   }
00045 
00046   /**
00047    * Get the nesting level used for this attachment.
00048    * Always returns {@link LEVEL_MIXED}.
00049    * @return int
00050    */
00051   public function getNestingLevel()
00052   {
00053     return self::LEVEL_MIXED;
00054   }
00055 
00056   /**
00057    * Get the Content-Disposition of this attachment.
00058    * By default attachments have a disposition of "attachment".
00059    * @return string
00060    */
00061   public function getDisposition()
00062   {
00063     return $this->_getHeaderFieldModel('Content-Disposition');
00064   }
00065 
00066   /**
00067    * Set the Content-Disposition of this attachment.
00068    * @param string $disposition
00069    */
00070   public function setDisposition($disposition)
00071   {
00072     if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition))
00073     {
00074       $this->getHeaders()->addParameterizedHeader(
00075         'Content-Disposition', $disposition
00076         );
00077     }
00078     return $this;
00079   }
00080 
00081   /**
00082    * Get the filename of this attachment when downloaded.
00083    * @return string
00084    */
00085   public function getFilename()
00086   {
00087     return $this->_getHeaderParameter('Content-Disposition', 'filename');
00088   }
00089 
00090   /**
00091    * Set the filename of this attachment.
00092    * @param string $filename
00093    */
00094   public function setFilename($filename)
00095   {
00096     $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
00097     $this->_setHeaderParameter('Content-Type', 'name', $filename);
00098     return $this;
00099   }
00100 
00101   /**
00102    * Get the file size of this attachment.
00103    * @return int
00104    */
00105   public function getSize()
00106   {
00107     return $this->_getHeaderParameter('Content-Disposition', 'size');
00108   }
00109 
00110   /**
00111    * Set the file size of this attachment.
00112    * @param int $size
00113    */
00114   public function setSize($size)
00115   {
00116     $this->_setHeaderParameter('Content-Disposition', 'size', $size);
00117     return $this;
00118   }
00119 
00120   /**
00121    * Set the file that this attachment is for.
00122    * @param Swift_FileStream $file
00123    * @param string $contentType optional
00124    */
00125   public function setFile(Swift_FileStream $file, $contentType = null)
00126   {
00127     $this->setFilename(basename($file->getPath()));
00128     $this->setBody($file, $contentType);
00129     if (!isset($contentType))
00130     {
00131       $extension = strtolower(substr(
00132         $file->getPath(), strrpos($file->getPath(), '.') + 1
00133         ));
00134 
00135       if (array_key_exists($extension, $this->_mimeTypes))
00136       {
00137         $this->setContentType($this->_mimeTypes[$extension]);
00138       }
00139     }
00140     return $this;
00141   }
00142 
00143 }