Completed
Push — master ( e090e4...18d79e )
by Damian
02:26
created

code/checks/SMTPConnectCheck.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Checks if the SMTP connection configured through PHP.ini works as expected.
5
 *
6
 * Only checks socket connection with HELO command, not actually sending the email.
7
 */
8
class SMTPConnectCheck implements EnvironmentCheck {
0 ignored issues
show
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
9
	/**
10
	 * @var string
11
	 */
12
	protected $host;
13
14
	/**
15
	 * @var int
16
	 */
17
	protected $port;
18
19
	/**
20
	 * Timeout (in seconds).
21
	 *
22
	 * @var int
23
	 */
24
	protected $timeout;
25
26
	/**
27
	 * @param null|string $host
28
	 * @param null|int $port
29
	 * @param int $timeout
30
	 */
31
	function __construct($host = null, $port = null, $timeout = 15) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
		$this->host = ($host) ? $host : ini_get('SMTP');
33
		if(!$this->host) $this->host = 'localhost';
34
		
35
		$this->port = ($port) ? $port : ini_get('smtp_port');
36
		if(!$this->port) $this->port = 25;
37
38
		$this->timeout = $timeout;
39
	}
40
41
	/**
42
	 * @inheritdoc
43
	 *
44
	 * @return array
45
	 */
46
	function check() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
		$f = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
48
		if(!$f) {
49
			return array(
50
				EnvironmentCheck::ERROR, 
51
				sprintf("Couldn't connect to SMTP on %s:%s (Error: %s %s)", $this->host, $this->port, $errno, $errstr)
52
			);
53
		}
54
55
		fwrite($f, "HELO its_me\r\n");
56
		$response = fread($f, 26);
57
		if(substr($response, 0, 3) != '220') {
58
			return array(
59
				EnvironmentCheck::ERROR,
60
				sprintf("Invalid mail server response: %s", $response)
61
			);
62
		}
63
64
		return array(EnvironmentCheck::OK, '');
65
	}
66
}
67