您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\swiftmailer;
  8. use yii\mail\BaseMessage;
  9. /**
  10. * Message implements a message class based on SwiftMailer.
  11. *
  12. * @see http://swiftmailer.org/docs/messages.html
  13. * @see Mailer
  14. *
  15. * @method Mailer getMailer() returns mailer instance.
  16. *
  17. * @property \Swift_Message $swiftMessage Swift message instance. This property is read-only.
  18. *
  19. * @author Paul Klimov <klimov.paul@gmail.com>
  20. * @since 2.0
  21. */
  22. class Message extends BaseMessage
  23. {
  24. /**
  25. * @var \Swift_Message Swift message instance.
  26. */
  27. private $_swiftMessage;
  28. /**
  29. * @return \Swift_Message Swift message instance.
  30. */
  31. public function getSwiftMessage()
  32. {
  33. if (!is_object($this->_swiftMessage)) {
  34. $this->_swiftMessage = $this->createSwiftMessage();
  35. }
  36. return $this->_swiftMessage;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getCharset()
  42. {
  43. return $this->getSwiftMessage()->getCharset();
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function setCharset($charset)
  49. {
  50. $this->getSwiftMessage()->setCharset($charset);
  51. return $this;
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function getFrom()
  57. {
  58. return $this->getSwiftMessage()->getFrom();
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function setFrom($from)
  64. {
  65. $this->getSwiftMessage()->setFrom($from);
  66. return $this;
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function getReplyTo()
  72. {
  73. return $this->getSwiftMessage()->getReplyTo();
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function setReplyTo($replyTo)
  79. {
  80. $this->getSwiftMessage()->setReplyTo($replyTo);
  81. return $this;
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function getTo()
  87. {
  88. return $this->getSwiftMessage()->getTo();
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function setTo($to)
  94. {
  95. $this->getSwiftMessage()->setTo($to);
  96. return $this;
  97. }
  98. /**
  99. * @inheritdoc
  100. */
  101. public function getCc()
  102. {
  103. return $this->getSwiftMessage()->getCc();
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function setCc($cc)
  109. {
  110. $this->getSwiftMessage()->setCc($cc);
  111. return $this;
  112. }
  113. /**
  114. * @inheritdoc
  115. */
  116. public function getBcc()
  117. {
  118. return $this->getSwiftMessage()->getBcc();
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function setBcc($bcc)
  124. {
  125. $this->getSwiftMessage()->setBcc($bcc);
  126. return $this;
  127. }
  128. /**
  129. * @inheritdoc
  130. */
  131. public function getSubject()
  132. {
  133. return $this->getSwiftMessage()->getSubject();
  134. }
  135. /**
  136. * @inheritdoc
  137. */
  138. public function setSubject($subject)
  139. {
  140. $this->getSwiftMessage()->setSubject($subject);
  141. return $this;
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function setTextBody($text)
  147. {
  148. $this->setBody($text, 'text/plain');
  149. return $this;
  150. }
  151. /**
  152. * @inheritdoc
  153. */
  154. public function setHtmlBody($html)
  155. {
  156. $this->setBody($html, 'text/html');
  157. return $this;
  158. }
  159. /**
  160. * Sets the message body.
  161. * If body is already set and its content type matches given one, it will
  162. * be overridden, if content type miss match the multipart message will be composed.
  163. * @param string $body body content.
  164. * @param string $contentType body content type.
  165. */
  166. protected function setBody($body, $contentType)
  167. {
  168. $message = $this->getSwiftMessage();
  169. $oldBody = $message->getBody();
  170. $charset = $message->getCharset();
  171. if (empty($oldBody)) {
  172. $parts = $message->getChildren();
  173. $partFound = false;
  174. foreach ($parts as $key => $part) {
  175. if (!($part instanceof \Swift_Mime_Attachment)) {
  176. /* @var $part \Swift_Mime_MimePart */
  177. if ($part->getContentType() == $contentType) {
  178. $charset = $part->getCharset();
  179. unset($parts[$key]);
  180. $partFound = true;
  181. break;
  182. }
  183. }
  184. }
  185. if ($partFound) {
  186. reset($parts);
  187. $message->setChildren($parts);
  188. $message->addPart($body, $contentType, $charset);
  189. } else {
  190. $message->setBody($body, $contentType);
  191. }
  192. } else {
  193. $oldContentType = $message->getContentType();
  194. if ($oldContentType == $contentType) {
  195. $message->setBody($body, $contentType);
  196. } else {
  197. $message->setBody(null);
  198. $message->setContentType(null);
  199. $message->addPart($oldBody, $oldContentType, $charset);
  200. $message->addPart($body, $contentType, $charset);
  201. }
  202. }
  203. }
  204. /**
  205. * @inheritdoc
  206. */
  207. public function attach($fileName, array $options = [])
  208. {
  209. $attachment = \Swift_Attachment::fromPath($fileName);
  210. if (!empty($options['fileName'])) {
  211. $attachment->setFilename($options['fileName']);
  212. }
  213. if (!empty($options['contentType'])) {
  214. $attachment->setContentType($options['contentType']);
  215. }
  216. $this->getSwiftMessage()->attach($attachment);
  217. return $this;
  218. }
  219. /**
  220. * @inheritdoc
  221. */
  222. public function attachContent($content, array $options = [])
  223. {
  224. $attachment = \Swift_Attachment::newInstance($content);
  225. if (!empty($options['fileName'])) {
  226. $attachment->setFilename($options['fileName']);
  227. }
  228. if (!empty($options['contentType'])) {
  229. $attachment->setContentType($options['contentType']);
  230. }
  231. $this->getSwiftMessage()->attach($attachment);
  232. return $this;
  233. }
  234. /**
  235. * @inheritdoc
  236. */
  237. public function embed($fileName, array $options = [])
  238. {
  239. $embedFile = \Swift_EmbeddedFile::fromPath($fileName);
  240. if (!empty($options['fileName'])) {
  241. $embedFile->setFilename($options['fileName']);
  242. }
  243. if (!empty($options['contentType'])) {
  244. $embedFile->setContentType($options['contentType']);
  245. }
  246. return $this->getSwiftMessage()->embed($embedFile);
  247. }
  248. /**
  249. * @inheritdoc
  250. */
  251. public function embedContent($content, array $options = [])
  252. {
  253. $embedFile = \Swift_EmbeddedFile::newInstance($content);
  254. if (!empty($options['fileName'])) {
  255. $embedFile->setFilename($options['fileName']);
  256. }
  257. if (!empty($options['contentType'])) {
  258. $embedFile->setContentType($options['contentType']);
  259. }
  260. return $this->getSwiftMessage()->embed($embedFile);
  261. }
  262. /**
  263. * @inheritdoc
  264. */
  265. public function toString()
  266. {
  267. return $this->getSwiftMessage()->toString();
  268. }
  269. /**
  270. * Creates the Swift email message instance.
  271. * @return \Swift_Message email message instance.
  272. */
  273. protected function createSwiftMessage()
  274. {
  275. return new \Swift_Message();
  276. }
  277. }