Passed
Push — master ( efdecb...0935cc )
by zyt
07:57
created

Validator::setBindAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace SMTPValidateEmail;
4
5
use SMTPValidateEmail\Exceptions\Exception;
6
use SMTPValidateEmail\Exceptions\Timeout as TimeoutException;
7
use SMTPValidateEmail\Exceptions\NoTimeout as NoTimeoutException;
8
use SMTPValidateEmail\Exceptions\NoConnection as NoConnectionException;
9
use SMTPValidateEmail\Exceptions\UnexpectedResponse as UnexpectedResponseException;
10
use SMTPValidateEmail\Exceptions\NoHelo as NoHeloException;
11
use SMTPValidateEmail\Exceptions\NoMailFrom as NoMailFromException;
12
use SMTPValidateEmail\Exceptions\NoResponse as NoResponseException;
13
use SMTPValidateEmail\Exceptions\SendFailed as SendFailedException;
14
15
class Validator
16
{
17
18
    public $log = [];
19
20
    /**
21
     * Print stuff as it happens or not
22
     *
23
     * @var bool
24
     */
25
    public $debug = false;
26
27
    /**
28
     * Default smtp port to connect to
29
     *
30
     * @var int
31
     */
32
    public $connect_port = 25;
33
34
    /**
35
     * Are "catch-all" accounts considered valid or not?
36
     * If not, the class checks for a "catch-all" and if it determines the box
37
     * has a "catch-all", sets all the emails on that domain as invalid.
38
     *
39
     * @var bool
40
     */
41
    public $catchall_is_valid = true;
42
43
    /**
44
     * Whether to perform the "catch-all" test or not
45
     *
46
     * @var bool
47
     */
48
    public $catchall_test = false; // Set to true to perform a catchall test
49
50
    /**
51
     * Being unable to communicate with the remote MTA could mean an address
52
     * is invalid, but it might not, depending on your use case, set the
53
     * value appropriately.
54
     *
55
     * @var bool
56
     */
57
    public $no_comm_is_valid = false;
58
59
    /**
60
     * Being unable to connect with the remote host could mean a server
61
     * configuration issue, but it might not, depending on your use case,
62
     * set the value appropriately.
63
     */
64
    public $no_conn_is_valid = false;
65
66
    /**
67
     * Whether "greylisted" responses are considered as valid or invalid addresses
68
     *
69
     * @var bool
70
     */
71
    public $greylisted_considered_valid = true;
72
73
    /**
74
     * Stream context arguments for connection socket, necessary to initiate
75
     * Server IP (in case reverse IP), see: https://stackoverflow.com/a/8968016
76
     */
77
    public $stream_context_args = [];
78
79
    /**
80
     * Timeout values for various commands (in seconds) per RFC 2821
81
     *
82
     * @var array
83
     */
84
    protected $command_timeouts = [
85
        'ehlo' => 120,
86
        'helo' => 120,
87
        'tls'  => 180, // start tls
88
        'mail' => 300, // mail from
89
        'rcpt' => 300, // rcpt to,
90
        'rset' => 30,
91
        'quit' => 60,
92
        'noop' => 60
93
    ];
94
95
    /**
96
     * Whether NOOP commands are sent at all.
97
     *
98
     * @var bool
99
     */
100
    protected $send_noops = true;
101
102
    public const CRLF = "\r\n";
103
104
    // Some smtp response codes
105
    public const SMTP_CONNECT_SUCCESS = 220;
106
    public const SMTP_QUIT_SUCCESS    = 221;
107
    public const SMTP_GENERIC_SUCCESS = 250;
108
    public const SMTP_USER_NOT_LOCAL  = 251;
109
    public const SMTP_CANNOT_VRFY     = 252;
110
111
    public const SMTP_SERVICE_UNAVAILABLE = 421;
112
113
    // 450 Requested mail action not taken: mailbox unavailable (e.g.,
114
    // mailbox busy or temporarily blocked for policy reasons)
115
    public const SMTP_MAIL_ACTION_NOT_TAKEN = 450;
116
    // 451 Requested action aborted: local error in processing
117
    public const SMTP_MAIL_ACTION_ABORTED = 451;
118
    // 452 Requested action not taken: insufficient system storage
119
    public const SMTP_REQUESTED_ACTION_NOT_TAKEN = 452;
120
121
    // 500 Syntax error (may be due to a denied command)
122
    public const SMTP_SYNTAX_ERROR = 500;
123
    // 502 Comment not implemented
124
    public const SMTP_NOT_IMPLEMENTED = 502;
125
    // 503 Bad sequence of commands (may happen due to a denied command)
126
    public const SMTP_BAD_SEQUENCE = 503;
127
128
    // 550 Requested action not taken: mailbox unavailable (e.g., mailbox
129
    // not found, no access, or command rejected for policy reasons)
130
    public const SMTP_MBOX_UNAVAILABLE = 550;
131
132
    // 554 Seen this from hotmail MTAs, in response to RSET :(
133
    public const SMTP_TRANSACTION_FAILED = 554;
134
135
    /**
136
     * List of response codes considered as "greylisted"
137
     *
138
     * @var array
139
     */
140
    private $greylisted = [
141
        self::SMTP_MAIL_ACTION_NOT_TAKEN,
142
        self::SMTP_MAIL_ACTION_ABORTED,
143
        self::SMTP_REQUESTED_ACTION_NOT_TAKEN
144
    ];
145
146
    /**
147
     * Internal states we can be in
148
     *
149
     * @var array
150
     */
151
    private $state = [
152
        'helo' => false,
153
        'mail' => false,
154
        'rcpt' => false
155
    ];
156
157
    /**
158
     * Holds the socket connection resource
159
     *
160
     * @var resource
161
     */
162
    private $socket;
163
164
    /**
165
     * Holds all the domains we'll validate accounts on
166
     *
167
     * @var array
168
     */
169
    private $domains = [];
170
171
    /**
172
     * @var array
173
     */
174
    private $domains_info = [];
175
176
    /**
177
     * Default connect timeout for each MTA attempted (seconds)
178
     *
179
     * @var int
180
     */
181
    private $connect_timeout = 10;
182
183
    /**
184
     * Default sender username
185
     *
186
     * @var string
187
     */
188
    private $from_user = 'user';
189
190
    /**
191
     * Default sender host
192
     *
193
     * @var string
194
     */
195
    private $from_domain = 'localhost';
196
197
    /**
198
     * The host we're currently connected to
199
     *
200
     * @var string|null
201
     */
202
    private $host;
203
204
    /**
205
     * List of validation results
206
     *
207
     * @var array
208
     */
209
    private $results = [];
210
211
    /**
212
     * @param array|string $emails Email(s) to validate
213
     * @param string|null $sender Sender's email address
214
     */
215 19
    public function __construct($emails = [], ?string $sender = null)
216
    {
217 19
        if (!empty($emails)) {
218 13
            $this->setEmails($emails);
219
        }
220 19
        if (null !== $sender) {
221 13
            $this->setSender($sender);
222
        }
223 19
    }
224
225
    /**
226
     * Disconnects from the SMTP server if needed to release resources.
227
     *
228
     * @throws NoConnectionException
229
     * @throws SendFailedException
230
     * @throws TimeoutException
231
     * @throws UnexpectedResponseException
232
     */
233 19
    public function __destruct()
234
    {
235 19
        $this->disconnect(false);
236 19
    }
237
238
    /**
239
     * Does a catch-all test for the given domain.
240
     *
241
     * @param string $domain
242
     *
243
     * @return bool Whether the MTA accepts any random recipient.
244
     *
245
     * @throws NoConnectionException
246
     * @throws NoMailFromException
247
     * @throws SendFailedException
248
     * @throws TimeoutException
249
     * @throws UnexpectedResponseException
250
     */
251 5
    public function acceptsAnyRecipient(string $domain): bool
252
    {
253 5
        if (!$this->catchall_test) {
254 3
            return false;
255
        }
256
257 2
        $test     = 'catch-all-test-' . time();
258 2
        $accepted = $this->rcpt($test . '@' . $domain);
259 2
        if ($accepted) {
260
            // Success on a non-existing address is a "catch-all"
261 2
            $this->domains_info[$domain]['catchall'] = true;
262 2
            return true;
263
        }
264
265
        // Log when we get disconnected while trying catchall detection
266
        $this->noop();
267
        if (!$this->connected()) {
268
            $this->debug('Disconnected after trying a non-existing recipient on ' . $domain);
269
        }
270
271
        /**
272
         * N.B.:
273
         * Disconnects are considered as a non-catch-all case this way, but
274
         * that might not always be the case.
275
         */
276
        return false;
277
    }
278
279
    /**
280
     * Performs validation of specified email addresses.
281
     *
282
     * @param array|string $emails Emails to validate (or a single one as a string).
283
     * @param string|null $sender Sender email address.
284
     *
285
     * @return array List of emails and their results.
286
     *
287
     * @throws NoConnectionException
288
     * @throws NoHeloException
289
     * @throws NoMailFromException
290
     * @throws NoTimeoutException
291
     * @throws SendFailedException
292
     */
293 13
    public function validate($emails = [], ?string $sender = null): array
294
    {
295 13
        $this->results = [];
296
297 13
        if (!empty($emails)) {
298 1
            $this->setEmails($emails);
299
        }
300 13
        if (null !== $sender) {
301 1
            $this->setSender($sender);
302
        }
303
304 13
        if (empty($this->domains)) {
305 1
            return $this->results;
306
        }
307
308 12
        $this->loop();
309
310 10
        return $this->getResults();
311
    }
312
313
    /**
314
     * @throws NoConnectionException
315
     * @throws NoHeloException
316
     * @throws NoMailFromException
317
     * @throws NoTimeoutException
318
     * @throws SendFailedException
319
     */
320 12
    protected function loop(): void
321
    {
322
        // Query the MTAs on each domain if we have them
323 12
        foreach ($this->domains as $domain => $users) {
324 12
            $mxs = $this->buildMxs($domain);
325
326 12
            $this->debug('MX records (' . $domain . '): ' . print_r($mxs, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($mxs, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

326
            $this->debug('MX records (' . $domain . '): ' . /** @scrutinizer ignore-type */ print_r($mxs, true));
Loading history...
327 12
            $this->domains_info[$domain]          = [];
328 12
            $this->domains_info[$domain]['users'] = $users;
329 12
            $this->domains_info[$domain]['mxs']   = $mxs;
330
331
            // Set default results as though we can't communicate at all...
332 12
            $this->setDomainResults($users, $domain, $this->no_conn_is_valid);
333 12
            $this->attemptConnection($mxs);
334 12
            $this->performSmtpDance($domain, $users);
335
        }
336 10
    }
337
338
    /**
339
     * @param string $domain
340
     * @return array
341
     */
342 12
    protected function buildMxs(string $domain): array
343
    {
344 12
        $mxs = [];
345
346
        $this->debug('Building MX records for domain: ' . $domain);
347 12
348
        // Query the MX records for the current domain
349
        [$hosts, $weights] = $this->mxQuery($domain);
350 12
351
        // Sort out the MX priorities
352
        foreach ($hosts as $k => $host) {
353 12
            $mxs[$host] = $weights[$k];
354
        }
355
        asort($mxs);
356 12
357
        // Add the hostname itself with 0 weight (RFC 2821)
358 12
        $mxs[$domain] = 0;
359
360
        return $mxs;
361
    }
362
363
    /**
364
     * @param array $mxs
365
     *
366 12
     * @throws NoTimeoutException
367
     */
368
    protected function attemptConnection(array $mxs): void
369 12
    {
370
        // Try each host, $_weight unused in the foreach body, but array_keys() doesn't guarantee the order
371 12
        foreach ($mxs as $host => $_weight) {
372 8
            try {
373 8
                $this->connect($host);
374
                if ($this->connected()) {
375 4
                    break;
376
                }
377 4
            } catch (NoConnectionException $e) {
378
                // Unable to connect to host, so these addresses are invalid?
379
                $this->debug('Unable to connect. Exception caught: ' . $e->getMessage());
380 12
            }
381
        }
382
    }
383
384
    /**
385
     * @param string $domain
386
     * @param array $users
387
     *
388
     * @throws NoConnectionException
389
     * @throws NoHeloException
390
     * @throws NoMailFromException
391 12
     * @throws SendFailedException
392
     */
393
    protected function performSmtpDance(string $domain, array $users): void
394 12
    {
395 4
        // Bail early if not connected for whatever reason...
396
        if (!$this->connected()) {
397
            return;
398
        }
399 8
400 2
        try {
401
            $this->attemptMailCommands($domain, $users);
402
        } catch (UnexpectedResponseException $e) {
403
            // Unexpected responses handled as $this->no_comm_is_valid, that way anyone can
404 2
            // decide for themselves if such results are considered valid or not
405
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
406
        } catch (TimeoutException $e) {
407
            // A timeout is a comm failure, so treat the results on that domain
408
            // according to $this->no_comm_is_valid as well
409 6
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
410
        }
411
    }
412
413
    /**
414
     * @param string $domain
415
     * @param array $users
416
     *
417
     * @throws NoConnectionException
418
     * @throws NoHeloException
419
     * @throws NoMailFromException
420
     * @throws SendFailedException
421
     * @throws TimeoutException
422 8
     * @throws UnexpectedResponseException
423
     */
424
    protected function attemptMailCommands(string $domain, array $users): void
425 8
    {
426
        // Bail if HELO doesn't go through...
427
        if (!$this->helo()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->helo() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
428
            return;
429
        }
430 6
431
        // Try issuing MAIL FROM
432 1
        if (!$this->mail($this->from_user . '@' . $this->from_domain)) {
433 1
            // MAIL FROM not accepted, we can't talk
434
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
435
            return;
436
        }
437
438
        /**
439
         * If we're still connected, proceed (because we might get disconnected, or banned, or
440
         * greylisted temporarily etc.). See mail() for more info.
441 5
         */
442
        if (!$this->connected()) {
443
            return;
444
        }
445
446 5
        // Attempt a catch-all test for the domain (if configured to do so)
447
        $is_catchall_domain = $this->acceptsAnyRecipient($domain);
448
449
        // If a catchall domain is detected, and we consider
450
        // accounts on such domains as invalid, mark all the
451 5
        // users as invalid and move on
452 2
        if ($is_catchall_domain && !$this->catchall_is_valid) {
453 1
            $this->setDomainResults($users, $domain, $this->catchall_is_valid);
454 1
            return;
455
        }
456
457
        $this->noop();
458 4
459
        // RCPT for each user
460
        foreach ($users as $user) {
461 4
            $address                 = $user . '@' . $domain;
462 4
            $this->results[$address] = $this->rcpt($address);
463 4
        }
464
465
        // Issue a RSET for all the things we just made the MTA do
466
        $this->rset();
467 4
        $this->disconnect();
468 4
    }
469 4
470
    /**
471
     * Get validation results
472
     *
473
     * @param bool $include_domains_info Whether to include extra info in the results
474
     *
475
     * @return array
476
     */
477
    public function getResults(bool $include_domains_info = true): array
478 11
    {
479
        if ($include_domains_info) {
480 11
            $this->results['domains'] = $this->domains_info;
481 11
        } else {
482
            unset($this->results['domains']);
483 1
        }
484
485
        return $this->results;
486 11
    }
487
488
    /**
489
     * Helper to set results for all the users on a domain to a specific value
490
     *
491
     * @param array $users Users (usernames)
492
     * @param string $domain The domain for the users/usernames
493
     * @param bool $val Value to set
494
     *
495
     * @return void
496
     */
497
    private function setDomainResults(array $users, string $domain, bool $val): void
498 12
    {
499
        foreach ($users as $user) {
500 12
            $this->results[$user . '@' . $domain] = $val;
501 12
        }
502
    }
503 12
504
    /**
505
     * Returns true if we're connected to an MTA
506
     *
507
     * @return bool
508
     */
509
    protected function connected(): bool
510 19
    {
511
        return is_resource($this->socket);
512 19
    }
513
514
    /**
515
     * Tries to connect to the specified host on the pre-configured port.
516
     *
517
     * @param string $host Host to connect to
518
     *
519
     * @throws NoConnectionException
520
     * @throws NoTimeoutException
521
     *
522
     * @return void
523
     */
524
    protected function connect(string $host): void
525 12
    {
526
        $remote_socket = $host . ':' . $this->connect_port;
527 12
        $errnum        = 0;
528 12
        $errstr        = '';
529 12
        $this->host    = $remote_socket;
530 12
531
        // Open connection
532
        $this->debug('Connecting to ' . $this->host . ' (timeout: ' . $this->connect_timeout . ')');
533 12
        // @codingStandardsIgnoreLine
534
        $this->socket = /** @scrutinizer ignore-unhandled */ @stream_socket_client(
0 ignored issues
show
Documentation Bug introduced by
It seems like @stream_socket_client($t...->stream_context_args)) can also be of type false. However, the property $socket is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
535 12
            $this->host,
536 12
            $errnum,
537 12
            $errstr,
538 12
            $this->connect_timeout,
539 12
            STREAM_CLIENT_CONNECT,
540 12
            stream_context_create($this->stream_context_args)
541 12
        );
542
543
        // Check and throw if not connected
544
        if (!$this->connected()) {
545 12
            $this->debug('Connect failed: ' . $errstr . ', error number: ' . $errnum . ', host: ' . $this->host);
546 4
            throw new NoConnectionException('Cannot open a connection to remote host (' . $this->host . ')');
547 4
        }
548
549
        $result = stream_set_timeout($this->socket, $this->connect_timeout);
550 8
        if (!$result) {
551 8
            throw new NoTimeoutException('Cannot set timeout');
552
        }
553
554
        $this->debug('Connected to ' . $this->host . ' successfully');
555 8
    }
556 8
557
    /**
558
     * Disconnects the currently connected MTA.
559
     *
560
     * @param bool $quit Whether to send QUIT command before closing the socket on our end.
561
     *
562
     * @throws NoConnectionException
563
     * @throws SendFailedException
564
     * @throws TimeoutException
565
     * @throws UnexpectedResponseException
566
     */
567
    protected function disconnect(bool $quit = true): void
568 19
    {
569
        if ($quit) {
570 19
            $this->quit();
571 4
        }
572
573
        if ($this->connected()) {
574 19
            $this->debug('Closing socket to ' . $this->host);
575 8
            fclose($this->socket);
576 8
        }
577
578
        $this->host = null;
579 19
        $this->resetState();
580 19
    }
581 19
582
    /**
583
     * Resets internal state flags to defaults
584
     */
585
    private function resetState(): void
586 19
    {
587
        $this->state['helo'] = false;
588 19
        $this->state['mail'] = false;
589 19
        $this->state['rcpt'] = false;
590 19
    }
591 19
592
    /**
593
     * Sends a HELO/EHLO sequence.
594
     *
595
     * @return bool|null True if successful, false otherwise. Null if already done.
596
     *
597
     * @throws NoConnectionException
598
     * @throws SendFailedException
599
     * @throws TimeoutException
600
     * @throws UnexpectedResponseException
601
     */
602
    protected function helo(): ?bool
603 8
    {
604
        // Don't do it if already done
605
        if ($this->state['helo']) {
606 8
            return null;
607
        }
608
609
        try {
610
            $this->expect(self::SMTP_CONNECT_SUCCESS, $this->command_timeouts['helo']);
611 8
            $this->ehlo();
612 8
613
            // Session started
614
            $this->state['helo'] = true;
615 6
616
            // Are we going for a TLS connection?
617
            /*
618
            if ($this->tls) {
619
                // send STARTTLS, wait 3 minutes
620
                $this->send('STARTTLS');
621
                $this->expect(self::SMTP_CONNECT_SUCCESS, $this->command_timeouts['tls']);
622
                $result = stream_socket_enable_crypto($this->socket, true,
623
                    STREAM_CRYPTO_METHOD_TLS_CLIENT);
624
                if (!$result) {
625
                    throw new SMTP_Validate_Email_Exception_No_TLS('Cannot enable TLS');
626
                }
627
            }
628
            */
629
630
            $result = true;
631 6
        } catch (UnexpectedResponseException $e) {
632 2
            // Connected, but got an unexpected response, so disconnect
633
            $result = false;
634
            $this->debug('Unexpected response after connecting: ' . $e->getMessage());
635
            $this->disconnect(false);
636
        }
637
638
        return $result;
639 6
    }
640
641
    /**
642
     * Sends `EHLO` or `HELO`, depending on what's supported by the remote host.
643
     *
644
     * @throws NoConnectionException
645
     * @throws SendFailedException
646
     * @throws TimeoutException
647
     * @throws UnexpectedResponseException
648
     */
649
    protected function ehlo(): void
650 8
    {
651
        try {
652
            // Modern
653
            $this->send('EHLO ' . $this->from_domain);
654 8
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['ehlo']);
655 6
        } catch (UnexpectedResponseException $e) {
656 2
            // Legacy
657
            $this->send('HELO ' . $this->from_domain);
658
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['helo']);
659
        }
660
    }
661 6
662
    /**
663
     * Sends a `MAIL FROM` command which indicates the sender.
664
     *
665
     * @param string $from
666
     *
667
     * @return bool Whether the command was accepted or not.
668
     *
669
     * @throws NoConnectionException
670
     * @throws NoHeloException
671
     * @throws SendFailedException
672
     * @throws TimeoutException
673
     * @throws UnexpectedResponseException
674
     */
675
    protected function mail(string $from): bool
676 6
    {
677
        if (!$this->state['helo']) {
678 6
            throw new NoHeloException('Need HELO before MAIL FROM');
679
        }
680
681
        // Issue MAIL FROM, 5 minute timeout
682
        $this->send('MAIL FROM:<' . $from . '>');
683 6
684
        try {
685
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['mail']);
686 6
687
            // Set state flags
688
            $this->state['mail'] = true;
689 5
            $this->state['rcpt'] = false;
690 5
691
            $result = true;
692 5
        } catch (UnexpectedResponseException $e) {
693 1
            $result = false;
694 1
695
            // Got something unexpected in response to MAIL FROM
696
            $this->debug("Unexpected response to MAIL FROM\n:" . $e->getMessage());
697 1
698
            // Hotmail has been known to do this + was closing the connection
699
            // forcibly on their end, so we're killing the socket here too
700
            $this->disconnect(false);
701 1
        }
702
703
        return $result;
704 6
    }
705
706
    /**
707
     * Sends a RCPT TO command to indicate a recipient. Returns whether the
708
     * recipient was accepted or not.
709
     *
710
     * @param string $to Recipient (email address).
711
     *
712
     * @return bool Whether the address was accepted or not.
713
     *
714
     * @throws NoMailFromException
715
     */
716
    protected function rcpt(string $to): bool
717 5
    {
718
        // Need to have issued MAIL FROM first
719
        if (!$this->state['mail']) {
720 5
            throw new NoMailFromException('Need MAIL FROM before RCPT TO');
721
        }
722
723
        $valid          = false;
724 5
        $expected_codes = [
725
            self::SMTP_GENERIC_SUCCESS,
726 5
            self::SMTP_USER_NOT_LOCAL
727 5
        ];
728
729
        if ($this->greylisted_considered_valid) {
730 5
            $expected_codes = array_merge($expected_codes, $this->greylisted);
731 5
        }
732
733
        // Issue RCPT TO, 5 minute timeout
734
        try {
735
            $this->send('RCPT TO:<' . $to . '>');
736 5
            // Handle response
737
            try {
738
                $this->expect($expected_codes, $this->command_timeouts['rcpt']);
739 5
                $this->state['rcpt'] = true;
740 5
                $valid               = true;
741 5
            } catch (UnexpectedResponseException $e) {
742
                $this->debug('Unexpected response to RCPT TO: ' . $e->getMessage());
743 5
            }
744
        } catch (Exception $e) {
745
            $this->debug('Sending RCPT TO failed: ' . $e->getMessage());
746
        }
747
748
        return $valid;
749 5
    }
