BaseMessage.php 1.36 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Paul Klimov committed
8
namespace yii\mail;
9 10 11 12 13

use yii\base\Object;
use Yii;

/**
Qiang Xue committed
14 15 16 17
 * BaseMessage serves as a base class that implements the [[send()]] method required by [[MessageInterface]].
 *
 * By default, [[send()]] will use the "mail" application component to send the current message.
 * The "mail" application component should be a mailer instance implementing [[MailerInterface]].
18 19 20
 *
 * @see BaseMailer
 *
21
 * @property MailerInterface $mailer The mailer component. This property is read-only.
22 23 24 25
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
26
abstract class BaseMessage extends Object implements MessageInterface
27 28
{
	/**
Qiang Xue committed
29
	 * @return MailerInterface the mailer component
30 31 32
	 */
	public function getMailer()
	{
33
		return Yii::$app->getComponent('mail');
34 35 36
	}

	/**
37
	 * @inheritdoc
38 39 40 41 42 43
	 */
	public function send()
	{
		return $this->getMailer()->send($this);
	}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	/**
	 * PHP magic method that returns the string representation of this object.
	 * @return string the string representation of this object.
	 */
	public function __toString()
	{
		// __toString cannot throw exception
		// use trigger_error to bypass this limitation
		try {
			return $this->toString();
		} catch (\Exception $e) {
			trigger_error($e->getMessage());
			return '';
		}
	}
59
}