Attachment.php 3.9 KB

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