Attachment.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * An attachment, in a multipart message.
  11. *
  12. * @package Swift
  13. * @subpackage Mime
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
  17. {
  18. /** Recognized MIME types */
  19. private $_mimeTypes = array();
  20. /**
  21. * Create a new Attachment with $headers, $encoder and $cache.
  22. *
  23. * @param Swift_Mime_HeaderSet $headers
  24. * @param Swift_Mime_ContentEncoder $encoder
  25. * @param Swift_KeyCache $cache
  26. * @param Swift_Mime_Grammar $grammar
  27. * @param array $mimeTypes optional
  28. */
  29. public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $mimeTypes = array())
  30. {
  31. parent::__construct($headers, $encoder, $cache, $grammar);
  32. $this->setDisposition('attachment');
  33. $this->setContentType('application/octet-stream');
  34. $this->_mimeTypes = $mimeTypes;
  35. }
  36. /**
  37. * Get the nesting level used for this attachment.
  38. *
  39. * Always returns {@link LEVEL_MIXED}.
  40. *
  41. * @return integer
  42. */
  43. public function getNestingLevel()
  44. {
  45. return self::LEVEL_MIXED;
  46. }
  47. /**
  48. * Get the Content-Disposition of this attachment.
  49. *
  50. * By default attachments have a disposition of "attachment".
  51. *
  52. * @return string
  53. */
  54. public function getDisposition()
  55. {
  56. return $this->_getHeaderFieldModel('Content-Disposition');
  57. }
  58. /**
  59. * Set the Content-Disposition of this attachment.
  60. *
  61. * @param string $disposition
  62. *
  63. * @return Swift_Mime_Attachment
  64. */
  65. public function setDisposition($disposition)
  66. {
  67. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition)) {
  68. $this->getHeaders()->addParameterizedHeader(
  69. 'Content-Disposition', $disposition
  70. );
  71. }
  72. return $this;
  73. }
  74. /**
  75. * Get the filename of this attachment when downloaded.
  76. *
  77. * @return string
  78. */
  79. public function getFilename()
  80. {
  81. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  82. }
  83. /**
  84. * Set the filename of this attachment.
  85. *
  86. * @param string $filename
  87. *
  88. * @return Swift_Mime_Attachment
  89. */
  90. public function setFilename($filename)
  91. {
  92. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  93. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  94. return $this;
  95. }
  96. /**
  97. * Get the file size of this attachment.
  98. *
  99. * @return integer
  100. */
  101. public function getSize()
  102. {
  103. return $this->_getHeaderParameter('Content-Disposition', 'size');
  104. }
  105. /**
  106. * Set the file size of this attachment.
  107. *
  108. * @param integer $size
  109. *
  110. * @return Swift_Mime_Attachment
  111. */
  112. public function setSize($size)
  113. {
  114. $this->_setHeaderParameter('Content-Disposition', 'size', $size);
  115. return $this;
  116. }
  117. /**
  118. * Set the file that this attachment is for.
  119. *
  120. * @param Swift_FileStream $file
  121. * @param string $contentType optional
  122. *
  123. * @return Swift_Mime_Attachment
  124. */
  125. public function setFile(Swift_FileStream $file, $contentType = null)
  126. {
  127. $this->setFilename(basename($file->getPath()));
  128. $this->setBody($file, $contentType);
  129. if (!isset($contentType)) {
  130. $extension = strtolower(substr(
  131. $file->getPath(), strrpos($file->getPath(), '.') + 1
  132. ));
  133. if (array_key_exists($extension, $this->_mimeTypes)) {
  134. $this->setContentType($this->_mimeTypes[$extension]);
  135. }
  136. }
  137. return $this;
  138. }
  139. }