<?php
/**
 * Created by PhpStorm.
 * User: andri
 * Date: 26.10.17
 * Time: 12:56
 */

namespace app\components;

use dektrium\user\Mailer;
use Yii;


class DektriumMailer extends Mailer
{

    /**
     * @param string $to
     * @param string $subject
     * @param string $view
     * @param array  $params
     *
     * @return bool
     */
    public function sendMessage($to, $subject, $view, $params = [])
    {
        /** @var \yii\swiftmailer\Mailer $mailer **/
        $mailer = $this->mailerComponent === null ? Yii::$app->mailer : Yii::$app->get($this->mailerComponent);
        $mailer->viewPath = $this->viewPath;
        $mailer->getView()->theme = Yii::$app->view->theme;

        if ($this->sender === null) {
            $this->sender = isset(Yii::$app->params['adminEmail']) ?
                Yii::$app->params['adminEmail']
                : 'no-reply@example.com';
        }

        return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params)
            ->setTo($to)
            ->setFrom($this->sender)
            ->setSubject($subject)
            ->send();
    }

}