1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHALCON-EXT package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhalconExt\Mail; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\View\Simple; |
|
|
|
|
15
|
|
|
use PhalconExt\Di\ProvidesDi; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A swift mailer tuned for phalcon. |
19
|
|
|
* |
20
|
|
|
* @author Jitendra Adhikari <[email protected]> |
21
|
|
|
* @license MIT |
22
|
|
|
* |
23
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
24
|
|
|
*/ |
25
|
|
|
class Mailer |
26
|
|
|
{ |
27
|
|
|
use ProvidesDi; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
protected $config = []; |
31
|
|
|
|
32
|
|
|
/** @var \Swift_Transport */ |
33
|
|
|
protected $transport; |
34
|
|
|
|
35
|
|
|
/** @var \Swift_Mailer */ |
36
|
|
|
protected $mailer; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
public $failed = []; |
40
|
|
|
|
41
|
|
|
public function __construct(array $config) |
42
|
|
|
{ |
43
|
|
|
if (!isset($config['driver'], $config['from']['email'], $config['from']['name'])) { |
44
|
|
|
throw new \InvalidArgumentException('The mailer config is invalid, missing driver &/or identity'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($config['driver'] === 'smtp' && !isset($config['host'], $config['port'])) { |
48
|
|
|
throw new \InvalidArgumentException('The mailer config is invalid, missing smtp host &/or port'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->config = $config; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create new raw mail. |
56
|
|
|
* |
57
|
|
|
* @return Mail |
58
|
|
|
*/ |
59
|
|
|
public function newMail(): Mail |
60
|
|
|
{ |
61
|
|
|
$from = $this->config['from']; |
62
|
|
|
|
63
|
|
|
return (new Mail($this))->setFrom($from['email'], $from['name']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create new mail from view template. |
68
|
|
|
* |
69
|
|
|
* @param string $viewFile |
70
|
|
|
* @param array $viewParams |
71
|
|
|
* @param string $type |
72
|
|
|
* |
73
|
|
|
* @return Mail |
74
|
|
|
*/ |
75
|
|
|
public function newTemplateMail(string $viewFile, array $viewParams = [], string $type = 'text/html'): Mail |
76
|
|
|
{ |
77
|
|
|
$dirName = \dirname($viewFile); |
78
|
|
|
$fileName = \basename($viewFile); |
79
|
|
|
$view = $this->di('view'); |
80
|
|
|
|
81
|
|
|
$markup = $view instanceof Simple |
82
|
|
|
? $view->render($viewFile, $viewParams) |
83
|
|
|
: $view->start()->setVars($viewParams)->render($dirName, $fileName)->finish()->getContent(); |
84
|
|
|
|
85
|
|
|
return $this->newMail()->setBody($markup, $type); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the swift mailer. |
90
|
|
|
* |
91
|
|
|
* @throws \InvalidArgumentException When configured driver not supported. |
92
|
|
|
* |
93
|
|
|
* @return \Swift_Mailer |
94
|
|
|
*/ |
95
|
|
|
public function getMailer(): \Swift_Mailer |
96
|
|
|
{ |
97
|
|
|
return $this->mailer ?? $this->mailer = new \Swift_Mailer($this->getTransport()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the swift transport. |
102
|
|
|
* |
103
|
|
|
* @throws \InvalidArgumentException When configured driver not supported. |
104
|
|
|
* |
105
|
|
|
* @return \Swift_Transport |
106
|
|
|
*/ |
107
|
|
|
public function getTransport(): \Swift_Transport |
108
|
|
|
{ |
109
|
|
|
if (!$this->transport) { |
110
|
|
|
$this->transport = $this->initTransport(); |
111
|
|
|
|
112
|
|
|
if (null !== $loggerConfig = $this->config['logger'] ?? null) { |
113
|
|
|
$this->transport->registerPlugin(new Logger($loggerConfig)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->transport; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Instantiate the swift transport. |
122
|
|
|
* |
123
|
|
|
* @throws \InvalidArgumentException When configured driver not supported. |
124
|
|
|
* |
125
|
|
|
* @return \Swift_Transport |
126
|
|
|
*/ |
127
|
|
|
protected function initTransport(): \Swift_Transport |
128
|
|
|
{ |
129
|
|
|
$config = $this->config; |
130
|
|
|
$driver = \strtolower($config['driver']); |
131
|
|
|
|
132
|
|
|
if ('null' === $driver) { |
133
|
|
|
return new \Swift_NullTransport; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ('sendmail' === $driver) { |
137
|
|
|
return new \Swift_SendmailTransport($config['sendmail'] ?? '/usr/sbin/sendmail -bs'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ('smtp' !== $driver) { |
141
|
|
|
throw new \InvalidArgumentException(sprintf('Mail driver "%s" not supported', $driver)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->createSmtpTransport($config); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create new Smtp Transport. |
149
|
|
|
* |
150
|
|
|
* @param array $config |
151
|
|
|
* |
152
|
|
|
* @return \Swift_SmtpTransport |
153
|
|
|
*/ |
154
|
|
|
protected function createSmtpTransport(array $config): \Swift_SmtpTransport |
155
|
|
|
{ |
156
|
|
|
$transport = (new \Swift_SmtpTransport)->setHost($config['host'])->setPort($config['port']); |
157
|
|
|
|
158
|
|
|
if ($config['encryption'] ?? null) { |
159
|
|
|
$transport->setEncryption($config['encryption']); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ($config['username'] ?? null) { |
163
|
|
|
$transport->setUsername($config['username'])->setPassword($config['password']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $transport; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Mail the mail with swift mailer. |
171
|
|
|
* |
172
|
|
|
* @return int The count of mailed recipients. |
173
|
|
|
*/ |
174
|
|
|
public function mail(Mail $mail) |
175
|
|
|
{ |
176
|
|
|
$this->failed = []; |
177
|
|
|
|
178
|
|
|
return $this->getMailer()->send($mail, $this->failed); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths