Issues (14)

src/channels/MailChannel.php (1 issue)

1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
namespace tuyakhov\notifications\channels;
6
7
8
use tuyakhov\notifications\messages\MailMessage;
9
use tuyakhov\notifications\NotifiableInterface;
10
use tuyakhov\notifications\NotificationInterface;
11
use yii\base\Component;
12
use yii\di\Instance;
13
use yii\mail\MailerInterface;
14
15
class MailChannel extends Component implements ChannelInterface
16
{
17
    /**
18
     * @var $mailer MailerInterface|array|string the mailer object or the application component ID of the mailer object.
0 ignored issues
show
Documentation Bug introduced by
The doc comment $mailer at position 0 could not be parsed: Unknown type name '$mailer' at position 0 in $mailer.
Loading history...
19
     */
20
    public $mailer = 'mailer';
21
22
    /**
23
     * The message sender.
24
     * @var string
25
     */
26
    public $from;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function init()
32
    {
33
        parent::init();
34
        $this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
35
    }
36
    
37
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
38
    {
39
        /**
40
         * @var $message MailMessage
41
         */
42
        $message = $notification->exportFor('mail');
43
        return $this->mailer->compose($message->view, $message->viewData)
44
            ->setFrom(isset($message->from) ? $message->from : $this->from)
45
            ->setTo($recipient->routeNotificationFor('mail'))
46
            ->setSubject($message->subject)
47
            ->send();
48
    }
49
}
50