Passed
Pull Request — master (#50)
by zyt
10:07
created

Validator::sendingNoops()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SMTPValidateEmail;
4
5
use \SMTPValidateEmail\Exceptions\Exception as 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
    const CRLF = "\r\n";
103
104
    // Some smtp response codes
105
    const SMTP_CONNECT_SUCCESS = 220;
106
    const SMTP_QUIT_SUCCESS    = 221;
107
    const SMTP_GENERIC_SUCCESS = 250;
108
    const SMTP_USER_NOT_LOCAL  = 251;
109
    const SMTP_CANNOT_VRFY     = 252;
110
111
    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
    const SMTP_MAIL_ACTION_NOT_TAKEN = 450;
116
    // 451 Requested action aborted: local error in processing
117
    const SMTP_MAIL_ACTION_ABORTED = 451;
118
    // 452 Requested action not taken: insufficient system storage
119
    const SMTP_REQUESTED_ACTION_NOT_TAKEN = 452;
120
121
    // 500 Syntax error (may be due to a denied command)
122
    const SMTP_SYNTAX_ERROR = 500;
123
    // 502 Comment not implemented
124
    const SMTP_NOT_IMPLEMENTED = 502;
125
    // 503 Bad sequence of commands (may be due to a denied command)
126
    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
    const SMTP_MBOX_UNAVAILABLE = 550;
131
132
    // 554 Seen this from hotmail MTAs, in response to RSET :(
133
    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 = null;
203
204
    /**
205
     * List of validation results
206
     *
207
     * @var array
208 15
     */
209
    private $results = [];
210 15
211 10
    /**
212
     * @param array|string $emails Email(s) to validate
213 15
     * @param string|null $sender Sender's email address
214 10
     */
215
    public function __construct($emails = [], $sender = null)
216 15
    {
217
        if (!empty($emails)) {
218
            $this->setEmails($emails);
219
        }
220
        if (null !== $sender) {
221 15
            $this->setSender($sender);
222
        }
223 15
    }
224 15
225
    /**
226 3
     * Disconnects from the SMTP server if needed to release resources.
227
     *
228 3
     * @throws NoConnectionException
229 1
     * @throws SendFailedException
230
     * @throws TimeoutException
231
     * @throws UnexpectedResponseException
232 2
     */
233 2
    public function __destruct()
234 2
    {
235
        $this->disconnect(false);
236 2
    }
237 2
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
    public function acceptsAnyRecipient($domain)
252
    {
253
        if (!$this->catchall_test) {
254
            return false;
255
        }
256
257
        $test     = 'catch-all-test-' . time();
258
        $accepted = $this->rcpt($test . '@' . $domain);
259
        if ($accepted) {
260
            // Success on a non-existing address is a "catch-all"
261 10
            $this->domains_info[$domain]['catchall'] = true;
262
            return true;
263 10
        }
264
265 10
        // Log when we get disconnected while trying catchall detection
266 1
        $this->noop();
267
        if (!$this->connected()) {
268 10
            $this->debug('Disconnected after trying a non-existing recipient on ' . $domain);
269 1
        }
270
271
        /**
272 10
         * N.B.:
273 1
         * Disconnects are considered as a non-catch-all case this way, but
274
         * that might not always be the case.
275
         */
276 9
        return false;
277
    }
278 8
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 9
     *
285
     * @return array List of emails and their results.
286
     *
287 9
     * @throws NoConnectionException
288 9
     * @throws NoHeloException
289
     * @throws NoMailFromException
290 9
     * @throws NoTimeoutException
291 9
     * @throws SendFailedException
292 9
     */
293 9
    public function validate($emails = [], $sender = null)
294
    {
295
        $this->results = [];
296 9
297 9
        if (!empty($emails)) {
298 9
            $this->setEmails($emails);
299
        }
300 8
        if (null !== $sender) {
301
            $this->setSender($sender);
302
        }
303
304
        if (empty($this->domains)) {
305
            return $this->results;
306 9
        }
307
308 9
        $this->loop();
309
310
        return $this->getResults();
311 9
    }
312
313
    /**
314 9
     * @throws NoConnectionException
315
     * @throws NoHeloException
316
     * @throws NoMailFromException
317 9
     * @throws NoTimeoutException
318
     * @throws SendFailedException
319
     */
320 9
    protected function loop()
321
    {
322 9
        // Query the MTAs on each domain if we have them
323
        foreach ($this->domains as $domain => $users) {
324
            $mxs = $this->buildMxs($domain);
325
326
            $this->debug('MX records (' . $domain . '): ' . print_r($mxs, true));
327
            $this->domains_info[$domain]          = [];
328
            $this->domains_info[$domain]['users'] = $users;
329 9
            $this->domains_info[$domain]['mxs']   = $mxs;
330
331
            // Set default results as though we can't communicate at all...
332 9
            $this->setDomainResults($users, $domain, $this->no_conn_is_valid);
333
            $this->attemptConnection($mxs);
334
            $this->performSmtpDance($domain, $users);
335 9
        }
336 5
    }
337 5
338
    /**
339 4
     * @param string $domain
340
     * @return array
341 4
     */
342
    protected function buildMxs($domain)
343
    {
344
        $mxs = [];
345 9
346
        // Query the MX records for the current domain
347 9
        list($hosts, $weights) = $this->mxQuery($domain);
348
349
        // Sort out the MX priorities
350 9
        foreach ($hosts as $k => $host) {
351
            $mxs[$host] = $weights[$k];
352
        }
353 5
        asort($mxs);
354
355 4
        // Add the hostname itself with 0 weight (RFC 2821)
356
        $mxs[$domain] = 0;
357 1
358
        return $mxs;
359
    }
360
361
    /**
362
     * @param array $mxs
363
     *
364
     * @throws NoTimeoutException
365 4
     */
366 3
    protected function attemptConnection(array $mxs)
367
    {
368
        // Try each host, $_weight unused in the foreach body, but array_keys() doesn't guarantee the order
369 3
        foreach ($mxs as $host => $_weight) {
370
            try {
371
                $this->connect($host);
372
                if ($this->connected()) {
373
                    break;
374 3
                }
375 2
            } catch (NoConnectionException $e) {
376 1
                // Unable to connect to host, so these addresses are invalid?
377 1
                $this->debug('Unable to connect. Exception caught: ' . $e->getMessage());
378
            }
379
        }
380
    }
381
382 2
    /**
383 2
     * @param string $domain
384
     * @param array $users
385 2
     *
386 2
     * @throws NoConnectionException
387 2
     * @throws NoHeloException
388 2
     * @throws NoMailFromException
389
     * @throws SendFailedException
390
     */
391
    protected function performSmtpDance($domain, array $users)
392
    {
393 2
        // Bail early if not connected for whatever reason...
394
        if (!$this->connected()) {
395 2
            return;
396 3
        }
397
398
        try {
399
            $this->attemptMailCommands($domain, $users);
400 1
        } catch (UnexpectedResponseException $e) {
401
            // Unexpected responses handled as $this->no_comm_is_valid, that way anyone can
402
            // decide for themselves if such results are considered valid or not
403
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
404 1
        } catch (TimeoutException $e) {
405
            // A timeout is a comm failure, so treat the results on that domain
406
            // according to $this->no_comm_is_valid as well
407
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
408
        }
409
    }
410 7
411
    /**
412
     * @param string $domain
413
     * @param array $users
414
     *
415
     * @throws NoConnectionException
416
     * @throws NoHeloException
417
     * @throws NoMailFromException
418
     * @throws SendFailedException
419 9
     * @throws TimeoutException
420
     * @throws UnexpectedResponseException
421 9
     */
422 9
    protected function attemptMailCommands($domain, array $users)
423
    {
424 1
        // Bail if HELO doesn't go through...
425
        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...
426
            return;
427 9
        }
428
429
        // Try issuing MAIL FROM
430
        if (!$this->mail($this->from_user . '@' . $this->from_domain)) {
431
            // MAIL FROM not accepted, we can't talk
432
            $this->setDomainResults($users, $domain, $this->no_comm_is_valid);
433
            return;
434
        }
435
436
        /**
437
         * If we're still connected, proceed (cause we might get
438
         * disconnected, or banned, or greylisted temporarily etc.)
439 9
         * see mail() for more
440
         */
441 9
        if (!$this->connected()) {
442 9
            return;
443
        }
444 9
445
        // Attempt a catch-all test for the domain (if configured to do so)
446
        $is_catchall_domain = $this->acceptsAnyRecipient($domain);
447
448
        // If a catchall domain is detected, and we consider
449
        // accounts on such domains as invalid, mark all the
450
        // users as invalid and move on
451 15
        if ($is_catchall_domain) {
452
            if (!$this->catchall_is_valid) {
453 15
                $this->setDomainResults($users, $domain, $this->catchall_is_valid);
454
                return;
455
            }
456
        }
457
458
        $this->noop();
459
460
        // RCPT for each user
461
        foreach ($users as $user) {
462
            $address                 = $user . '@' . $domain;
463
            $this->results[$address] = $this->rcpt($address);
464
        }
465
466 9
        // Issue a RSET for all the things we just made the MTA do
467
        $this->rset();
468 9
        $this->disconnect();
469 9
    }
470 9
471 9
    /**
472
     * Get validation results
473
     *
474 9
     * @param bool $include_domains_info Whether to include extra info in the results
475
     *
476 9
     * @return array
477 9
     */
478 9
    public function getResults($include_domains_info = true)
479 9
    {
480 9
        if ($include_domains_info) {
481 9
            $this->results['domains'] = $this->domains_info;
482 9
        } else {
483
            unset($this->results['domains']);
484
        }
485
486 9
        return $this->results;
487 4
    }
488 4
489
    /**
490
     * Helper to set results for all the users on a domain to a specific value
491 5
     *
492 5
     * @param array $users Users (usernames)
493
     * @param string $domain The domain for the users/usernames
494
     * @param bool $val Value to set
495
     *
496 5
     * @return void
497 5
     */
498
    private function setDomainResults(array $users, $domain, $val)
499
    {
500
        foreach ($users as $user) {
501
            $this->results[$user . '@' . $domain] = $val;
502
        }
503
    }
504
505
    /**
506 15
     * Returns true if we're connected to an MTA
507
     *
508 15
     * @return bool
509 2
     */
510
    protected function connected()
511
    {
512 15
        return is_resource($this->socket);
513 5
    }
514 5
515
    /**
516
     * Tries to connect to the specified host on the pre-configured port.
517 15
     *
518 15
     * @param string $host Host to connect to
519 15
     *
520
     * @throws NoConnectionException
521
     * @throws NoTimeoutException
522
     *
523
     * @return void
524
     */
525
    protected function connect($host)
526 15
    {
527
        $remote_socket = $host . ':' . $this->connect_port;
528 15
        $errnum        = 0;
529 15
        $errstr        = '';
530 15
        $this->host    = $remote_socket;
531 15
532
        // Open connection
533
        $this->debug('Connecting to ' . $this->host);
534
        // @codingStandardsIgnoreLine
535
        $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...
536
            $this->host,
537
            $errnum,
538
            $errstr,
539
            $this->connect_timeout,
540 5
            STREAM_CLIENT_CONNECT,
541
            stream_context_create($this->stream_context_args)
542
        );
543 5
544
        // Check and throw if not connected
545
        if (!$this->connected()) {
546
            $this->debug('Connect failed: ' . $errstr . ', error number: ' . $errnum . ', host: ' . $this->host);
547 5
            throw new NoConnectionException('Cannot open a connection to remote host (' . $this->host . ')');
548
        }
549 5
550 5
        $result = stream_set_timeout($this->socket, $this->connect_timeout);
551
        if (!$result) {
552
            throw new NoTimeoutException('Cannot set timeout');
553 4
        }
554
555
        $this->debug('Connected to ' . $this->host . ' successfully');
556
    }
557
558
    /**
559
     * Disconnects the currently connected MTA.
560
     *
561
     * @param bool $quit Whether to send QUIT command before closing the socket on our end.
562
     *
563
     * @throws NoConnectionException
564
     * @throws SendFailedException
565
     * @throws TimeoutException
566
     * @throws UnexpectedResponseException
567
     */
