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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
<?php
# CATS Online Registration System
# Copyright (C) 2004 Adam Beaumont, Thomas Cort, Patrick McLean, Scott Stoddard
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# This file contains a PHP SMTP client
class cats_smtp {
var $connection;
var $host;
var $port;
function cats_smtp() {
global $smtp_host;
global $smtp_port;
if (!isset($smtp_host)) {
$this->host = "localhost"; # try the localhost
} else {
$this->host = $smtp_host;
}
if (!isset($smtp_port)) {
$this->port = 25; # use default smtp port
} else {
$this->port = $smtp_port;
}
$this->connection = 0;
}
function connect() {
$this->connection = fsockopen($this->host,$this->port);
if (!$this->connection) {
$r = new return_result(false);
$r->add_message("No Connection\n");
return $r;
}
$result = $this->get_sock_data();
if (strncmp($result,"220",3) != 0) {
$r = new return_result(false);
$r->add_message("Error connecting\n$result\n");
return $r;
}
return new return_result(true);
}
function disconnect() {
fclose($this->connection);
$this->connection = 0;
}
# read as much data from the socket as possible
function get_sock_data() {
$data = "";
while($str = fgets($this->connection,515)) {
$data .= $str;
if(substr($str,3,1) == " ") {
break;
}
}
return $data;
}
# write data to a socket and append "\r\n"
function put_sock_data($data) {
fwrite($this->connection,$data . "\r\n");
}
# SMTP Commands
function cmd_helo() {
$this->put_sock_data("ehlo " . $this->host);
$result = $this->get_sock_data();
if (strncmp($result,"250",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with HELO command\n$result\n");
return $r;
}
return new return_result(true);
}
function cmd_auth() {
global $smtp_user, $smtp_pass;
$coded_pass = base64_encode("$smtp_user\0$smtp_user\0$smtp_pass");
$this->put_sock_data("AUTH PLAIN " . $coded_pass);
$result = $this->get_sock_data();
if (strncmp($result,"235",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with AUTH command\n$result\n");
return $r;
}
return new return_result(true);
}
function cmd_quit() {
$this->put_sock_data("quit");
$result = $this->get_sock_data();
if (strncmp($result,"221",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with QUIT command\n$result\n");
return $r;
}
return new return_result(true);
}
function cmd_mail_from($addr) {
$this->put_sock_data("mail from: $addr");
$result = $this->get_sock_data();
if (strncmp($result,"250",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with MAIL FROM: $addr command\n$result\n");
return $r;
}
return new return_result(true);
}
function cmd_rcpt_to($addr) {
$this->put_sock_data("rcpt to: $addr");
$result = $this->get_sock_data();
if (strncmp($result,"250",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with RCPT TO: $addr command\n$result\n");
return $r;
}
return new return_result(true);
}
function cmd_data($from,$to,$subject,$data) {
$this->put_sock_data("DATA");
$result = $this->get_sock_data();
if (strncmp($result,"354",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with DATA command\n$result\n");
return $r;
}
$this->put_sock_data("From: $from");
$this->put_sock_data("To: $to");
$this->put_sock_data("Subject: $subject");
$this->put_sock_data($data);
$this->put_sock_data(".");
$result = $this->get_sock_data();
if (strncmp($result,"250",3) != 0) {
$r = new return_result(false);
$r->add_message("Error with DATA command\n$result\n");
return $r;
}
return new return_result(true);
}
}
?>
|