1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Mail; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Nip\Config\Utils\PackageHasConfigTrait; |
7
|
|
|
use Nip\Mail\Transport\AbstractTransport; |
8
|
|
|
use Nip\Mail\Transport\SendgridTransport; |
9
|
|
|
use Swift_SmtpTransport as SmtpTransport; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TransportManager |
13
|
|
|
* @package Nip\Mail |
14
|
|
|
*/ |
15
|
|
|
class TransportManager |
16
|
|
|
{ |
17
|
|
|
use PackageHasConfigTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The array of resolved mailers. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $transports = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The registered custom driver creators. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $customCreators = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param null $name |
|
|
|
|
35
|
|
|
* @return AbstractTransport |
36
|
|
|
*/ |
37
|
3 |
|
public function transport($name = null) |
38
|
|
|
{ |
39
|
3 |
|
$name = $name ?: $this->getDefaultDriver(); |
|
|
|
|
40
|
|
|
|
41
|
3 |
|
return $this->transports[$name] = $this->get($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Attempt to get the transport from the local cache. |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @return AbstractTransport |
49
|
|
|
*/ |
50
|
3 |
|
protected function get($name) |
51
|
|
|
{ |
52
|
3 |
|
return $this->transports[$name] ?? $this->resolve($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* @return AbstractTransport |
58
|
|
|
*/ |
59
|
2 |
|
protected function resolve($name) |
60
|
|
|
{ |
61
|
2 |
|
$config = static::getPackageConfig('mailers.' . $name); |
62
|
|
|
|
63
|
2 |
|
if (is_null($config)) { |
64
|
|
|
throw new InvalidArgumentException("Mailer [{$name}] is not defined."); |
65
|
|
|
} |
66
|
2 |
|
$config = $config->toArray(); |
67
|
|
|
|
68
|
2 |
|
if (isset($this->customCreators[$name])) { |
69
|
|
|
return call_user_func($this->customCreators[$name], $config); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
if (trim($name) === '' || !method_exists($this, $method = 'create' . ucfirst($name) . 'Transport')) { |
73
|
|
|
throw new InvalidArgumentException("Unsupported mail transport [{$name}]."); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return $this->{$method}($config); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create an instance of the Mailgun Swift Transport driver. |
81
|
|
|
* |
82
|
|
|
* @param array $config |
83
|
|
|
* @return SendgridTransport |
84
|
|
|
*/ |
85
|
1 |
|
protected function createSendgridTransport(array $config) |
86
|
|
|
{ |
87
|
1 |
|
$transport = new SendgridTransport(); |
88
|
1 |
|
$transport->setApiKey($config['api_key']); |
89
|
|
|
|
90
|
1 |
|
return $transport; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create an instance of the SMTP Swift Transport driver. |
95
|
|
|
* |
96
|
|
|
* @param array $config |
97
|
|
|
* @return SmtpTransport |
98
|
|
|
*/ |
99
|
1 |
|
protected function createSmtpTransport(array $config) |
100
|
|
|
{// The Swift SMTP transport instance will allow us to use any SMTP backend |
101
|
|
|
// for delivering mail such as Sendgrid, Amazon SES, or a custom server |
102
|
|
|
// a developer has available. We will just pass this configured host. |
103
|
1 |
|
$transport = new SmtpTransport( |
104
|
1 |
|
$config['host'], |
105
|
1 |
|
$config['port'] |
106
|
|
|
); |
107
|
|
|
|
108
|
1 |
|
if (!empty($config['encryption'])) { |
109
|
1 |
|
$transport->setEncryption($config['encryption']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Once we have the transport we will check for the presence of a username |
113
|
|
|
// and password. If we have it we will set the credentials on the Swift |
114
|
|
|
// transporter instance so that we'll properly authenticate delivery. |
115
|
1 |
|
if (isset($config['username'])) { |
116
|
|
|
$transport->setUsername($config['username']); |
117
|
|
|
|
118
|
|
|
$transport->setPassword($config['password']); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $this->configureSmtpTransport($transport, $config); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Configure the additional SMTP driver options. |
126
|
|
|
* |
127
|
|
|
* @param \Swift_SmtpTransport $transport |
128
|
|
|
* @param array $config |
129
|
|
|
* @return \Swift_SmtpTransport |
130
|
|
|
*/ |
131
|
1 |
|
protected function configureSmtpTransport($transport, array $config) |
132
|
|
|
{ |
133
|
1 |
|
if (isset($config['stream'])) { |
134
|
|
|
$transport->setStreamOptions($config['stream']); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
if (isset($config['source_ip'])) { |
138
|
|
|
$transport->setSourceIp($config['source_ip']); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
if (isset($config['local_domain'])) { |
142
|
|
|
$transport->setLocalDomain($config['local_domain']); |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
if (isset($config['timeout'])) { |
146
|
|
|
$transport->setTimeout($config['timeout']); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
if (isset($config['auth_mode'])) { |
150
|
|
|
$transport->setAuthMode($config['auth_mode']); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return $transport; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the default mail driver name. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
1 |
|
public function getDefaultDriver() |
162
|
|
|
{ |
163
|
|
|
// Here we will check if the "driver" key exists and if it does we will use |
164
|
|
|
// that as the default driver in order to provide support for old styles |
165
|
|
|
// of the Laravel mail configuration file for backwards compatibility. |
166
|
1 |
|
return static::getPackageConfig('default', 'smtp'); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
2 |
|
protected static function getPackageConfigName() |
173
|
|
|
{ |
174
|
2 |
|
return 'mail'; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|