PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/application/protected/extensions/mail/YiiMailMessage.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 148 lines | 54 code | 11 blank | 83 comment | 5 complexity | 6e931c5dd60fff335d53c8fc7e73e48f MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * YiiMailMessage class file.
  4. *
  5. * @author Jonah Turnquist <poppitypop@gmail.com>
  6. * @link https://code.google.com/p/yii-mail/
  7. * @package Yii-Mail
  8. */
  9. /**
  10. * Any requests to set or get attributes or call methods on this class that are
  11. * not found in that class are redirected to the {@link Swift_Mime_Message}
  12. * object.
  13. *
  14. * This means you need to look at the Swift Mailer documentation to see what
  15. * methods are availiable for this class. There are a <b>lot</b> of methods,
  16. * more than I wish to document. Any methods availiable in
  17. * {@link Swift_Mime_Message} are availiable here.
  18. *
  19. * Documentation for the most important methods can be found at
  20. * {@link http://swiftmailer.org/docs/messages}
  21. *
  22. * The YiiMailMessage component also allows using a shorthand for methods in
  23. * {@link Swift_Mime_Message} that start with set* or get*
  24. * For instance, instead of calling $message->setFrom('...') you can use
  25. * $message->from = '...'.
  26. *
  27. * Here are a few methods to get you started:
  28. * <ul>
  29. * <li>setSubject('Your subject')</li>
  30. * <li>setFrom(array('john@doe.com' => 'John Doe'))</li>
  31. * <li>setTo(array('receiver@domain.org', 'other@domain.org' => 'Name'))</li>
  32. * <li>attach(Swift_Attachment::fromPath('my-document.pdf'))</li>
  33. * </ul>
  34. */
  35. class YiiMailMessage extends CComponent {
  36. /**
  37. * @var string the view to use for rendering the body, null if no view is
  38. * used. An extra variable $mail will be passed to the view .which you may
  39. * use to set e.g. the email subject from within the view
  40. */
  41. public $view;
  42. /**
  43. * @var Swift_Mime_Message
  44. */
  45. public $message;
  46. /**
  47. * Any requests to set or get attributes or call methods on this class that
  48. * are not found are redirected to the {@link Swift_Mime_Message} object.
  49. * @param string the attribute name
  50. */
  51. public function __get($name) {
  52. try {
  53. return parent::__get($name);
  54. } catch (CException $e) {
  55. $getter = 'get' . $name;
  56. if (method_exists($this->message, $getter))
  57. return $this->message->$getter();
  58. else
  59. throw $e;
  60. }
  61. }
  62. /**
  63. * Any requests to set or get attributes or call methods on this class that
  64. * are not found are redirected to the {@link Swift_Mime_Message} object.
  65. * @param string the attribute name
  66. */
  67. public function __set($name, $value) {
  68. try {
  69. return parent::__set($name, $value);
  70. } catch (CException $e) {
  71. $setter = 'set' . $name;
  72. if (method_exists($this->message, $setter))
  73. $this->message->$setter($value);
  74. else
  75. throw $e;
  76. }
  77. }
  78. /**
  79. * Any requests to set or get attributes or call methods on this class that
  80. * are not found are redirected to the {@link Swift_Mime_Message} object.
  81. * @param string the method name
  82. */
  83. public function __call($name, $parameters) {
  84. try {
  85. return parent::__call($name, $parameters);
  86. } catch (CException $e) {
  87. if (method_exists($this->message, $name))
  88. return call_user_func_array(array($this->message, $name), $parameters);
  89. else
  90. throw $e;
  91. }
  92. }
  93. /**
  94. * You may optionally set some message info using the paramaters of this
  95. * constructor.
  96. * Use {@link view} and {@link setBody()} for more control.
  97. *
  98. * @param string $subject
  99. * @param string $body
  100. * @param string $contentType
  101. * @param string $charset
  102. * @return Swift_Mime_Message
  103. */
  104. public function __construct($subject = null, $body = null, $contentType = null, $charset = null) {
  105. Yii::app()->mail->registerScripts();
  106. $this->message = Swift_Message::newInstance($subject = null, $body = null, $contentType = null, $charset = null);
  107. }
  108. /**
  109. * Set the body of this entity, either as a string, or array of view
  110. * variables if a view is set, or as an instance of
  111. * {@link Swift_OutputByteStream}.
  112. *
  113. * @param mixed the body of the message. If a $this->view is set and this
  114. * is a string, this is passed to the view as $body. If $this->view is set
  115. * and this is an array, the array values are passed to the view like in the
  116. * controller render() method
  117. * @param string content type optional. For html, set to 'html/text'
  118. * @param string charset optional
  119. */
  120. public function setHtml($body) {
  121. if ($this->view !== null) {
  122. if (!is_array($body))
  123. $body = array('body' => $body);
  124. // create a dummy controller to render the view (needed in the console app)
  125. $controller = new CController('YiiMail');
  126. // renderPartial won't work with CConsoleApplication, so use
  127. // renderInternal - this requires that we use an actual path to the
  128. // view rather than the usual alias
  129. $viewPath = Yii::getPathOfAlias(Yii::app()->mail->viewPath . '.' . $this->view) . '.php';
  130. $body = $controller->renderInternal($viewPath, array_merge($body, array('mail' => $this)), true);
  131. }
  132. return $this->message->addPart($body, 'text/html');
  133. }
  134. public function setText($body) {
  135. return $this->message->setBody($body, 'text/plain');
  136. }
  137. }