1
|
|
|
<?php declare(strict_types=1);
|
2
|
|
|
|
3
|
|
|
namespace yrc\jobs\notifications\email;
|
4
|
|
|
|
5
|
|
|
use RPQ\Server\AbstractJob;
|
6
|
|
|
use yii\mail\MessageInterface;
|
7
|
|
|
use Yii;
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* @abstract AbstractEmailNotification
|
11
|
|
|
* All email notifications should extend this abstract class to inherit basic queue based email sending capabilities
|
12
|
|
|
* @property array $origin
|
13
|
|
|
* @property string $subject
|
14
|
|
|
* @property string $viewFile
|
15
|
|
|
*/
|
16
|
|
|
abstract class AbstractEmailNotification extends AbstractJob
|
17
|
|
|
{
|
18
|
|
|
/**
|
19
|
|
|
* The sender email address and name as an array.
|
20
|
|
|
* @var array
|
21
|
|
|
*/
|
22
|
|
|
protected $origin = [
|
23
|
|
|
'[email protected]' => 'No Reply'
|
24
|
|
|
];
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* The email subject. This should be explicitly declared in extensions
|
28
|
|
|
* @var string
|
29
|
|
|
*/
|
30
|
|
|
protected $subject;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* The name of the viewfile to use within @app/views/email/(html|text)
|
34
|
|
|
* @var string
|
35
|
|
|
*/
|
36
|
|
|
protected $viewFile;
|
37
|
|
|
|
38
|
|
|
abstract protected function getSubject();
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Generates a message composition and returns a MessageInterface
|
42
|
|
|
* @param string|array $to
|
43
|
|
|
* @param array $args
|
44
|
|
|
* @return yii\mail\MessageInterface
|
45
|
|
|
*/
|
46
|
|
|
protected function compose($to, array $args = []) : MessageInterface
|
47
|
|
|
{
|
48
|
|
|
// Grab the \yii\console\Controller instance, and setup the view paths
|
49
|
|
|
// Yii::$app->mailer->compose() can't handle .twig extension, so the render/renderPartial methods need to be called manually
|
50
|
|
|
$controller = Yii::$app->controller;
|
51
|
|
|
$controller->setViewPath(Yii::$app->mailer->viewPath);
|
52
|
|
|
|
53
|
|
|
// Compose a message, and return a MessageInterface if the consumer needs to modify the message interface in any way.
|
54
|
|
|
$composition = Yii::$app->mailer->compose()
|
55
|
|
|
->setFrom($args['origin'] ?? $this->origin)
|
56
|
|
|
->setSubject(Yii::t('app', $this->getSubject())) // The subject should be translated, if possible
|
57
|
|
|
->setTo($to);
|
58
|
|
|
|
59
|
|
|
$controller->layout = Yii::$app->mailer->htmlLayout;
|
60
|
|
|
$composition->setHtmlBody($controller->render("html/{$this->viewFile}", $args));
|
61
|
|
|
|
62
|
|
|
$controller->layout = Yii::$app->mailer->textLayout;
|
63
|
|
|
$composition->setTextBody($controller->render("text/{$this->viewFile}", $args));
|
64
|
|
|
|
65
|
|
|
return $composition;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Implementation of RPQ\AbstractJob\Perform to send the actual email
|
70
|
|
|
* @param array $args
|
71
|
|
|
* @return int
|
72
|
|
|
*/
|
73
|
|
|
public function perform(array $args = []) : int
|
74
|
|
|
{
|
75
|
|
|
$message = $this->compose($args['email'], $args);
|
76
|
|
|
|
77
|
|
|
if ($message->send()) {
|
78
|
|
|
return 0;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
return 1;
|
82
|
|
|
}
|
83
|
|
|
} |