MimePart.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 Swift_Mime_HeaderSet $headers
  28. * @param Swift_Mime_ContentEncoder $encoder
  29. * @param Swift_KeyCache $cache
  30. * @param Swift_Mime_Grammar $grammar
  31. * @param string $charset
  32. */
  33. public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
  34. {
  35. parent::__construct($headers, $encoder, $cache, $grammar);
  36. $this->setContentType('text/plain');
  37. if (!is_null($charset)) {
  38. $this->setCharset($charset);
  39. }
  40. }
  41. /**
  42. * Set the body of this entity, either as a string, or as an instance of
  43. * {@link Swift_OutputByteStream}.
  44. *
  45. * @param mixed $body
  46. * @param string $contentType optional
  47. * @param string $charset optional
  48. *
  49. * @return Swift_Mime_MimePart
  50. */
  51. public function setBody($body, $contentType = null, $charset = null)
  52. {
  53. if (isset($charset)) {
  54. $this->setCharset($charset);
  55. }
  56. $body = $this->_convertString($body);
  57. parent::setBody($body, $contentType);
  58. return $this;
  59. }
  60. /**
  61. * Get the character set of this entity.
  62. *
  63. * @return string
  64. */
  65. public function getCharset()
  66. {
  67. return $this->_getHeaderParameter('Content-Type', 'charset');
  68. }
  69. /**
  70. * Set the character set of this entity.
  71. *
  72. * @param string $charset
  73. *
  74. * @return Swift_Mime_MimePart
  75. */
  76. public function setCharset($charset)
  77. {
  78. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  79. if ($charset !== $this->_userCharset) {
  80. $this->_clearCache();
  81. }
  82. $this->_userCharset = $charset;
  83. parent::charsetChanged($charset);
  84. return $this;
  85. }
  86. /**
  87. * Get the format of this entity (i.e. flowed or fixed).
  88. *
  89. * @return string
  90. */
  91. public function getFormat()
  92. {
  93. return $this->_getHeaderParameter('Content-Type', 'format');
  94. }
  95. /**
  96. * Set the format of this entity (flowed or fixed).
  97. *
  98. * @param string $format
  99. *
  100. * @return Swift_Mime_MimePart
  101. */
  102. public function setFormat($format)
  103. {
  104. $this->_setHeaderParameter('Content-Type', 'format', $format);
  105. $this->_userFormat = $format;
  106. return $this;
  107. }
  108. /**
  109. * Test if delsp is being used for this entity.
  110. *
  111. * @return bool
  112. */
  113. public function getDelSp()
  114. {
  115. return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
  116. ? true
  117. : false;
  118. }
  119. /**
  120. * Turn delsp on or off for this entity.
  121. *
  122. * @param bool $delsp
  123. *
  124. * @return Swift_Mime_MimePart
  125. */
  126. public function setDelSp($delsp = true)
  127. {
  128. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  129. $this->_userDelSp = $delsp;
  130. return $this;
  131. }
  132. /**
  133. * Get the nesting level of this entity.
  134. *
  135. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  136. *
  137. * @return int
  138. */
  139. public function getNestingLevel()
  140. {
  141. return $this->_nestingLevel;
  142. }
  143. /**
  144. * Receive notification that the charset has changed on this document, or a
  145. * parent document.
  146. *
  147. * @param string $charset
  148. */
  149. public function charsetChanged($charset)
  150. {
  151. $this->setCharset($charset);
  152. }
  153. /** Fix the content-type and encoding of this entity */
  154. protected function _fixHeaders()
  155. {
  156. parent::_fixHeaders();
  157. if (count($this->getChildren())) {
  158. $this->_setHeaderParameter('Content-Type', 'charset', null);
  159. $this->_setHeaderParameter('Content-Type', 'format', null);
  160. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  161. } else {
  162. $this->setCharset($this->_userCharset);
  163. $this->setFormat($this->_userFormat);
  164. $this->setDelSp($this->_userDelSp);
  165. }
  166. }
  167. /** Set the nesting level of this entity */
  168. protected function _setNestingLevel($level)
  169. {
  170. $this->_nestingLevel = $level;
  171. }
  172. /** Encode charset when charset is not utf-8 */
  173. protected function _convertString($string)
  174. {
  175. $charset = strtolower($this->getCharset());
  176. if (!in_array($charset, array('utf-8', 'iso-8859-1', ''))) {
  177. // mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
  178. if (function_exists('mb_convert_encoding')) {
  179. $string = mb_convert_encoding($string, 'utf-8', $charset);
  180. } elseif (function_exists('iconv')) {
  181. $string = iconv($charset, 'utf-8//TRANSLIT//IGNORE', $string);
  182. } else {
  183. throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your charset or install the mbstring or iconv extension).');
  184. }
  185. return $string;
  186. }
  187. return $string;
  188. }
  189. }