-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSMTPServer.php
More file actions
155 lines (136 loc) · 4.17 KB
/
SMTPServer.php
File metadata and controls
155 lines (136 loc) · 4.17 KB
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
<?php
/*
phpsmtpd, Fake SMTP server for development purposes
Copyright (C) 2011 Web Power BV, http://www.webpower.nl
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
interface SMTPStore
{
public function sendMail($from, array $to, $message);
}
class SMTPServer extends socketServer
{
private $store;
public function setStore(SMTPStore $store)
{
$this->store = $store;
}
public function sendMail($from, array $to, $message)
{
$this->store->sendMail($from, $to, $message);
}
public function on_accept(socketServerClient $client)
{
$client->setServer($this);
}
}
class SMTPServerClient extends socketServerClient
{
private $server;
private $last_action;
private $hostname;
private $mail_from;
private $mail_to = array();
private $sending_data = false;
private $quit = false;
private $message = array();
public function setServer(smtpServer $server)
{
$this->server = $server;
}
public function on_read()
{
if (strpos($this->read_buffer, "\r\n") !== false) {
$reads = explode("\r\n", $this->read_buffer);
foreach ($reads as $read) {
$this->handleRead($read);
}
}
$this->read_buffer = '';
}
private function handleRead($read)
{
echo 'onread: ' . str_replace(str_split("\r\n"), array('\r', '\n'), $read) . "\n";
$this->last_action = time();
if ($this->sending_data) {
if (trim($read) === '.') {
$this->sending_data = false;
$this->writeClean('250 Ok: queued as 12345');
$this->server->sendMail(
$this->mail_from,
$this->mail_to,
implode("\r\n", $this->message)
);
} else {
$this->message[] = $read;
}
} else if (strtoupper(substr($read, 0, 4)) === 'QUIT') {
$this->quit = true;
$this->writeClean('221 Bye');
} else if (strtoupper(substr($read, 0, 4)) === 'EHLO') {
$this->hostname = substr($read, 5);
$this->writeClean('250-smtp2.example.com Hello '.$this->hostname.' ['.$this->remote_address.']');
$this->writeClean('250-SIZE 14680064');
$this->writeClean('250-PIPELINING');
$this->writeClean('250 HELP');
} else if (strtoupper(substr($read, 0, 4)) === 'HELO') {
$this->hostname = trim(substr($read, 5));
$this->writeClean('250 Hello '.$this->hostname.', I am glad to meet you');
} else if (strtoupper(substr($read, 0, 10)) === 'MAIL FROM:') {
$this->mail_from = substr($read, 10);
$this->writeClean('250 Ok');
} else if (strtoupper(substr($read, 0, 8)) === 'RCPT TO:') {
$this->mail_to[] = substr($read, 8);
$this->writeClean('250 Ok');
} else if (strtoupper(substr($read, 0, 4)) === 'DATA') {
$this->sending_data = true;
$this->writeClean("354 End data with <CR><LF>.<CR><LF>");
}
}
public function write($buffer, $length = 4096)
{
echo 'onwrite: ' . $buffer;
return parent::write($buffer, $length);
}
public function writeClean($buffer, $length = 4096)
{
$this->write($buffer . "\r\n", $length);
}
public function on_connect()
{
$this->last_action = time();
echo "[".__CLASS__."] accepted connection from {$this->remote_address}\n";
$this->write("220 smtp2.example.com ESMTP PHPsmtpd\r\n");
}
public function on_disconnect()
{
echo "[".__CLASS__."] {$this->remote_address} disconnected\n";
}
public function on_write()
{
$this->last_action = time();
if (strlen($this->write_buffer) == 0 && $this->quit) {
$this->disconnected = true;
$this->on_disconnect();
$this->close();
}
}
public function on_timer()
{
$idle_time = time() - $this->last_action;
if ($idle_time > 15) {
echo "[".__CLASS__."] Client timeout exceeded ({$this->remote_address})\n";
$this->close();
}
}
}