568
    protected function disconnect($quit = true)
569 4
    {
570 1
        if ($quit) {
571
            $this->quit();
572
        }
573
574
        if ($this->connected()) {
575
            $this->debug('Closing socket to ' . $this->host);
576
            fclose($this->socket);
577 4
        }
578
579
        $this->host = null;
580
        $this->resetState();
581
    }
582
583
    /**
584
     * Resets internal state flags to defaults
585 5
     */
586
    private function resetState()
587
    {
588
        $this->state['helo'] = false;
589 5
        $this->state['mail'] = false;
590 4
        $this->state['rcpt'] = false;
591 1
    }
592
593
    /**
594
     * Sends a HELO/EHLO sequence.
595
     *
596 4
     * @return bool|null True if successful, false otherwise. Null if already done.
597
     *
598
     * @throws NoConnectionException
599
     * @throws SendFailedException
600
     * @throws TimeoutException
601
     * @throws UnexpectedResponseException
602
     */
603
    protected function helo()
604
    {
605
        // Don't do it if already done
606
        if ($this->state['helo']) {
607 4
            return null;
608
        }
609 4
610
        try {
611
            $this->expect(self::SMTP_CONNECT_SUCCESS, $this->command_timeouts['helo']);
612
            $this->ehlo();
613
614 4
            // Session started
615
            $this->state['helo'] = true;
616
617 4
            // Are we going for a TLS connection?
618
            /*
619
            if ($this->tls) {
620 3
                // send STARTTLS, wait 3 minutes
621 3
                $this->send('STARTTLS');
622
                $this->expect(self::SMTP_CONNECT_SUCCESS, $this->command_timeouts['tls']);
623 3
                $result = stream_socket_enable_crypto($this->socket, true,
624 1
                    STREAM_CRYPTO_METHOD_TLS_CLIENT);
625 1
                if (!$result) {
626
                    throw new SMTP_Validate_Email_Exception_No_TLS('Cannot enable TLS');
627
                }
628 1
            }
629
            */
630
631
            $result = true;
632 1
        } catch (UnexpectedResponseException $e) {
633
            // Connected, but got an unexpected response, so disconnect
634
            $result = false;
635 4
            $this->debug('Unexpected response after connecting: ' . $e->getMessage());
636
            $this->disconnect(false);
637
        }
638
639
        return $result;
640
    }
641
642
    /**
643
     * Sends `EHLO` or `HELO`, depending on what's supported by the remote host.
644
     *
645
     * @throws NoConnectionException
646 3
     * @throws SendFailedException
647
     * @throws TimeoutException
648
     * @throws UnexpectedResponseException
649 3
     */
650
    protected function ehlo()
651
    {
652
        try {
653 3
            // Modern
654
            $this->send('EHLO ' . $this->from_domain);
655 3
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['ehlo']);
656 3
        } catch (UnexpectedResponseException $e) {
657
            // Legacy
658
            $this->send('HELO ' . $this->from_domain);
659 3
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['helo']);
660 3
        }
661
    }
662
663
    /**
664
     * Sends a `MAIL FROM` command which indicates the sender.
665 3
     *
666
     * @param string $from
667
     *
668 3
     * @return bool Whether the command was accepted or not.
669 3
     *
670 3
     * @throws NoConnectionException
671
     * @throws NoHeloException
672 3
     * @throws SendFailedException
673
     * @throws TimeoutException
674
     * @throws UnexpectedResponseException
675
     */
676
    protected function mail($from)
677
    {
678 3
        if (!$this->state['helo']) {
679
            throw new NoHeloException('Need HELO before MAIL FROM');
680
        }
681
682
        // Issue MAIL FROM, 5 minute timeout
683
        $this->send('MAIL FROM:<' . $from . '>');
684
685
        try {
686 2
            $this->expect(self::SMTP_GENERIC_SUCCESS, $this->command_timeouts['mail']);
687
688 2
            // Set state flags
689
            $this->state['mail'] = true;
690
            $this->state['rcpt'] = false;
691
692 2
            $result = true;
693 2
        } catch (UnexpectedResponseException $e) {
694 2
            $result = false;
695
696 2
            // Got something unexpected in response to MAIL FROM
697
            $this->debug("Unexpected response to MAIL FROM\n:" . $e->getMessage());
698 2
699 2
            // Hotmail has been known to do this + was closing the connection
700 2
            // forcibly on their end, so we're killing the socket here too
701 2
            $this->disconnect(false);
702
        }
703
704
        return $result;
705
    }
