Attachment.php 3.9KB

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, 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. * Always returns {@link LEVEL_MIXED}.
  37. * @return int
  38. */
  39. public function getNestingLevel()
  40. {
  41. return self::LEVEL_MIXED;
  42. }
  43. /**
  44. * Get the Content-Disposition of this attachment.
  45. * By default attachments have a disposition of "attachment".
  46. * @return string
  47. */
  48. public function getDisposition()
  49. {
  50. return $this->_getHeaderFieldModel('Content-Disposition');
  51. }
  52. /**
  53. * Set the Content-Disposition of this attachment.
  54. * @param string $disposition
  55. * @return Swift_Mime_Attachment
  56. */
  57. public function setDisposition($disposition)
  58. {
  59. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition)) {
  60. $this->getHeaders()->addParameterizedHeader(
  61. 'Content-Disposition', $disposition
  62. );
  63. }
  64. return $this;
  65. }
  66. /**
  67. * Get the filename of this attachment when downloaded.
  68. * @return string
  69. */
  70. public function getFilename()
  71. {
  72. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  73. }
  74. /**
  75. * Set the filename of this attachment.
  76. * @param string $filename
  77. * @return Swift_Mime_Attachment
  78. */
  79. public function setFilename($filename)
  80. {
  81. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  82. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  83. return $this;
  84. }
  85. /**
  86. * Get the file size of this attachment.
  87. * @return int
  88. */
  89. public function getSize()
  90. {
  91. return $this->_getHeaderParameter('Content-Disposition', 'size');
  92. }
  93. /**
  94. * Set the file size of this attachment.
  95. * @param int $size
  96. * @return Swift_Mime_Attachment
  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. * @return Swift_Mime_Attachment
  108. */
  109. public function setFile(Swift_FileStream $file, $contentType = null)
  110. {
  111. $this->setFilename(basename($file->getPath()));
  112. $this->setBody($file, $contentType);
  113. if (!isset($contentType)) {
  114. $extension = strtolower(substr(
  115. $file->getPath(), strrpos($file->getPath(), '.') + 1
  116. ));
  117. if (array_key_exists($extension, $this->_mimeTypes)) {
  118. $this->setContentType($this->_mimeTypes[$extension]);
  119. }
  120. }
  121. return $this;
  122. }
  123. }