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 mail driver handler. |
22
|
|
|
* |
23
|
|
|
* @var \Closure|string |
24
|
|
|
*/ |
25
|
|
|
protected $mailDriverHandler; |
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 the mail driver handler. |
52
|
|
|
* |
53
|
|
|
* @param \Closure|string $handler |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
2 |
|
public function registerMailDriverHandler($handler) |
57
|
|
|
{ |
58
|
2 |
|
$this->mailDriverHandler = $handler; |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Call the registered mail driver handler. |
65
|
|
|
* |
66
|
|
|
* @param mixed ...$args |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
3 |
|
protected function callMailDriverHandler(...$args) |
70
|
|
|
{ |
71
|
3 |
|
if ($this->mailDriverHandler instanceof Closure) { |
72
|
2 |
|
return $this->container->call($this->mailDriverHandler, $args); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if (is_string($this->mailDriverHandler)) { |
76
|
|
|
return $this->container->call($this->mailDriverHandler, $args, 'mailDriver'); |
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->callMailDriverHandler($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
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
3 |
|
public function send($view, array $data = [], $callback = null) |
106
|
|
|
{ |
107
|
3 |
|
if ($view instanceof MailableContract) { |
108
|
|
|
return $view->send($this); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// First we need to parse the view, which could either be a string or an array |
112
|
|
|
// containing both an HTML and plain text versions of the view which should |
113
|
|
|
// be used when sending an e-mail. We will extract both of them out here. |
114
|
3 |
|
list($view, $plain, $raw) = $this->parseView($view); |
115
|
|
|
|
116
|
3 |
|
$data['message'] = $message = $this->createMessage(); |
117
|
|
|
|
118
|
|
|
// Once we have retrieved the view content for the e-mail we will set the body |
119
|
|
|
// of this message using the HTML type, which will provide a simple wrapper |
120
|
|
|
// to creating view based emails that are able to receive arrays of data. |
121
|
3 |
|
$this->addContent($message, $view, $plain, $raw, $data); |
122
|
|
|
|
123
|
3 |
|
$this->callMessageBuilder($callback, $message); |
|
|
|
|
124
|
|
|
|
125
|
3 |
|
if (isset($this->to['address'])) { |
126
|
|
|
$message->to($this->to['address'], $this->to['name'], true); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
$swift = $this->getSwiftMailerForMessage($message); |
130
|
|
|
|
131
|
3 |
|
$message = $message->getSwiftMessage(); |
132
|
|
|
|
133
|
3 |
|
$this->sendSwiftMessage($message, $swift); |
134
|
3 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Send a Swift Message instance. |
138
|
|
|
* |
139
|
|
|
* @param \Swift_Message $message |
140
|
|
|
* @param \Swift_Mailer $swift |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
3 |
|
protected function sendSwiftMessage($message, Swift_Mailer $swift = null) |
144
|
|
|
{ |
145
|
3 |
|
if ($this->events) { |
146
|
|
|
$this->events->fire(new MessageSending($message)); |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
if (is_null($swift)) { |
150
|
|
|
$swift = $this->getSwiftMailer(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
3 |
|
return $swift->send($message, $this->failedRecipients); |
155
|
|
|
} finally { |
156
|
3 |
|
$this->forceReconnection($swift); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Force the transport to re-connect. |
162
|
|
|
* |
163
|
|
|
* This will prevent errors in daemon queue situations. |
164
|
|
|
* |
165
|
|
|
* @param \Swift_Mailer $swiftMailer |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
3 |
|
protected function forceReconnection($swiftMailer = null) |
169
|
|
|
{ |
170
|
3 |
|
if (is_null($swiftMailer)) { |
171
|
|
|
$swiftMailer = $this->getSwiftMailer(); |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
$swiftMailer->getTransport()->stop(); |
175
|
3 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the Swift Mailer instance. |
179
|
|
|
* |
180
|
|
|
* @return \Swift_Mailer |
181
|
|
|
*/ |
182
|
1 |
|
public function getSwiftMailer() |
183
|
|
|
{ |
184
|
1 |
|
return $this->swiftManager->mailer(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the Swift Mailer instance. |
189
|
|
|
* |
190
|
|
|
* @param \Swift_Mailer $swift |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
2 |
|
public function setSwiftMailer($swift) |
194
|
|
|
{ |
195
|
2 |
|
$this->swiftManager->setDefaultMailer($swift); |
196
|
|
|
|
197
|
|
|
// Our $swift is managed by the SwiftMailerManager singleton, |
198
|
|
|
// so just let $this->swift go. |
199
|
2 |
|
} |
200
|
|
|
} |
201
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.