750
751
    /**
752
     * Sends a RSET command and resets certain parts of internal state.
753
     *
754
     * @throws NoConnectionException
755
     * @throws SendFailedException
756
     * @throws TimeoutException
757
     * @throws UnexpectedResponseException
758
     */
759
    protected function rset(): void
760 4
    {
761
        $this->send('RSET');
762 4
763
        // MS ESMTP doesn't follow RFC according to ZF tracker, see [ZF-1377]
764
        $expected = [
765
            self::SMTP_GENERIC_SUCCESS,
766 4
            self::SMTP_CONNECT_SUCCESS,
767 4
            self::SMTP_NOT_IMPLEMENTED,
768 4
            // hotmail returns this o_O
769
            self::SMTP_TRANSACTION_FAILED
770 4
        ];
771
        $this->expect($expected, $this->command_timeouts['rset'], true);
772 4
        $this->state['mail'] = false;
773 4
        $this->state['rcpt'] = false;
774 4
    }
775 4
776
    /**
777
     * Sends a QUIT command.
778
     *
779
     * @throws NoConnectionException
780
     * @throws SendFailedException
781
     * @throws TimeoutException
782
     * @throws UnexpectedResponseException
783
     */
784
    protected function quit(): void
785 4
    {
786
        // Although RFC says QUIT can be issued at any time, we won't
787
        if ($this->state['helo']) {
788 4
            $this->send('QUIT');
789 4
            $this->expect(
790 4
                [self::SMTP_GENERIC_SUCCESS,self::SMTP_QUIT_SUCCESS],
791 4
                $this->command_timeouts['quit'],
792 4
                true
793 4
            );
794
        }
795
    }
