1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* @see https://github.com/zendframework/zend-mail for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Mail\Transport;
use Zend\Mail\Address;
use Zend\Mail\Headers;
use Zend\Mail\Message;
use Zend\Mail\Protocol;
use Zend\Mail\Protocol\Exception as ProtocolException;
use Zend\ServiceManager\ServiceManager;
/**
* SMTP connection object
*
* Loads an instance of Zend\Mail\Protocol\Smtp and forwards smtp transactions
*/
class Smtp implements TransportInterface
{
/**
* @var SmtpOptions
*/
protected $options;
/**
* @var Envelope|null
*/
protected $envelope;
/**
* @var Protocol\Smtp
*/
protected $connection;
/**
* @var bool
*/
protected $autoDisconnect = true;
/**
* @var Protocol\SmtpPluginManager
*/
protected $plugins;
/**
* When did we connect to the server?
*
* @var int|null
*/
protected $connectedTime;
/**
* Constructor.
*
* @param SmtpOptions $options Optional
*/
public function __construct(SmtpOptions $options = null)
{
if (! $options instanceof SmtpOptions) {
$options = new SmtpOptions();
}
$this->setOptions($options);
}
/**
* Set options
*
* @param SmtpOptions $options
* @return Smtp
*/
public function setOptions(SmtpOptions $options)
{
$this->options = $options;
return $this;
}
/**
* Get options
*
* @return SmtpOptions
*/
public function getOptions()
{
return $this->options;
}
/**
* Set options
*
* @param Envelope $envelope
*/
public function setEnvelope(Envelope $envelope)
{
$this->envelope = $envelope;
}
/**
* Get envelope
*
* @return Envelope|null
*/
public function getEnvelope()
{
return $this->envelope;
}
/**
* Set plugin manager for obtaining SMTP protocol connection
*
* @param Protocol\SmtpPluginManager $plugins
* @throws Exception\InvalidArgumentException
* @return Smtp
*/
public function setPluginManager(Protocol\SmtpPluginManager $plugins)
{
$this->plugins = $plugins;
return $this;
}
/**
* Get plugin manager for loading SMTP protocol connection
*
* @return Protocol\SmtpPluginManager
*/
public function getPluginManager()
{
if (null === $this->plugins) {
$this->setPluginManager(new Protocol\SmtpPluginManager(new ServiceManager()));
}
return $this->plugins;
}
/**
* Set the automatic disconnection when destruct
*
* @param bool $flag
* @return Smtp
*/
public function setAutoDisconnect($flag)
{
$this->autoDisconnect = (bool) $flag;
return $this;
}
/**
* Get the automatic disconnection value
*
* @return bool
*/
public function getAutoDisconnect()
{
return $this->autoDisconnect;
}
/**
* Return an SMTP connection
*
* @param string $name
* @param array|null $options
* @return Protocol\Smtp
*/
public function plugin($name, array $options = null)
{
return $this->getPluginManager()->get($name, $options);
}
/**
* Class destructor to ensure all open connections are closed
*/
public function __destruct()
{
if (! $this->getConnection() instanceof Protocol\Smtp) {
return;
}
try {
$this->getConnection()->quit();
} catch (ProtocolException\ExceptionInterface $e) {
// ignore
}
if ($this->autoDisconnect) {
$this->getConnection()->disconnect();
}
}
/**
* Sets the connection protocol instance
*
* @param Protocol\AbstractProtocol $connection
*/
public function setConnection(Protocol\AbstractProtocol $connection)
{
$this->connection = $connection;
if (($connection instanceof Protocol\Smtp)
&& ($this->getOptions()->getConnectionTimeLimit() !== null)
) {
$connection->setUseCompleteQuit(false);
}
}
/**
* Gets the connection protocol instance
*
* @return Protocol\Smtp
*/
public function getConnection()
{
$timeLimit = $this->getOptions()->getConnectionTimeLimit();
if ($timeLimit !== null
&& $this->connectedTime !== null
&& ((time() - $this->connectedTime) > $timeLimit)
) {
$this->connection = null;
}
return $this->connection;
}
/**
* Disconnect the connection protocol instance
*
* @return void
*/
public function disconnect()
{
if ($this->getConnection() instanceof Protocol\Smtp) {
$this->getConnection()->disconnect();
$this->connectedTime = null;
}
}
/**
* Send an email via the SMTP connection protocol
*
* The connection via the protocol adapter is made just-in-time to allow a
* developer to add a custom adapter if required before mail is sent.
*
* @param Message $message
* @throws Exception\RuntimeException
*/
public function send(Message $message)
{
// If sending multiple messages per session use existing adapter
$connection = $this->getConnection();
if (! ($connection instanceof Protocol\Smtp) || ! $connection->hasSession()) {
$connection = $this->connect();
} else {
// Reset connection to ensure reliable transaction
$connection->rset();
}
// Prepare message
$from = $this->prepareFromAddress($message);
$recipients = $this->prepareRecipients($message);
$headers = $this->prepareHeaders($message);
$body = $this->prepareBody($message);
if ((count($recipients) == 0) && (! empty($headers) || ! empty($body))) {
// Per RFC 2821 3.3 (page 18)
throw new Exception\RuntimeException(
sprintf(
'%s transport expects at least one recipient if the message has at least one header or body',
__CLASS__
)
);
}
// Set sender email address
$connection->mail($from);
// Set recipient forward paths
foreach ($recipients as $recipient) {
$connection->rcpt($recipient);
}
// Issue DATA command to client
$connection->data($headers . Headers::EOL . $body);
}
/**
* Retrieve email address for envelope FROM
*
* @param Message $message
* @throws Exception\RuntimeException
* @return string
*/
protected function prepareFromAddress(Message $message)
{
if ($this->getEnvelope() && $this->getEnvelope()->getFrom()) {
return $this->getEnvelope()->getFrom();
}
$sender = $message->getSender();
if ($sender instanceof Address\AddressInterface) {
return $sender->getEmail();
}
$from = $message->getFrom();
if (! count($from)) {
// Per RFC 2822 3.6
throw new Exception\RuntimeException(sprintf(
'%s transport expects either a Sender or at least one From address in the Message; none provided',
__CLASS__
));
}
$from->rewind();
$sender = $from->current();
return $sender->getEmail();
}
/**
* Prepare array of email address recipients
*
* @param Message $message
* @return array
*/
protected function prepareRecipients(Message $message)
{
if ($this->getEnvelope() && $this->getEnvelope()->getTo()) {
return (array) $this->getEnvelope()->getTo();
}
$recipients = [];
foreach ($message->getTo() as $address) {
$recipients[] = $address->getEmail();
}
foreach ($message->getCc() as $address) {
$recipients[] = $address->getEmail();
}
foreach ($message->getBcc() as $address) {
$recipients[] = $address->getEmail();
}
$recipients = array_unique($recipients);
return $recipients;
}
/**
* Prepare header string from message
*
* @param Message $message
* @return string
*/
protected function prepareHeaders(Message $message)
{
$headers = clone $message->getHeaders();
$headers->removeHeader('Bcc');
return $headers->toString();
}
/**
* Prepare body string from message
*
* @param Message $message
* @return string
*/
protected function prepareBody(Message $message)
{
return $message->getBodyText();
}
/**
* Lazy load the connection
*
* @return Protocol\Smtp
*/
protected function lazyLoadConnection()
{
// Check if authentication is required and determine required class
$options = $this->getOptions();
$config = $options->getConnectionConfig();
$config['host'] = $options->getHost();
$config['port'] = $options->getPort();
$this->setConnection($this->plugin($options->getConnectionClass(), $config));
return $this->connect();
}
/**
* Connect the connection, and pass it helo
*
* @return Protocol\Smtp
*/
protected function connect()
{
if (! $this->connection instanceof Protocol\Smtp) {
return $this->lazyLoadConnection();
}
$this->connection->connect();
$this->connectedTime = time();
$this->connection->helo($this->getOptions()->getName());
return $this->connection;
}
}