BaseMessage.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\mail;
  8. use yii\base\Object;
  9. use Yii;
  10. /**
  11. * BaseMessage serves as a base class that implements the [[send()]] method required by [[MessageInterface]].
  12. *
  13. * By default, [[send()]] will use the "mail" application component to send the current message.
  14. * The "mail" application component should be a mailer instance implementing [[MailerInterface]].
  15. *
  16. * @see BaseMailer
  17. *
  18. * @author Paul Klimov <[email protected]>
  19. * @since 2.0
  20. */
  21. abstract class BaseMessage extends Object implements MessageInterface
  22. {
  23. /**
  24. * @inheritdoc
  25. */
  26. public function send(MailerInterface $mailer = null)
  27. {
  28. if ($mailer === null) {
  29. $mailer = Yii::$app->getMail();
  30. }
  31. return $mailer->send($this);
  32. }
  33. /**
  34. * PHP magic method that returns the string representation of this object.
  35. * @return string the string representation of this object.
  36. */
  37. public function __toString()
  38. {
  39. // __toString cannot throw exception
  40. // use trigger_error to bypass this limitation
  41. try {
  42. return $this->toString();
  43. } catch (\Exception $e) {
  44. trigger_error($e->getMessage() . "\n\n" . $e->getTraceAsString());
  45. return '';
  46. }
  47. }
  48. }