796 4
797
    /**
798
     * Sends a NOOP command.
799
     *
800
     * @throws NoConnectionException
801
     * @throws SendFailedException
802
     * @throws TimeoutException
803
     * @throws UnexpectedResponseException
804
     */
805
    protected function noop(): void
806 4
    {
807
        // Bail if NOOPs are not to be sent.
808
        if (!$this->send_noops) {
809 4
            return;
810 1
        }
811
812
        $this->send('NOOP');
813 3
814
        /**
815
         * The `SMTP` string is here to fix issues with some bad RFC implementations.
816
         * Found at least 1 server replying to NOOP without any code.
817
         */
818
        $expected_codes = [
819
            'SMTP',
820 3
            self::SMTP_BAD_SEQUENCE,
821 3
            self::SMTP_NOT_IMPLEMENTED,
822 3
            self::SMTP_GENERIC_SUCCESS,
823 3
            self::SMTP_SYNTAX_ERROR,
824 3
            self::SMTP_CONNECT_SUCCESS
825 3
        ];
826
        $this->expect($expected_codes, $this->command_timeouts['noop'], true);
827 3
    }
828 3
829
    /**
830
     * Sends a command to the remote host.
831
     *
832
     * @param string $cmd The command to send.
833
     *
834
     * @return int Number of bytes written to the stream.
835
     *
836
     * @throws NoConnectionException
837
     * @throws SendFailedException
838
     */
