1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Multimail; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Mail\Mailable as MailableContract; |
7
|
|
|
use Illuminate\Mail\Events\MessageSending; |
8
|
|
|
use Illuminate\Mail\Mailer as BaseMailer; |
9
|
|
|
use Swift_Mailer; |
10
|
|
|
|
11
|
|
|
class Mailer extends BaseMailer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The Swift Mailer Manager instance. |
15
|
|
|
* |
16
|
|
|
* @var \ElfSundae\Multimail\SwiftMailerManager |
17
|
|
|
*/ |
18
|
|
|
protected $swiftManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The registered handler of sending message. |
22
|
|
|
* |
23
|
|
|
* @var \Closure|string |
24
|
|
|
*/ |
25
|
|
|
protected $sendingMessageHandler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the Swift Mailer Manager instance. |
29
|
|
|
* |
30
|
|
|
* @return \ElfSundae\Multimail\SwiftMailerManager |
31
|
|
|
*/ |
32
|
5 |
|
public function getSwiftMailerManager() |
33
|
|
|
{ |
34
|
5 |
|
return $this->swiftManager; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the Swift Mailer Manager instance. |
39
|
|
|
* |
40
|
|
|
* @param \ElfSundae\Multimail\SwiftMailerManager $manager |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
7 |
|
public function setSwiftMailerManager(SwiftMailerManager $manager) |
44
|
|
|
{ |
45
|
7 |
|
$this->swiftManager = $manager; |
46
|
|
|
|
47
|
7 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Register handler of sending message. |
52
|
|
|
* |
53
|
|
|
* @param \Closure|string $handler |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
2 |
|
public function registerSendingMessageHandler($handler) |
57
|
|
|
{ |
58
|
2 |
|
$this->sendingMessageHandler = $handler; |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Call the registered handler of sending message. |
65
|
|
|
* |
66
|
|
|
* @param mixed ...$args |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
3 |
|
protected function callSendingMessageHandler(...$args) |
70
|
|
|
{ |
71
|
3 |
|
if ($this->sendingMessageHandler instanceof Closure) { |
72
|
2 |
|
return $this->container->call($this->sendingMessageHandler, $args); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if (is_string($this->sendingMessageHandler)) { |
76
|
|
|
return $this->container->call($this->sendingMessageHandler, $args, 'sendingMessage'); |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get a Swift Mailer instance for the given message. |
82
|
|
|
* |
83
|
|
|
* @param mixed $message |
84
|
|
|
* @return \Swift_Mailer |
85
|
|
|
*/ |
86
|
3 |
|
protected function getSwiftMailerForMessage($message) |
87
|
|
|
{ |
88
|
3 |
|
$swift = $this->callSendingMessageHandler($message, $this); |
89
|
|
|
|
90
|
3 |
|
if ($swift instanceof Swift_Mailer) { |
91
|
|
|
return $swift; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
return $this->swiftManager->mailer($swift); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Send a new message using a view. |
99
|
|
|
* |
100
|
|
|
* @param string|array $view |
101
|
|
|
* @param array $data |
102
|
|
|
* @param \Closure|string $callback |
103
|
|
|
*/ |
104
|
3 |
|
public function send($view, array $data = [], $callback = null) |
105
|
|
|
{ |
106
|
3 |
|
if ($view instanceof MailableContract) { |
107
|
|
|
return $view->send($this); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// First we need to parse the view, which could either be a string or an array |
111
|
|
|
// containing both an HTML and plain text versions of the view which should |
112
|
|
|
// be used when sending an e-mail. We will extract both of them out here. |
113
|
3 |
|
list($view, $plain, $raw) = $this->parseView($view); |
114
|
|
|
|
115
|
3 |
|
$data['message'] = $message = $this->createMessage(); |
116
|
|
|
|
117
|
|
|
// Once we have retrieved the view content for the e-mail we will set the body |
118
|
|
|
// of this message using the HTML type, which will provide a simple wrapper |
119
|
|
|
// to creating view based emails that are able to receive arrays of data. |
120
|
3 |
|
$this->addContent($message, $view, $plain, $raw, $data); |
121
|
|
|
|
122
|
3 |
|
$this->callMessageBuilder($callback, $message); |
123
|
|
|
|
124
|
3 |
|
if (isset($this->to['address'])) { |
125
|
|
|
$message->to($this->to['address'], $this->to['name'], true); |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
$swift = $this->getSwiftMailerForMessage($message); |
129
|
|
|
|
130
|
3 |
|
$message = $message->getSwiftMessage(); |
131
|
|
|
|
132
|
3 |
|
$this->sendSwiftMessage($message, $swift); |
133
|
3 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Send a Swift Message instance. |
137
|
|
|
* |
138
|
|
|
* @param \Swift_Message $message |
139
|
|
|
* @param \Swift_Mailer $swift |
140
|
|
|
*/ |
141
|
3 |
|
protected function sendSwiftMessage($message, $swift = null) |
142
|
|
|
{ |
143
|
3 |
|
if ($this->events) { |
144
|
|
|
$this->events->fire(new MessageSending($message)); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
if (is_null($swift)) { |
148
|
|
|
$swift = $this->getSwiftMailer(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
try { |
152
|
3 |
|
return $swift->send($message, $this->failedRecipients); |
153
|
|
|
} finally { |
154
|
3 |
|
$this->forceReconnection($swift); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Force the transport to re-connect. |
160
|
|
|
* |
161
|
|
|
* This will prevent errors in daemon queue situations. |
162
|
|
|
* |
163
|
|
|
* @param \Swift_Mailer $swift |
164
|
|
|
*/ |
165
|
3 |
|
protected function forceReconnection($swift = null) |
166
|
|
|
{ |
167
|
3 |
|
if (is_null($swift)) { |
168
|
|
|
$swift = $this->getSwiftMailer(); |
169
|
|
|
} |
170
|
|
|
|
171
|
3 |
|
$swift->getTransport()->stop(); |
172
|
3 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the Swift Mailer instance. |
176
|
|
|
* |
177
|
|
|
* @param string|null $driver |
178
|
|
|
* @return \Swift_Mailer |
179
|
|
|
*/ |
180
|
1 |
|
public function getSwiftMailer($driver = null) |
181
|
|
|
{ |
182
|
1 |
|
return $this->swiftManager->mailer($driver); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the Swift Mailer instance. |
187
|
|
|
* |
188
|
|
|
* @param string|\Swift_Mailer $swift |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
2 |
|
public function setSwiftMailer($swift) |
192
|
|
|
{ |
193
|
2 |
|
$this->swiftManager->setDefaultMailer($swift); |
194
|
|
|
|
195
|
2 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the mail driver. |
200
|
|
|
* |
201
|
|
|
* @param string $driver |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function mailDriver($driver) |
205
|
|
|
{ |
206
|
|
|
$this->swiftManager->setDefaultDriver($driver); |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|