1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Service; |
13
|
|
|
|
14
|
|
|
use Da\User\Contracts\ServiceInterface; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\mail\BaseMailer; |
17
|
|
|
use yii\mail\MailerInterface; |
18
|
|
|
|
19
|
|
|
class MailService implements ServiceInterface |
20
|
|
|
{ |
21
|
|
|
protected $viewPath = '@Da/User/resources/views/mail'; |
22
|
|
|
|
23
|
|
|
protected $type; |
24
|
|
|
protected $from; |
25
|
|
|
protected $to; |
26
|
|
|
protected $subject; |
27
|
|
|
protected $view; |
28
|
|
|
protected $params = []; |
29
|
|
|
protected $mailer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* MailService constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $type the mailer type |
35
|
|
|
* @param string $from from email account |
36
|
|
|
* @param string $to to email account |
37
|
|
|
* @param string $subject the email subject |
38
|
|
|
* @param string $view the view to render mail |
39
|
|
|
* @param array $params view parameters |
40
|
|
|
* @param BaseMailer|MailerInterface $mailer mailer interface |
41
|
|
|
*/ |
42
|
10 |
|
public function __construct($type, $from, $to, $subject, $view, array $params, MailerInterface $mailer) |
43
|
|
|
{ |
44
|
10 |
|
$this->type = $type; |
45
|
10 |
|
$this->from = $from; |
46
|
10 |
|
$this->to = $to; |
47
|
10 |
|
$this->subject = $subject; |
48
|
10 |
|
$this->view = $view; |
49
|
10 |
|
$this->params = $params; |
50
|
10 |
|
$this->mailer = $mailer; |
51
|
10 |
|
$this->mailer->setViewPath($this->viewPath); |
52
|
10 |
|
$this->mailer->getView()->theme = Yii::$app->view->theme; |
53
|
10 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $name |
57
|
|
|
* @param $value |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
4 |
|
public function setViewParam($name, $value) |
62
|
|
|
{ |
63
|
4 |
|
$this->params[$name] = $value; |
64
|
|
|
|
65
|
4 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* gets mailer type |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
9 |
|
public function getType() |
73
|
|
|
{ |
74
|
9 |
|
return $this->type; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
10 |
|
public function run() |
81
|
|
|
{ |
82
|
10 |
|
return $this->mailer |
83
|
10 |
|
->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params) |
84
|
10 |
|
->setFrom($this->from) |
85
|
10 |
|
->setTo($this->to) |
86
|
10 |
|
->setSubject($this->subject) |
87
|
10 |
|
->send(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|