|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
-
-
- namespace yii\mail;
-
- use Yii;
- use yii\base\Component;
- use yii\base\InvalidConfigException;
- use yii\base\ViewContextInterface;
- use yii\web\View;
-
-
- abstract class BaseMailer extends Component implements MailerInterface, ViewContextInterface
- {
-
-
- const EVENT_BEFORE_SEND = 'beforeSend';
-
-
- const EVENT_AFTER_SEND = 'afterSend';
-
-
-
- public $htmlLayout = 'layouts/html';
-
-
- public $textLayout = 'layouts/text';
-
-
- public $messageConfig = [];
-
-
- public $messageClass = 'yii\mail\BaseMessage';
-
-
- public $useFileTransport = false;
-
-
- public $fileTransportPath = '@runtime/mail';
-
-
- public $fileTransportCallback;
-
-
-
- private $_view = [];
-
-
- private $_viewPath;
-
-
-
-
- public function setView($view)
- {
- if (!is_array($view) && !is_object($view)) {
- throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or configuration array, "' . gettype($view) . '" given.');
- }
- $this->_view = $view;
- }
-
-
-
- public function getView()
- {
- if (!is_object($this->_view)) {
- $this->_view = $this->createView($this->_view);
- }
-
- return $this->_view;
- }
-
-
-
- protected function createView(array $config)
- {
- if (!array_key_exists('class', $config)) {
- $config['class'] = View::className();
- }
-
- return Yii::createObject($config);
- }
-
- private $_message;
-
-
-
- public function compose($view = null, array $params = [])
- {
- $message = $this->createMessage();
- if ($view === null) {
- return $message;
- }
-
- if (!array_key_exists('message', $params)) {
- $params['message'] = $message;
- }
-
- $this->_message = $message;
-
- if (is_array($view)) {
- if (isset($view['html'])) {
- $html = $this->render($view['html'], $params, $this->htmlLayout);
- }
- if (isset($view['text'])) {
- $text = $this->render($view['text'], $params, $this->textLayout);
- }
- } else {
- $html = $this->render($view, $params, $this->htmlLayout);
- }
-
-
- $this->_message = null;
-
- if (isset($html)) {
- $message->setHtmlBody($html);
- }
- if (isset($text)) {
- $message->setTextBody($text);
- } elseif (isset($html)) {
- if (preg_match('~<body[^>]*>(.*?)</body>~is', $html, $match)) {
- $html = $match[1];
- }
-
- $html = preg_replace('~<((style|script))[^>]*>(.*?)</\1>~is', '', $html);
-
- $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, Yii::$app ? Yii::$app->charset : 'UTF-8');
-
- $text = preg_replace("~^[ \t]+~m", '', trim($text));
- $text = preg_replace('~\R\R+~mu', "\n\n", $text);
- $message->setTextBody($text);
- }
- return $message;
- }
-
-
-
- protected function createMessage()
- {
- $config = $this->messageConfig;
- if (!array_key_exists('class', $config)) {
- $config['class'] = $this->messageClass;
- }
- $config['mailer'] = $this;
- return Yii::createObject($config);
- }
-
-
-
- public function send($message)
- {
- if (!$this->beforeSend($message)) {
- return false;
- }
-
- $address = $message->getTo();
- if (is_array($address)) {
- $address = implode(', ', array_keys($address));
- }
- Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
-
- if ($this->useFileTransport) {
- $isSuccessful = $this->saveMessage($message);
- } else {
- $isSuccessful = $this->sendMessage($message);
- }
- $this->afterSend($message, $isSuccessful);
-
- return $isSuccessful;
- }
-
-
-
- public function sendMultiple(array $messages)
- {
- $successCount = 0;
- foreach ($messages as $message) {
- if ($this->send($message)) {
- $successCount++;
- }
- }
-
- return $successCount;
- }
-
-
-
- public function render($view, $params = [], $layout = false)
- {
- $output = $this->getView()->render($view, $params, $this);
- if ($layout !== false) {
- return $this->getView()->render($layout, ['content' => $output, 'message' => $this->_message], $this);
- } else {
- return $output;
- }
- }
-
-
-
- abstract protected function sendMessage($message);
-
-
-
- protected function saveMessage($message)
- {
- $path = Yii::getAlias($this->fileTransportPath);
- if (!is_dir($path)) {
- mkdir($path, 0777, true);
- }
- if ($this->fileTransportCallback !== null) {
- $file = $path . '/' . call_user_func($this->fileTransportCallback, $this, $message);
- } else {
- $file = $path . '/' . $this->generateMessageFileName();
- }
- file_put_contents($file, $message->toString());
-
- return true;
- }
-
-
-
- public function generateMessageFileName()
- {
- $time = microtime(true);
-
- return date('Ymd-His-', $time) . sprintf('%04d', (int) (($time - (int) $time) * 10000)) . '-' . sprintf('%04d', mt_rand(0, 10000)) . '.eml';
- }
-
-
-
- public function getViewPath()
- {
- if ($this->_viewPath === null) {
- $this->setViewPath('@app/mail');
- }
- return $this->_viewPath;
- }
-
-
-
- public function setViewPath($path)
- {
- $this->_viewPath = Yii::getAlias($path);
- }
-
-
-
- public function beforeSend($message)
- {
- $event = new MailEvent(['message' => $message]);
- $this->trigger(self::EVENT_BEFORE_SEND, $event);
-
- return $event->isValid;
- }
-
-
-
- public function afterSend($message, $isSuccessful)
- {
- $event = new MailEvent(['message' => $message, 'isSuccessful' => $isSuccessful]);
- $this->trigger(self::EVENT_AFTER_SEND, $event);
- }
- }
|