Attachment.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
  16. {
  17. /** Recognized MIME types */
  18. private $_mimeTypes = array();
  19. /**
  20. * Create a new Attachment with $headers, $encoder and $cache.
  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,
  28. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache,
  29. 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. * Always returns {@link LEVEL_MIXED}.
  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. * By default attachments have a disposition of "attachment".
  48. * @return string
  49. */
  50. public function getDisposition()
  51. {
  52. return $this->_getHeaderFieldModel('Content-Disposition');
  53. }
  54. /**
  55. * Set the Content-Disposition of this attachment.
  56. * @param string $disposition
  57. * @return Swift_Mime_Attachment
  58. */
  59. public function setDisposition($disposition)
  60. {
  61. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition))
  62. {
  63. $this->getHeaders()->addParameterizedHeader(
  64. 'Content-Disposition', $disposition
  65. );
  66. }
  67. return $this;
  68. }
  69. /**
  70. * Get the filename of this attachment when downloaded.
  71. * @return string
  72. */
  73. public function getFilename()
  74. {
  75. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  76. }
  77. /**
  78. * Set the filename of this attachment.
  79. * @param string $filename
  80. * @return Swift_Mime_Attachment
  81. */
  82. public function setFilename($filename)
  83. {
  84. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  85. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  86. return $this;
  87. }
  88. /**
  89. * Get the file size of this attachment.
  90. * @return int
  91. */
  92. public function getSize()
  93. {
  94. return $this->_getHeaderParameter('Content-Disposition', 'size');
  95. }
  96. /**
  97. * Set the file size of this attachment.
  98. * @param int $size
  99. * @return Swift_Mime_Attachment
  100. */
  101. public function setSize($size)
  102. {
  103. $this->_setHeaderParameter('Content-Disposition', 'size', $size);
  104. return $this;
  105. }
  106. /**
  107. * Set the file that this attachment is for.
  108. * @param Swift_FileStream $file
  109. * @param string $contentType optional
  110. * @return Swift_Mime_Attachment
  111. */
  112. public function setFile(Swift_FileStream $file, $contentType = null)
  113. {
  114. $this->setFilename(basename($file->getPath()));
  115. $this->setBody($file, $contentType);
  116. if (!isset($contentType))
  117. {
  118. $extension = strtolower(substr(
  119. $file->getPath(), strrpos($file->getPath(), '.') + 1
  120. ));
  121. if (array_key_exists($extension, $this->_mimeTypes))
  122. {
  123. $this->setContentType($this->_mimeTypes[$extension]);
  124. }
  125. }
  126. return $this;
  127. }
  128. }