706
707
    /**
708 2
     * Sends a RCPT TO command to indicate a recipient. Returns whether the
709
     * recipient was accepted or not.
710
     *
711 2
     * @param string $to Recipient (email address).
712 2
     *
713 2
     * @return bool Whether the address was accepted or not.
714 2
     *
715 2
     * @throws NoMailFromException
716 2
     */
717
    protected function rcpt($to)
718
    {
719 2
        // Need to have issued MAIL FROM first
720
        if (!$this->state['mail']) {
721
            throw new NoMailFromException('Need MAIL FROM before RCPT TO');
722
        }
723
724
        $valid          = false;
725
        $expected_codes = [
726 3
            self::SMTP_GENERIC_SUCCESS,
727
            self::SMTP_USER_NOT_LOCAL
728 3
        ];
729
730
        if ($this->greylisted_considered_valid) {
731
            $expected_codes = array_merge($expected_codes, $this->greylisted);
732
        }
733
734
        // Issue RCPT TO, 5 minute timeout
735 3
        try {
736 3
            $this->send('RCPT TO:<' . $to . '>');
737 3
            // Handle response
738 3
            try {
739 3
                $this->expect($expected_codes, $this->command_timeouts['rcpt']);
740 3
                $this->state['rcpt'] = true;
741
                $valid               = true;
742 3
            } catch (UnexpectedResponseException $e) {
743 3
                $this->debug('Unexpected response to RCPT TO: ' . $e->getMessage());
744
            }
745
        } catch (Exception $e) {
746
            $this->debug('Sending RCPT TO failed: ' . $e->getMessage());
747
        }
748
749
        return $valid;
750
    }
751
752
    /**
753
     * Sends a RSET command and resets certain parts of internal state.
754 5
     *
755
     * @throws NoConnectionException
756
     * @throws SendFailedException
757 5
     * @throws TimeoutException
758
     * @throws UnexpectedResponseException
759 4
     */
760
    protected function rset()
761 4
    {
762
        $this->send('RSET');
763
764 4
        // MS ESMTP doesn't follow RFC according to ZF tracker, see [ZF-1377]
765
        $expected = [
766
            self::SMTP_GENERIC_SUCCESS,
767
            self::SMTP_CONNECT_SUCCESS,
768 4
            self::SMTP_NOT_IMPLEMENTED,
769
            // hotmail returns this o_O
770
            self::SMTP_TRANSACTION_FAILED
771
        ];
772
        $this->expect($expected, $this->command_timeouts['rset'], true);
773
        $this->state['mail'] = false;
774
        $this->state['rcpt'] = false;
775
    }
776
777
    /**
778
     * Sends a QUIT command.
779
     *
780
     * @throws NoConnectionException
781
     * @throws SendFailedException
782 5
     * @throws TimeoutException
783
     * @throws UnexpectedResponseException
784
     */
785 5
    protected function quit()
786
    {
787
        // Although RFC says QUIT can be issued at any time, we won't
788 5
        if ($this->state['helo']) {
789 5
            $this->send('QUIT');
790
            $this->expect(
791
                [self::SMTP_GENERIC_SUCCESS,self::SMTP_QUIT_SUCCESS],
792
                $this->command_timeouts['quit'],
793 5
                true
794 5
            );
795
        }
796
    }
797 5
798 5
    /**
799
     * Sends a NOOP command.
800
     *
801
     * @throws NoConnectionException
802
     * @throws SendFailedException
803 5
     * @throws TimeoutException
804 1
     * @throws UnexpectedResponseException
805
     */
806
    protected function noop()
