|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElfSundae\Multimail; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Mail\Events\MessageSending; |
|
7
|
|
|
use Illuminate\Mail\Mailer as BaseMailer; |
|
8
|
|
|
use Swift_Mailer; |
|
9
|
|
|
|
|
10
|
|
|
class Mailer extends BaseMailer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The Swift Mailer Manager instance. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \ElfSundae\Multimail\SwiftMailerManager |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $swiftManager; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The registered handler of sending message. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Closure|string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $sendingMessageHandler; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the Swift Mailer Manager instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @return \ElfSundae\Multimail\SwiftMailerManager |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function getSwiftMailerManager() |
|
32
|
|
|
{ |
|
33
|
6 |
|
return $this->swiftManager; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the Swift Mailer Manager instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \ElfSundae\Multimail\SwiftMailerManager $manager |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
8 |
|
public function setSwiftMailerManager(SwiftMailerManager $manager) |
|
43
|
|
|
{ |
|
44
|
8 |
|
$this->swiftManager = $manager; |
|
45
|
|
|
|
|
46
|
8 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Register handler of sending message. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Closure|string $handler |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function registerSendingMessageHandler($handler) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->sendingMessageHandler = $handler; |
|
58
|
|
|
|
|
59
|
2 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Call the registered handler of sending message. |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed ...$args |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
3 |
|
protected function callSendingMessageHandler(...$args) |
|
69
|
|
|
{ |
|
70
|
3 |
|
if ($this->sendingMessageHandler instanceof Closure) { |
|
71
|
2 |
|
return $this->container->call($this->sendingMessageHandler, $args); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
if (is_string($this->sendingMessageHandler)) { |
|
75
|
|
|
return $this->container->call($this->sendingMessageHandler, $args, 'sendingMail'); |
|
76
|
|
|
} |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get a Swift Mailer instance for the given message. |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $message |
|
83
|
|
|
* @return \Swift_Mailer |
|
84
|
|
|
*/ |
|
85
|
3 |
|
protected function getSwiftMailerForMessage($message) |
|
86
|
|
|
{ |
|
87
|
3 |
|
$driver = $this->callSendingMessageHandler($message, $this); |
|
88
|
|
|
|
|
89
|
3 |
|
if ($driver instanceof Swift_Mailer) { |
|
90
|
|
|
return $driver; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return $this->getSwiftMailer($driver); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Send a Swift Message instance. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Swift_Message $message |
|
100
|
|
|
*/ |
|
101
|
3 |
|
protected function sendSwiftMessage($message) |
|
102
|
|
|
{ |
|
103
|
3 |
|
if ($this->events) { |
|
104
|
|
|
$this->events->fire(new MessageSending($message)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
$swift = $this->getSwiftMailerForMessage($message); |
|
108
|
|
|
|
|
109
|
|
|
try { |
|
110
|
3 |
|
return $swift->send($message, $this->failedRecipients); |
|
111
|
|
|
} finally { |
|
112
|
3 |
|
$swift->getTransport()->stop(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the Swift Mailer instance. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string|null $driver |
|
120
|
|
|
* @return \Swift_Mailer |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function getSwiftMailer($driver = null) |
|
123
|
|
|
{ |
|
124
|
4 |
|
return $this->swiftManager->mailer($driver); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Set the Swift Mailer instance. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string|\Swift_Mailer $swift |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function setSwiftMailer($swift) |
|
134
|
|
|
{ |
|
135
|
2 |
|
$this->swiftManager->setDefaultMailer($swift); |
|
136
|
|
|
|
|
137
|
2 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the mail driver. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $driver |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function mailDriver($driver) |
|
147
|
|
|
{ |
|
148
|
1 |
|
$this->swiftManager->setDefaultDriver($driver); |
|
149
|
|
|
|
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|