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