839
    protected function send(string $cmd): int
840 8
    {
841
        // Must be connected
842
        $this->throwIfNotConnected();
843 8
844
        $this->debug('send>>>: ' . $cmd);
845 6
        // Write the cmd to the connection stream
846
        $result = fwrite($this->socket, $cmd . self::CRLF);
847 6
848
        // Did it work?
849
        if (false === $result) {
850 6
            throw new SendFailedException('Send failed on: ' . $this->host);
851
        }
852
853
        return $result;
854 6
    }
855
856
    /**
857
     * Receives a response line from the remote host.
858
     *
859
     * @param int|null $timeout Timeout in seconds.
860
     *
861
     * @return string Response line from the remote host.
862
     *
863
     * @throws NoConnectionException
864
     * @throws TimeoutException
865
     * @throws NoResponseException
866
     */
867
    protected function recv(?int $timeout = null): string
868 8
    {
869
        // Must be connected
870
        $this->throwIfNotConnected();
871 8
872
        // Has a custom timeout been specified?
873
        if (null !== $timeout) {
874 8
            stream_set_timeout($this->socket, $timeout);
875 8
        }
876
877
        // Retrieve response
878
        $line = fgets($this->socket, 1024);
879 8
        $this->debug('<<<recv: ' . $line);
880 8
881
        // Have we timed out?
882
        $info = stream_get_meta_data($this->socket);
883 8
        if (!empty($info['timed_out'])) {
884 8
            throw new TimeoutException('Timed out in recv');
885
        }
886
887
        // Did we actually receive anything?
888
        if (false === $line) {
889 8
            throw new NoResponseException('No response in recv');
890 2
        }
891
892
        return $line;
893 6
    }
