|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Mailer\SwiftMailer; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Yiisoft\Mailer\BaseMailer; |
|
8
|
|
|
use Yiisoft\Mailer\Composer; |
|
9
|
|
|
use Yiisoft\Mailer\MessageFactoryInterface; |
|
10
|
|
|
use Yiisoft\Mailer\MessageInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Mailer implements a mailer based on SwiftMailer. |
|
14
|
|
|
* |
|
15
|
|
|
* @see http://swiftmailer.org |
|
16
|
|
|
*/ |
|
17
|
|
|
class Mailer extends BaseMailer |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Swift_Mailer Swift mailer instance. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $swiftMailer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns transport instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Swift_Transport |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function getTransport(): \Swift_Transport |
|
30
|
|
|
{ |
|
31
|
3 |
|
return $this->swiftMailer->getTransport(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param MessageFactoryInterface $messageFactory |
|
36
|
|
|
* @param Composer $composer |
|
37
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
38
|
|
|
* @param LoggerInterface $logger |
|
39
|
|
|
* @param \Swift_Transport $transport |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public function __construct( |
|
42
|
|
|
MessageFactoryInterface $messageFactory, |
|
43
|
|
|
Composer $composer, |
|
44
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
45
|
|
|
LoggerInterface $logger, |
|
46
|
|
|
\Swift_Transport $transport |
|
47
|
|
|
) { |
|
48
|
4 |
|
parent::__construct($messageFactory, $composer, $eventDispatcher, $logger); |
|
49
|
4 |
|
$this->swiftMailer = new \Swift_Mailer($transport); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
protected function sendMessage(MessageInterface $message): void |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var Message $message */ |
|
55
|
1 |
|
$sent = $this->swiftMailer->send($message->getSwiftMessage()); |
|
56
|
1 |
|
if ($sent === 0) { |
|
57
|
1 |
|
throw new \RuntimeException('Unable send message'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Registers plugins. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Swift_Events_EventListener[] $plugins |
|
65
|
|
|
* |
|
66
|
|
|
* @return self |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function registerPlugins(array $plugins): self |
|
69
|
|
|
{ |
|
70
|
2 |
|
foreach ($plugins as $plugin) { |
|
71
|
2 |
|
$this->registerPlugin($plugin); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Registers plugin. |
|
79
|
|
|
* |
|
80
|
|
|
* @see \Swift_Mailer::registerPlugins |
|
81
|
|
|
* |
|
82
|
|
|
* @return self |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function registerPlugin(\Swift_Events_EventListener $plugin): self |
|
85
|
|
|
{ |
|
86
|
2 |
|
$this->swiftMailer->registerPlugin($plugin); |
|
87
|
|
|
|
|
88
|
2 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|