Attachment.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. */
  58. public function setDisposition($disposition)
  59. {
  60. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition))
  61. {
  62. $this->getHeaders()->addParameterizedHeader(
  63. 'Content-Disposition', $disposition
  64. );
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Get the filename of this attachment when downloaded.
  70. * @return string
  71. */
  72. public function getFilename()
  73. {
  74. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  75. }
  76. /**
  77. * Set the filename of this attachment.
  78. * @param string $filename
  79. */
  80. public function setFilename($filename)
  81. {
  82. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  83. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  84. return $this;
  85. }
  86. /**
  87. * Get the file size of this attachment.
  88. * @return int
  89. */
  90. public function getSize()
  91. {
  92. return $this->_getHeaderParameter('Content-Disposition', 'size');
  93. }
  94. /**
  95. * Set the file size of this attachment.
  96. * @param int $size
  97. */
  98. public function setSize($size)
  99. {
  100. $this->_setHeaderParameter('Content-Disposition', 'size', $size);
  101. return $this;
  102. }
  103. /**
  104. * Set the file that this attachment is for.
  105. * @param Swift_FileStream $file
  106. * @param string $contentType optional
  107. */
  108. public function setFile(Swift_FileStream $file, $contentType = null)
  109. {
  110. $this->setFilename(basename($file->getPath()));
  111. $this->setBody($file, $contentType);
  112. if (!isset($contentType))
  113. {
  114. $extension = strtolower(substr(
  115. $file->getPath(), strrpos($file->getPath(), '.') + 1
  116. ));
  117. if (array_key_exists($extension, $this->_mimeTypes))
  118. {
  119. $this->setContentType($this->_mimeTypes[$extension]);
  120. }
  121. }
  122. return $this;
  123. }
  124. }