You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.2KB

  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. }