Attachment.php 3.5 KB

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