894
895
    /**
896
     * @param int|int[]|array|string $codes List of one or more expected response codes.
897
     * @param int|null $timeout The timeout for this individual command, if any.
898
     * @param bool $empty_response_allowed When true, empty responses are allowed.
899
     *
900
     * @return string The last text message received.
901
     *
902
     * @throws NoConnectionException
903
     * @throws SendFailedException
904
     * @throws TimeoutException
905
     * @throws UnexpectedResponseException
906
     */
907
    protected function expect($codes, ?int $timeout = null, bool $empty_response_allowed = false): string
908 8
    {
909
        if (!is_array($codes)) {
910 8
            $codes = (array) $codes;
911 8
        }
912
913
        $code = null;
914 8
        $text = '';
915 8
916
        try {
917
            $line = $this->recv($timeout);
918 8
            $text = $line;
919 6
            while (preg_match('/^\d+-/', $line)) {
920 6
                $line  = $this->recv($timeout);
921 6
                $text .= $line;
922 6
            }
923
            sscanf($line, '%d%s', $code, $text);
924 6
            // TODO/FIXME: This is terrible to read/comprehend
925
            if ($code === self::SMTP_SERVICE_UNAVAILABLE ||
926 6
                (false === $empty_response_allowed && (null === $code || !in_array($code, $codes, true)))) {
927 6
                throw new UnexpectedResponseException($line);
928 6
            }
929
        } catch (NoResponseException $e) {
930 3
            /**
931
             * No response in expect() probably means that the remote server
932
             * forcibly closed the connection so let's clean up on our end as well?
933
             */
934
            $this->debug('No response in expect(): ' . $e->getMessage());
935 2
            $this->disconnect(false);
936 2
        }
937
938
        return $text;
939 8
    }
940
941
    /**
942
     * Splits the email address string into its respective user and domain parts
943
     * and returns those as an array.
944
     *
945
     * @param string $email Email address.
946
     *
947
     * @return array ['user', 'domain']
948
     */
949
    protected function splitEmail(string $email): array
950 14
    {
951
        $parts  = explode('@', $email);
952 14
        $domain = array_pop($parts);
953 14
        $user   = implode('@', $parts);
954 14
955
        return [$user, $domain];
956 14
    }
957
958
    /**
959
     * Sets the email addresses that should be validated.
960
     *
961
     * @param array|string $emails List of email addresses (or a single one a string).
962
     */
