MimePart.php 5.8KB

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