MimePart.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * A MIME part, in a multipart message.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
  15. {
  16. /** The format parameter last specified by the user */
  17. protected $userFormat;
  18. /** The charset last specified by the user */
  19. protected $userCharset;
  20. /** The delsp parameter last specified by the user */
  21. protected $userDelSp;
  22. /** The nesting level of this MimePart */
  23. private $nestingLevel = self::LEVEL_ALTERNATIVE;
  24. /**
  25. * Create a new MimePart with $headers, $encoder and $cache.
  26. *
  27. * @param string $charset
  28. */
  29. public function __construct(Swift_Mime_SimpleHeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_IdGenerator $idGenerator, $charset = null)
  30. {
  31. parent::__construct($headers, $encoder, $cache, $idGenerator);
  32. $this->setContentType('text/plain');
  33. if (null !== $charset) {
  34. $this->setCharset($charset);
  35. }
  36. }
  37. /**
  38. * Set the body of this entity, either as a string, or as an instance of
  39. * {@link Swift_OutputByteStream}.
  40. *
  41. * @param mixed $body
  42. * @param string $contentType optional
  43. * @param string $charset optional
  44. *
  45. * @return $this
  46. */
  47. public function setBody($body, $contentType = null, $charset = null)
  48. {
  49. if (isset($charset)) {
  50. $this->setCharset($charset);
  51. }
  52. $body = $this->convertString($body);
  53. parent::setBody($body, $contentType);
  54. return $this;
  55. }
  56. /**
  57. * Get the character set of this entity.
  58. *
  59. * @return string
  60. */
  61. public function getCharset()
  62. {
  63. return $this->getHeaderParameter('Content-Type', 'charset');
  64. }
  65. /**
  66. * Set the character set of this entity.
  67. *
  68. * @param string $charset
  69. *
  70. * @return $this
  71. */
  72. public function setCharset($charset)
  73. {
  74. $this->setHeaderParameter('Content-Type', 'charset', $charset);
  75. if ($charset !== $this->userCharset) {
  76. $this->clearCache();
  77. }
  78. $this->userCharset = $charset;
  79. parent::charsetChanged($charset);
  80. return $this;
  81. }
  82. /**
  83. * Get the format of this entity (i.e. flowed or fixed).
  84. *
  85. * @return string
  86. */
  87. public function getFormat()
  88. {
  89. return $this->getHeaderParameter('Content-Type', 'format');
  90. }
  91. /**
  92. * Set the format of this entity (flowed or fixed).
  93. *
  94. * @param string $format
  95. *
  96. * @return $this
  97. */
  98. public function setFormat($format)
  99. {
  100. $this->setHeaderParameter('Content-Type', 'format', $format);
  101. $this->userFormat = $format;
  102. return $this;
  103. }
  104. /**
  105. * Test if delsp is being used for this entity.
  106. *
  107. * @return bool
  108. */
  109. public function getDelSp()
  110. {
  111. return 'yes' === $this->getHeaderParameter('Content-Type', 'delsp');
  112. }
  113. /**
  114. * Turn delsp on or off for this entity.
  115. *
  116. * @param bool $delsp
  117. *
  118. * @return $this
  119. */
  120. public function setDelSp($delsp = true)
  121. {
  122. $this->setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  123. $this->userDelSp = $delsp;
  124. return $this;
  125. }
  126. /**
  127. * Get the nesting level of this entity.
  128. *
  129. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  130. *
  131. * @return int
  132. */
  133. public function getNestingLevel()
  134. {
  135. return $this->nestingLevel;
  136. }
  137. /**
  138. * Receive notification that the charset has changed on this document, or a
  139. * parent document.
  140. *
  141. * @param string $charset
  142. */
  143. public function charsetChanged($charset)
  144. {
  145. $this->setCharset($charset);
  146. }
  147. /** Fix the content-type and encoding of this entity */
  148. protected function fixHeaders()
  149. {
  150. parent::fixHeaders();
  151. if (\count($this->getChildren())) {
  152. $this->setHeaderParameter('Content-Type', 'charset', null);
  153. $this->setHeaderParameter('Content-Type', 'format', null);
  154. $this->setHeaderParameter('Content-Type', 'delsp', null);
  155. } else {
  156. $this->setCharset($this->userCharset);
  157. $this->setFormat($this->userFormat);
  158. $this->setDelSp($this->userDelSp);
  159. }
  160. }
  161. /** Set the nesting level of this entity */
  162. protected function setNestingLevel($level)
  163. {
  164. $this->nestingLevel = $level;
  165. }
  166. /** Encode charset when charset is not utf-8 */
  167. protected function convertString($string)
  168. {
  169. $charset = strtolower($this->getCharset() ?? '');
  170. if (!\in_array($charset, ['utf-8', 'iso-8859-1', 'iso-8859-15', ''])) {
  171. return mb_convert_encoding($string, $charset, 'utf-8');
  172. }
  173. return $string;
  174. }
  175. }