963
    public function setEmails($emails): void
964 13
    {
965
        if (!is_array($emails)) {
966 13
            $emails = (array) $emails;
967 12
        }
968
969
        $this->domains = [];
970 13
971
        foreach ($emails as $email) {
972 13
            [$user, $domain] = $this->splitEmail($email);
973 13
            if (!isset($this->domains[$domain])) {
974 13
                $this->domains[$domain] = [];
975 13
            }
976
            $this->domains[$domain][] = $user;
977 13
        }
978
    }
979 13
980
    /**
981
     * Sets the email address to use as the sender/validator.
982
     *
983
     * @param string $email
984
     */
985
    public function setSender(string $email): void
986 13
    {
987
        $parts             = $this->splitEmail($email);
988 13
        $this->from_user   = $parts[0];
989 13
        $this->from_domain = $parts[1];
990 13
    }
991 13
992
    /**
993
     * Queries the DNS server for MX entries of a certain domain.
994
     *
995
     * @param string $domain The domain for which to retrieve MX records.
996
     *
997
     * @return array MX hosts and their weights.
998
     */
999
    protected function mxQuery(string $domain): array
1000 12
    {
1001
        // If the domain does not end with a '.', add it (making it an absolute fqdn, which prevents any
1002 12
        // further suffixing attempts by wrongly configured resolvers etc.)
1003 12
        if (!preg_match('/\.$/', $domain)) {
1004 12
            $domain .= '.';
1005
        }
1006 12
1007
        $hosts  = [];
1008
        $weight = [];
1009
        getmxrr($domain, $hosts, $weight);
1010
1011
        return [$hosts, $weight];
1012
    }
1013
1014 8
    /**
1015
     * Throws if not currently connected.
1016 8
     *
1017 2
     * @throws NoConnectionException
1018
     */
1019 8
    private function throwIfNotConnected(): void
1020
    {
1021
        if (!$this->connected()) {
1022
            throw new NoConnectionException('No connection');
1023
        }
1024
    }
1025
1026
    /**
1027 12
     * Debug helper. If it detects a CLI env, it just dumps given `$str` on a
1028
     * new line, otherwise it prints stuff wrapped in <pre> tags.
1029 12
     *
1030 12
     * @param string $str
1031 12
     */
1032 1
    private function debug(string $str): void
1033
    {
1034
        $str = $this->stamp($str);
1035 1
1036
        $this->log($str);
1037 12
1038
        if ($this->debug) {
1039
            if ('cli' !== PHP_SAPI) {
1040
                $str = '<br/><pre>' . htmlspecialchars($str) . '</pre>';
1041
            }
1042
            echo "\n" . $str;
1043
        }
1044 12
    }
1045
1046 12
    /**
1047 12
     * Adds a message to the log array
1048
     *
1049
     * @param string $msg
1050
     */
1051
    private function log(string $msg): void
1052
    {
1053
        $this->log[] = $msg;
1054
    }
1055
1056 12
    /**
1057
     * Prepends the given $msg with the current date and time inside square brackets.
1058 12
     *
1059 12
     * @param string $msg
1060
     *
1061 12
     * @return string
1062
     */
1063
    private function stamp(string $msg): string
1064
    {
1065
        return '[' . $this->getLogDate() . '] ' . $msg;
1066
    }
1067
1068
    /**
1069 6
     * Logging helper which returns (formatted) current date and time
1070
     * (with microseconds) but avoids sprintf/microtime(true) combo.
1071 6
     * Empty string returned on failure.
1072
     *
1073
     * @see https://github.com/zytzagoo/smtp-validate-email/pull/58
1074
     *
1075
     * @return string
1076
     */
1077 1
    public function getLogDate(): string
1078
    {
1079 1
        $dt = \DateTime::createFromFormat('0.u00 U', microtime());
1080 1
1081
        $date = '';
1082
        if (false !== $dt) {
1083
            $date = $dt->format('Y-m-d\TH:i:s.uO');
1084
        }
1085
1086
        return $date;
1087
    }
1088
1089
    /**
1090 2
     * Returns the log array.
1091
     *
1092 2
     * @return array
1093 2
     */
1094 2
    public function getLog(): array
1095
    {
1096 1
        return $this->log;
1097
    }
1098
1099
    /**
1100
     * Truncates the log array.
1101
     */
1102
    public function clearLog(): void
1103
    {
1104
        $this->log = [];
1105 10
    }
1106
1107 10
    /**
1108 10
     * Compat for old lower_cased method calls.
1109
     *
1110
     * @param string $name
1111
     * @param array  $args
1112
     *
1113
     * @return mixed
1114
     */
1115 1
    public function __call(string $name, array $args)
1116
    {
1117 1
        $camelized = self::camelize($name);
1118
        if (\method_exists($this, $camelized)) {
1119
            return \call_user_func_array([$this, $camelized], $args);
1120
        }
1121
1122
        trigger_error('Fatal error: Call to undefined method ' . self::class . '::' . $name . '()', E_USER_ERROR);
1123
    }
1124
1125 9
    /**
1126
     * Set the desired connect timeout.
1127 9
     *
1128 9
     * @param int $timeout Connect timeout in seconds.
1129
     */
1130
    public function setConnectTimeout(int $timeout): void
1131
    {
1132
        $this->connect_timeout = $timeout;
1133
    }