807 4
    {
808
        // Bail if NOOPs are not to be sent.
809
        if (!$this->send_noops) {
810
            return;
811
        }
812
813
        $this->send('NOOP');
814
815
        /**
816
         * The `SMTP` string is here to fix issues with some bad RFC implementations.
817
         * Found at least 1 server replying to NOOP without any code.
818
         */
819
        $expected_codes = [
820
            'SMTP',
821 5
            self::SMTP_BAD_SEQUENCE,
822
            self::SMTP_NOT_IMPLEMENTED,
823 5
            self::SMTP_GENERIC_SUCCESS,
824 5
            self::SMTP_SYNTAX_ERROR,
825
            self::SMTP_CONNECT_SUCCESS
826
        ];
827 5
        $this->expect($expected_codes, $this->command_timeouts['noop'], true);
828 5
    }
829
830
    /**
831 5
     * Sends a command to the remote host.
832 4
     *
833 4
     * @param string $cmd The command to send.
834 4
     *
835 4
     * @return int|bool Number of bytes written to the stream.
836
     *
837 4
     * @throws NoConnectionException
838
     * @throws SendFailedException
839 4
     */
840 4
    protected function send($cmd)
841 4
    {
842
        // Must be connected
843 2
        $this->throwIfNotConnected();
844
845
        $this->debug('send>>>: ' . $cmd);
846
        // Write the cmd to the connection stream
847
        $result = fwrite($this->socket, $cmd . self::CRLF);
848 1
849 1
        // Did it work?
850
        if (false === $result) {
851
            throw new SendFailedException('Send failed on: ' . $this->host);
852 5
        }
853
854
        return $result;
855
    }
856
857
    /**
858
     * Receives a response line from the remote host.
859
     *
860
     * @param int $timeout Timeout in seconds.
861
     *
862
     * @return string Response line from the remote host.
863 11
     *
864
     * @throws NoConnectionException
865 11
     * @throws TimeoutException
866 11
     * @throws NoResponseException
867 11
     */
868
    protected function recv($timeout = null)
869 11
    {
870
        // Must be connected
871
        $this->throwIfNotConnected();
872
873
        // Has a custom timeout been specified?
874
        if (null !== $timeout) {
875
            stream_set_timeout($this->socket, $timeout);
876
        }
877
878
        // Retrieve response
879 10
        $line = fgets($this->socket, 1024);
880
        $this->debug('<<<recv: ' . $line);
881 10
882 9
        // Have we timed out?
883
        $info = stream_get_meta_data($this->socket);
884
        if (!empty($info['timed_out'])) {
885 10
            throw new TimeoutException('Timed out in recv');
886
        }
887 10
888 10
        // Did we actually receive anything?
889 10
        if (false === $line) {
890 10
            throw new NoResponseException('No response in recv');
891
        }
892 10
893
        return $line;
894 10
    }
895
896
    /**
897
     * @param int|int[]|array|string $codes List of one or more expected response codes.
898
     * @param int|null $timeout The timeout for this individual command, if any.
899
     * @param bool $empty_response_allowed When true, empty responses are allowed.
900
     *
901
     * @return string The last text message received.
902
     *
903 10
     * @throws NoConnectionException
904
     * @throws SendFailedException
905 10
     * @throws TimeoutException
906 10
     * @throws UnexpectedResponseException
907 10
     */
908 10
    protected function expect($codes, $timeout = null, $empty_response_allowed = false)
909
    {
910
        if (!is_array($codes)) {
911
            $codes = (array) $codes;
912
        }
913
914
        $code = null;
915
        $text = '';
916 9
917
        try {
918 9
            $line = $this->recv($timeout);
919 9
            $text = $line;
920 9
            while (preg_match('/^[0-9]+-/', $line)) {
921
                $line  = $this->recv($timeout);
922 9
                $text .= $line;
923
            }
924
            sscanf($line, '%d%s', $code, $text);
925
            // TODO/FIXME: This is terrible to read/comprehend
926
            if ($code == self::SMTP_SERVICE_UNAVAILABLE ||
927
                (false === $empty_response_allowed && (null === $code || !in_array($code, $codes)))) {
928
                throw new UnexpectedResponseException($line);
929
            }
930
        } catch (NoResponseException $e) {
931 5
            /**
932
             * No response in expect() probably means that the remote server
933 5
             * forcibly closed the connection so lets clean up on our end as well?
934 1
             */
935
            $this->debug('No response in expect(): ' . $e->getMessage());
936 5
            $this->disconnect(false);
937
        }
938
939
        return $text;
940
    }
