1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sendmail package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2010, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Sendmail\Sender\Smtp; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SMTP dialogue. |
14
|
|
|
* |
15
|
|
|
* @author Peter Gribanov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Dialogue |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Dialogue text. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $log = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SMTP server connection. |
28
|
|
|
* |
29
|
|
|
* @var resource |
30
|
|
|
*/ |
31
|
|
|
protected $connect; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct. |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
* |
38
|
|
|
* @param string $server |
39
|
|
|
* @param int $port |
40
|
|
|
* @param int $timeout |
41
|
|
|
*/ |
42
|
|
|
public function __construct($server, $port, $timeout = -1) |
43
|
|
|
{ |
44
|
|
|
$errno = 0; |
45
|
|
|
$errstr = ''; |
46
|
|
|
if ($timeout > 0) { |
47
|
|
|
$this->connect = fsockopen($server, $port, $errno, $errstr, $timeout); |
48
|
|
|
} else { |
49
|
|
|
$this->connect = fsockopen($server, $port, $errno, $errstr); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!is_resource($this->connect)) { |
53
|
|
|
if ($errno === 0 || !$errstr) { |
54
|
|
|
$errstr = 'Failed connect to: '.$server.':'.$port; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new \Exception($errstr, $errno); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// saving welcome message |
61
|
|
|
$this->log = fgets($this->connect, 4096); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sends the request and returns a response. |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
* |
69
|
|
|
* @param string $request |
70
|
|
|
* @param bool $verify |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function call($request, $verify = false) |
75
|
|
|
{ |
76
|
|
|
fwrite($this->connect, $request."\r\n"); |
77
|
|
|
|
78
|
|
|
$response = fread($this->connect, 4096); |
79
|
|
|
$this->log .= $request."\r\n".$response; |
80
|
|
|
|
81
|
|
|
if ($verify && $response[0] != 2) { |
82
|
|
|
throw new Exception($this->end()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $response[0] != 2; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get dialogue text. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getLog() |
94
|
|
|
{ |
95
|
|
|
return $this->log; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* End dialogue. |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function end() |
104
|
|
|
{ |
105
|
|
|
fclose($this->connect); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Destruct. |
112
|
|
|
*/ |
113
|
|
|
public function __destruct() |
114
|
|
|
{ |
115
|
|
|
$this->end(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|