1134
1135 1
    /**
1136
     * Get the current connect timeout.
1137 1
     *
1138
     * @return int
1139
     */
1140
    public function getConnectTimeout(): int
1141
    {
1142
        return $this->connect_timeout;
1143 3
    }
1144
1145 3
    /**
1146 3
     * Set connect port.
1147
     *
1148
     * @param int $port
1149
     */
1150
    public function setConnectPort(int $port): void
1151 1
    {
1152
        $this->connect_port = $port;
1153 1
    }
1154 1
1155
    /**
1156
     * Get current connect port.
1157
     *
1158
     * @return int
1159
     */
1160
    public function getConnectPort(): int
1161 1
    {
1162
        return $this->connect_port;
1163 1
    }
1164
1165
    /**
1166
     * Turn on "catch-all" detection.
1167
     */
1168
    public function enableCatchAllTest(): void
1169
    {
1170
        $this->catchall_test = true;
1171 2
    }
1172
1173 2
    /**
1174 2
     * Turn off "catch-all" detection.
1175
     */
1176
    public function disableCatchAllTest(): void
1177
    {
1178
        $this->catchall_test = false;
1179
    }
1180
1181 1
    /**
1182
     * Returns whether "catch-all" test is to be performed or not.
1183 1
     *
1184
     * @return bool
1185
     */
1186
    public function isCatchAllEnabled(): bool
1187
    {
1188
        return $this->catchall_test;
1189
    }
1190
1191 2
    /**
1192
     * Set whether "catch-all" results are considered valid or not.
1193 2
     *
1194 2
     * @param bool $flag When true, "catch-all" accounts are considered valid
1195
     */
1196
    public function setCatchAllValidity(bool $flag): void
1197
    {
1198
        $this->catchall_is_valid = $flag;
1199 1
    }
1200
1201 1
    /**
1202
     * Get current state of "catch-all" validity flag.
1203
     *
1204
     * @return bool
1205
     */
1206
    public function getCatchAllValidity(): bool
1207
    {
1208
        return $this->catchall_is_valid;
1209
    }
1210
1211 2
    /**
1212
     * Control sending of NOOP commands.
1213 2
     *
1214 2
     * @param bool $val
1215 2
     */
1216 2
    public function sendNoops(bool $val): void
1217 2
    {
1218
        $this->send_noops = $val;
1219
    }
1220 2
1221
    /**
1222
     * @return bool
1223
     */
1224
    public function sendingNoops(): bool
1225
    {
1226
        return $this->send_noops;
1227
    }
1228
1229
    /**
1230
     * Specify the socket bind address.
1231
     *
1232
     * This can be used to specify the IP address (v4 or v6) and/or the port number that
1233
     * PHP will use to access the network. The syntax is ip:port for v4 and [ip]:port for v6.
1234
     * Setting the IP or the port to 0 lets the system choose the IP and/or port.
1235
     * When no port is explicitly provided, it's defaulted to 0.
1236
     *
1237
     * @param string $bindAddress Socket bind address in `ip:port` or `[ip]:port` syntax
1238
     *
1239
     * @return void
1240
     */
1241
    public function setBindAddress(string $bindAddress): void
1242
    {
1243
        $ipWithPort = $this->parseBindAddress($bindAddress);
1244
1245
        $this->stream_context_args['socket']['bindto'] = $ipWithPort;
1246
    }
1247
1248
    /**
1249
     * Get the configured socket bind address. Null means system default is used.
1250
     *
1251
     * @return string|null
1252
     */
1253
    public function getBindAddress(): ?string
1254
    {
1255
        return $this->stream_context_args['socket']['bindto'] ?? null;
1256
    }
1257
1258
    /**
1259
     * Parse most commonly used ways of specifying socket bind addresses
1260
     * into an `ip:port` or `[ip]:port` format/syntax.
1261
     *
1262
     * @param string $bindAddress
1263
     *
1264
     * @return string
1265
     */
1266
    protected function parseBindAddress(string $bindAddress): string
1267
    {
1268
        // TODO/FIXME: This should be way more robust if all possible edge-cases are supposed to work
1269
1270
        if (($bindAddress[0] !== '[') && filter_var($bindAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1271
            // If given string does not start with [, but is valid ipv6, wrap it in [] and
1272
            // assume port is 0
1273
            $ip = '[' . $bindAddress . ']';
1274
            $port = '0';
1275
        } else {
1276
            // If address starts with [ or does not appear to be ipv6, let parse_url() handle it
1277
            $parts = @parse_url('https://' . $bindAddress);
1278
            $ip = $parts['host'] ?? $bindAddress;
1279
            $port = $parts['port'] ?? '0';
1280
        }
1281
1282
        return $ip . ':' . $port;
1283
    }
1284
1285
    /**
1286
     * Camelizes a string.
1287
     *
1288
     * @param string $id String to camelize.
1289
     *
1290
     * @return string
1291
     */
1292
    private static function camelize(string $id): string
1293
    {
1294
        return strtr(
1295
            ucwords(
1296
                strtr(
1297
                    $id,
1298
                    ['_' => ' ', '.' => '_ ', '\\' => '_ ']
1299
                )
1300
            ),
1301
            [' ' => '']
1302
        );
1303
    }
1304
}
1305