-
Notifications
You must be signed in to change notification settings - Fork 25
/
Mailer.php
78 lines (65 loc) · 2.07 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @author akiraz@bk.ru
* @link https://github.com/akiraz2/yii2-ticket-support
* @copyright 2018 akiraz2
* @license MIT
*/
namespace akiraz2\support;
use PhpImap\Mailbox;
use Yii;
use yii\base\Component;
/**
* Mailer.
*/
class Mailer extends Component
{
/** @var string */
public $viewPath = '@vendor/akiraz2/yii2-ticket-support/mail';
/** @var string|array Default: `Yii::$app->params['adminEmail']` OR `no-reply@example.com` */
public $sender;
/** @var string|array Default: `Yii::$app->params['adminEmail']` */
public $toEmail;
/** @var \yii\mail\BaseMailer Default: `Yii::$app->mailer` */
public $mailerComponent;
/** @var \akiraz2\support\Module */
protected $module;
protected $_mailer;
/** @inheritdoc */
public function init()
{
$this->module = Yii::$app->getModule('support');
$this->_mailer = $this->mailerComponent === null ? Yii::$app->mailer : Yii::$app->get($this->mailerComponent);
parent::init();
}
public function sendMessageToSupportEmail($subject, $view, $params = [])
{
if ($this->toEmail === null) {
$this->toEmail = isset(Yii::$app->params['adminEmail']) ? Yii::$app->params['adminEmail'] : 'no-reply@example.com';
}
return $this->sendMessage($this->toEmail, $subject, $view, $params);
}
/**
* @param string $to
* @param string $subject
* @param string $view
* @param array $params
*
* @return bool
*/
public function sendMessage($to, $subject, $view, $params = [])
{
$mailer = $this->_mailer;
$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(['text' => $view . '-text'],
$params)
->setTo($to)
->setFrom($this->sender)
->setSubject($subject)
->send();
}
}