941
942
    /**
943
     * Splits the email address string into its respective user and domain parts
944
     * and returns those as an array.
945
     *
946 9
     * @param string $email Email address.
947
     *
948 9
     * @return array ['user', 'domain']
949 9
     */
950 9
    protected function splitEmail($email)
951 1
    {
952
        $parts  = explode('@', $email);
953
        $domain = array_pop($parts);
954 1
        $user   = implode('@', $parts);
955
956 9
        return [$user, $domain];
957
    }
958
959
    /**
960
     * Sets the email addresses that should be validated.
961
     *
962
     * @param array|string $emails List of email addresses (or a single one a string).
963
     */
964
    public function setEmails($emails)
965 9
    {
966
        if (!is_array($emails)) {
967 9
            $emails = (array) $emails;
968 9
        }
969
970
        $this->domains = [];
971
972
        foreach ($emails as $email) {
973
            list($user, $domain) = $this->splitEmail($email);
974
            if (!isset($this->domains[$domain])) {
975
                $this->domains[$domain] = [];
976
            }
977 9
            $this->domains[$domain][] = $user;
978
        }
979 9
    }
980 9
981
    /**
982 9
     * Sets the email address to use as the sender/validator.
983
     *
984
     * @param string $email
985
     */
986
    public function setSender($email)
987
    {
988
        $parts             = $this->splitEmail($email);
989
        $this->from_user   = $parts[0];
990 4
        $this->from_domain = $parts[1];
991
    }
992 4
993
    /**
994
     * Queries the DNS server for MX entries of a certain domain.
995
     *
996
     * @param string $domain The domain for which to retrieve MX records.
997
     *
998
     * @return array MX hosts and their weights.
999
     */
1000 1
    protected function mxQuery($domain)
1001
    {
1002 1
        $hosts  = [];
1003 1
        $weight = [];
1004
        getmxrr($domain, $hosts, $weight);
1005
1006
        return [$hosts, $weight];
1007
    }
1008
1009
    /**
1010
     * Throws if not currently connected.
1011
     *
1012
     * @throws NoConnectionException
1013 2
     */
1014
    private function throwIfNotConnected()
1015 2
    {
1016 2
        if (!$this->connected()) {
1017 2
            throw new NoConnectionException('No connection');
1018
        }
1019 1
    }
1020
1021
    /**
1022
     * Debug helper. If it detects a CLI env, it just dumps given `$str` on a
1023
     * new line, otherwise it prints stuff <pre>.
1024
     *
1025
     * @param string $str
1026
     */
1027
    private function debug($str)
1028
    {
1029
        $str = $this->stamp($str);
1030 7
        $this->log($str);
1031
        if ($this->debug) {
1032 7
            if ('cli' !== PHP_SAPI) {
1033 7
                $str = '<br/><pre>' . htmlspecialchars($str) . '</pre>';
1034
            }
1035
            echo "\n" . $str;
1036
        }
1037
    }
1038
1039
    /**
1040 1
     * Adds a message to the log array
1041
     *
1042 1
     * @param string $msg
1043
     */
1044
    private function log($msg)
1045
    {
1046
        $this->log[] = $msg;
1047
    }
1048
1049
    /**
1050
     * Prepends the given $msg with the current date and time inside square brackets.
1051
     *
1052 6
     * @param string $msg
1053
     *
1054 6
     * @return string
1055 6
     */
1056
    private function stamp($msg)
1057
    {
1058
        $date = \DateTime::createFromFormat('U.u', sprintf('%.f', microtime(true)))->format('Y-m-d\TH:i:s.uO');
1059
        $line = '[' . $date . '] ' . $msg;
1060
1061
        return $line;
1062 1
    }
1063
1064 1
    /**
1065
     * Returns the log array.
1066
     *
1067
     * @return array
1068
     */
