|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nip\Mail\Transport; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory; |
|
8
|
|
|
use Symfony\Component\Mailer\Transport\AbstractTransport; |
|
9
|
|
|
use Symfony\Component\Mailer\Transport\Dsn; |
|
10
|
|
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport as SmtpTransport; |
|
11
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class TransportManager. |
|
15
|
|
|
*/ |
|
16
|
|
|
class TransportFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* |
|
21
|
|
|
* @return AbstractTransport |
|
22
|
|
|
*/ |
|
23
|
|
|
public function fromConfig($config) |
|
24
|
|
|
{ |
|
25
|
|
|
$name = $config['transport']; |
|
26
|
|
|
|
|
27
|
|
|
if ('' === trim($name) || !method_exists($this, $method = 'create' . ucfirst($name) . 'Transport')) { |
|
28
|
|
|
throw new InvalidArgumentException("Unsupported mail transport [{$name}]."); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this->{$method}($config); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create an instance of the Mailgun Swift Transport driver. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function createSendgridTransport(array $config): SendgridRestTransport |
|
38
|
|
|
{ |
|
39
|
|
|
$transport = new SendgridRestTransport(); |
|
40
|
|
|
$transport->setApiKey($config['api_key']); |
|
41
|
|
|
|
|
42
|
|
|
return $transport; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create an instance of the Ses Transport driver. |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function createSesTransport(array $config): TransportInterface |
|
49
|
|
|
{ |
|
50
|
|
|
$dsn = new Dsn( |
|
51
|
|
|
$config['scheme'] ?? 'ses', |
|
52
|
|
|
'default', |
|
53
|
|
|
$config['key'] ?? null, |
|
54
|
|
|
$config['secret'] ?? null, |
|
55
|
|
|
null, |
|
56
|
|
|
[ |
|
57
|
|
|
'region' => $config['region'] ?? null, |
|
58
|
|
|
] |
|
59
|
|
|
); |
|
60
|
|
|
return (new SesTransportFactory())->create($dsn); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create an instance of the SMTP Swift Transport driver. |
|
65
|
|
|
* |
|
66
|
|
|
* @return SmtpTransport |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function createSmtpTransport(array $config) |
|
69
|
|
|
{// The Swift SMTP transport instance will allow us to use any SMTP backend |
|
70
|
|
|
// for delivering mail such as Sendgrid, Amazon SES, or a custom server |
|
71
|
|
|
// a developer has available. We will just pass this configured host. |
|
72
|
|
|
$transport = new SmtpTransport( |
|
73
|
|
|
$config['host'], |
|
74
|
|
|
$config['port'] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
// if (!empty($config['encryption'])) { |
|
78
|
|
|
// $transport->setEncryption($config['encryption']); |
|
79
|
|
|
// } |
|
80
|
|
|
|
|
81
|
|
|
// Once we have the transport we will check for the presence of a username |
|
82
|
|
|
// and password. If we have it we will set the credentials on the Swift |
|
83
|
|
|
// transporter instance so that we'll properly authenticate delivery. |
|
84
|
|
|
if (isset($config['username'])) { |
|
85
|
|
|
$transport->setUsername($config['username']); |
|
86
|
|
|
|
|
87
|
|
|
$transport->setPassword($config['password']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->configureSmtpTransport($transport, $config); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Configure the additional SMTP driver options. |
|
95
|
|
|
* |
|
96
|
|
|
* @param SmtpTransport $transport |
|
97
|
|
|
* |
|
98
|
|
|
* @return SmtpTransport |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function configureSmtpTransport($transport, array $config) |
|
101
|
|
|
{ |
|
102
|
|
|
if (isset($config['stream'])) { |
|
103
|
|
|
$transport->setStreamOptions($config['stream']); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (isset($config['source_ip'])) { |
|
107
|
|
|
$transport->setSourceIp($config['source_ip']); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (isset($config['local_domain'])) { |
|
111
|
|
|
$transport->setLocalDomain($config['local_domain']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (isset($config['timeout'])) { |
|
115
|
|
|
$transport->setTimeout($config['timeout']); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (isset($config['auth_mode'])) { |
|
119
|
|
|
$transport->setAuthMode($config['auth_mode']); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $transport; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.