1069
    public function getLog()
1070
    {
1071
        return $this->log;
1072 3
    }
1073
1074 3
    /**
1075 3
     * Truncates the log array.
1076
     */
1077
    public function clearLog()
1078
    {
1079
        $this->log = [];
1080
    }
1081
1082 1
    /**
1083
     * Compat for old lower_cased method calls.
1084 1
     *
1085 1
     * @param string $name
1086
     * @param array  $args
1087
     *
1088
     * @return mixed
1089
     */
1090
    public function __call($name, $args)
1091
    {
1092 1
        $camelized = self::camelize($name);
1093
        if (\method_exists($this, $camelized)) {
1094 1
            return \call_user_func_array([$this, $camelized], $args);
1095
        } else {
1096
            trigger_error('Fatal error: Call to undefined method ' . self::class . '::' . $name . '()', E_USER_ERROR);
1097
        }
1098
    }
1099
1100
    /**
1101
     * Set the desired connect timeout.
1102
     *
1103
     * @param int $timeout Connect timeout in seconds.
1104 2
     */
1105
    public function setConnectTimeout($timeout)
1106 2
    {
1107 2
        $this->connect_timeout = (int) $timeout;
1108
    }
1109
1110
    /**
1111
     * Get the current connect timeout.
1112
     *
1113
     * @return int
1114 1
     */
1115
    public function getConnectTimeout()
1116 1
    {
1117
        return $this->connect_timeout;
1118
    }
1119
1120
    /**
1121
     * Set connect port.
1122
     *
1123
     * @param int $port
1124
     */
1125
    public function setConnectPort($port)
1126 2
    {
1127
        $this->connect_port = (int) $port;
1128 2
    }
1129 2
1130 2
    /**
1131 2
     * Get current connect port.
1132 2
     *
1133
     * @return int
1134
     */
1135 2
    public function getConnectPort()
1136
    {
1137
        return $this->connect_port;
1138
    }
1139
1140
    /**
1141
     * Turn on "catch-all" detection.
1142
     */
1143
    public function enableCatchAllTest()
1144
    {
1145
        $this->catchall_test = true;
1146
    }
1147
1148
    /**
1149
     * Turn off "catch-all" detection.
1150
     */
1151
    public function disableCatchAllTest()
1152
    {
1153
        $this->catchall_test = false;
1154
    }
1155
1156
    /**
1157
     * Returns whether "catch-all" test is to be performed or not.
1158
     *
1159
     * @return bool
1160
     */
1161
    public function isCatchAllEnabled()
1162
    {
1163
        return $this->catchall_test;
1164
    }
1165
1166
    /**
1167
     * Set whether "catch-all" results are considered valid or not.
1168
     *
1169
     * @param bool $flag When true, "catch-all" accounts are considered valid
1170
     */
1171
    public function setCatchAllValidity($flag)
1172
    {
1173
        $this->catchall_is_valid = (bool) $flag;
1174
    }
1175
1176
    /**
1177
     * Get current state of "catch-all" validity flag.
1178
     *
1179
     * @return bool
1180
     */
1181
    public function getCatchAllValidity()
1182
    {
1183
        return $this->catchall_is_valid;
1184
    }
1185
1186
    /**
1187
     * Turn off sending NOOP commands.
1188
     */
1189
    public function sendNoops($val)
1190
    {
1191
        $this->send_noops = (bool) $val;
1192
    }
1193
1194
    /**
1195
     * @return bool
1196
     */
1197
    public function sendingNoops()
1198
    {
1199
        return $this->send_noops;
1200
    }
1201
1202
    /**
1203
     * Camelizes a string.
1204
     *
1205
     * @param string $id String to camelize.
1206
     *
1207
     * @return string
1208
     */
1209
    private static function camelize($id)
1210
    {
1211
        return strtr(
1212
            ucwords(
1213
                strtr(
1214
                    $id,
1215
                    ['_' => ' ', '.' => '_ ', '\\' => '_ ']
1216
                )
1217
            ),
1218
            [' ' => '']
1219
        );
1220
    }
1221
}
1222