23 if (!class_exists(
'MIME4')) require_once
'MIME4.php';
35 function _cres(
$conn = null, &$resp, $code1 = null, $code2 = null, $debug = null) {
38 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
39 if (!(is_int($code1) && $code1 > 99 && $code1 < 1000)) $err[] =
'invalid 1 code value';
41 if (!(is_int($code2) && $code2 > 99 && $code2 < 1000)) $err[] =
'invalid 2 code value';
43 if (count($err) > 0)
return FUNC4::trace($debug, implode(
', ', $err), 1);
48 if ($result = fgets(
$conn, $_smtp->BLEN)) {
50 $rescode = substr($result, 0, 3);
51 if (!($rescode == $code1 || $rescode == $code2)) {
56 $resp[] =
'can not read';
60 }
while ($result[3] ==
'-');
65 function mxconnect($host = null, $port = null, $tout = null, $name = null, $context = null, $debug = null) {
69 if (!is_string($host))
FUNC4::trace($debug,
'invalid host type');
71 $host = strtolower(trim($host));
77 foreach ($arr as $mx) {
78 if ($con =
SMTP4::connect($mx, $port, null, null, null, $tout, $name, $context, null, $debug))
break;
81 if (!$con) $con =
SMTP4::connect($host, $port, null, null, null, $tout, $name, $context, null, $debug);
85 function connect($host = null, $port = null, $user = null, $pass = null, $vssl = null, $tout = null, $name = null, $context = null, $login = null, $debug = null) {
88 $_RESULT = $err = array();
90 if ($port == null) $port = $_smtp->PORT;
91 if ($tout == null) $tout = $_smtp->TOUT;
92 if (!is_string($host)) $err[] =
'invalid host type';
94 $host = strtolower(trim($host));
97 if (!(is_int($port) && $port > 0)) $err[] =
'invalid port value';
99 if (!is_string($user)) $err[] =
'invalid username type';
100 else if (($user =
FUNC4::str_clear($user)) ==
'') $err[] =
'invalid username value';
103 if (!is_string($pass)) $err[] =
'invalid password type';
104 else if (($pass =
FUNC4::str_clear($pass)) ==
'') $err[] =
'invalid password value';
106 if (($user != null && $pass == null) || ($user == null && $pass != null)) $err[] =
'invalid username/password combination';
108 if (!is_string($vssl)) $err[] =
'invalid ssl version type';
110 $vssl = strtolower($vssl);
111 if (!($vssl ==
'tls' || $vssl ==
'ssl' || $vssl ==
'sslv2' || $vssl ==
'sslv3')) $err[] =
'invalid ssl version value';
114 if (!(is_int($tout) && $tout > 0)) $err[] =
'invalid timeout value';
116 if (!is_string($name)) $err[] =
'invalid name type';
118 $name = strtolower(trim($name));
121 }
else $name =
'127.0.0.1';
122 if ($context != null && !is_resource($context)) $err[] =
'invalid context type';
123 if ($login != null) {
124 $login = strtolower(trim($login));
125 if (!($login ==
'login' || $login ==
'plain' || $login ==
'cram-md5')) $err[] =
'invalid authentication type value';
127 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
130 $prt = ($vssl == null) ?
'tcp' : $vssl;
131 $conn = ($context == null) ? fsockopen($prt.
'://'.$host, $port, $errno, $errstr, $tout) : fsockopen($prt.
'://'.$host, $port, $errno, $errstr, $tout, $context);
132 if (!
$conn) $_RESULT[101] = $errstr;
133 else if (!socket_set_timeout(
$conn, $_smtp->COUT)) $_RESULT[102] =
'could not set socket timeout';
139 if ($user == null) $ret =
true;
140 else if ($login != null) $ret =
SMTP4::auth(
$conn, $user, $pass, $login, $debug);
142 list($code, $arr) = each($_RESULT);
143 $auth[
'default'] = $auth[
'login'] = $auth[
'plain'] = $auth[
'cram-md5'] =
false;
144 foreach ($arr as $line) {
145 if (substr($line, 0, strlen(
'250-AUTH ')) ==
'250-AUTH ') {
146 foreach (explode(
' ', substr($line, strlen(
'250-AUTH '))) as $type) {
147 $type = strtolower(trim($type));
148 if ($type ==
'login' || $type ==
'plain' || $type ==
'cram-md5') $auth[$type] =
true;
150 }
else if (substr($line, 0, strlen(
'250 AUTH=')) ==
'250 AUTH=') {
151 $expl = explode(
' ', strtolower(trim(substr($line, strlen(
'250 AUTH=')))), 2);
152 if (
$expl[0] ==
'login' ||
$expl[0] ==
'plain' ||
$expl[0] ==
'cram-md5') $auth[
'default'] =
$expl[0];
155 if ($auth[
'default']) $ret =
SMTP4::auth(
$conn, $user, $pass, $auth[
'default'], $debug);
156 if (!$ret && $auth[
'login'] && $auth[
'default'] !=
'login') $ret =
SMTP4::auth(
$conn, $user, $pass,
'login', $debug);
157 if (!$ret && $auth[
'plain'] && $auth[
'default'] !=
'plain') $ret =
SMTP4::auth(
$conn, $user, $pass,
'plain', $debug);
158 if (!$ret && $auth[
'cram-md5'] && $auth[
'default'] !=
'cram-md5') $ret =
SMTP4::auth(
$conn, $user, $pass,
'cram-md5', $debug);
159 if (!$ret && !$auth[
'login'] && $auth[
'default'] !=
'login') $ret =
SMTP4::auth(
$conn, $user, $pass,
'login', $debug);
160 if (!$ret && !$auth[
'plain'] && $auth[
'default'] !=
'plain') $ret =
SMTP4::auth(
$conn, $user, $pass,
'plain', $debug);
161 if (!$ret && !$auth[
'cram-md5'] && $auth[
'default'] !=
'cram-md5') $ret =
SMTP4::auth(
$conn, $user, $pass,
'cram-md5', $debug);
176 $_RESULT = $err = array();
177 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
178 if (!is_array($addrs)) $err[] =
'invalid to address type';
181 if (count($addrs) > 0) {
182 foreach ($addrs as $addr) {
188 }
else $aver =
false;
189 if (!$aver) $err[] =
'invalid to address value';
191 if (!is_string(
$mess)) $err[] =
'invalid message value';
193 $from = @ini_get(
'sendmail_from');
196 if (!is_string(
$from)) $err[] =
'invalid from address type';
199 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
204 foreach ($addrs as $dest) {
222 if (!is_resource(
$conn))
return FUNC4::trace($debug,
'invalid resource connection', 1);
225 if (!fwrite(
$conn,
'QUIT'.$_smtp->CRLF)) $_RESULT[300] =
'can not write';
226 else $_RESULT[301] =
'Send QUIT';
227 return @fclose(
$conn);
238 else if (!fwrite(
$conn,
'QUIT'.$_smtp->CRLF)) $_RESULT[302] =
'can not write';
240 $_RESULT[303] = ($vget = @fgets(
$conn, $_smtp->BLEN)) ? $vget :
'can not read';
246 function helo(
$conn = null, $host = null, $debug = null) {
249 $_RESULT = $err = array();
250 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
251 if (!is_string($host)) $err[] =
'invalid host type';
253 $host = strtolower(trim($host));
256 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
260 if (!fwrite(
$conn,
'HELO '.$host.$_smtp->CRLF)) $_RESULT[304] =
'can not write';
263 $_RESULT[306] = $resp;
270 function ehlo(
$conn = null, $host = null, $debug = null) {
273 $_RESULT = $err = array();
274 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
275 if (!is_string($host)) $err[] =
'invalid host type';
277 $host = strtolower(trim($host));
280 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
284 if (!fwrite(
$conn,
'EHLO '.$host.$_smtp->CRLF)) $_RESULT[307] =
'can not write';
287 $_RESULT[309] = $resp;
294 function auth(
$conn = null, $user = null, $pass = null, $type = null, $debug = null) {
297 $_RESULT = $err = array();
298 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
299 if (!is_string($user)) $err[] =
'invalid username type';
300 else if (($user =
FUNC4::str_clear($user)) ==
'') $err[] =
'invalid username value';
301 if (!is_string($pass)) $err[] =
'invalid password type';
302 else if (($pass =
FUNC4::str_clear($pass)) ==
'') $err[] =
'invalid password value';
303 if ($type == null) $type =
'login';
304 if (!is_string($type)) $err[] =
'invalid authentication type';
306 $type = strtolower(trim($type));
307 if (!($type ==
'login' || $type ==
'plain' || $type ==
'cram-md5')) $err[] =
'invalid authentication type value';
309 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
313 if ($type ==
'login') {
314 if (!fwrite(
$conn,
'AUTH LOGIN'.$_smtp->CRLF)) $_RESULT[310] =
'can not write';
316 else if (!fwrite(
$conn, base64_encode($user).$_smtp->CRLF)) $_RESULT[312] =
'can not write';
318 else if (!fwrite(
$conn, base64_encode($pass).$_smtp->CRLF)) $_RESULT[314] =
'can not write';
321 $_RESULT[316] = $resp;
324 }
else if ($type ==
'plain') {
325 if (!fwrite(
$conn,
'AUTH PLAIN '.base64_encode($user.chr(0).$user.chr(0).$pass).$_smtp->CRLF)) $_RESULT[317] =
'can not write';
328 $_RESULT[319] = $resp;
331 }
else if ($type ==
'cram-md5') {
332 if (!fwrite(
$conn,
'AUTH CRAM-MD5'.$_smtp->CRLF)) $_RESULT[200] =
'can not write';
335 if (strlen($pass) > 64) $pass = pack(
'H32', md5($pass));
336 if (strlen($pass) < 64) $pass = str_pad($pass, 64, chr(0));
337 $pad1 = substr($pass, 0, 64) ^ str_repeat(chr(0x36), 64);
338 $pad2 = substr($pass, 0, 64) ^ str_repeat(chr(0x5C), 64);
339 $chal = substr($resp[count($resp)-1], 4);
340 $innr = pack(
'H32', md5($pad1.base64_decode($chal)));
341 if (!fwrite(
$conn, base64_encode($user.
' '.md5($pad2.$innr)).$_smtp->CRLF)) $_RESULT[202] =
'can not write';
344 $_RESULT[204] = $resp;
353 function from(
$conn = null, $addr = null, $debug = null) {
356 $_RESULT = $err = array();
357 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
358 if (!is_string($addr)) $err[] =
'invalid from address type';
359 else if (!($addr !=
'' &&
FUNC4::is_mail($addr))) $err[] =
'invalid from address value';
360 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
364 if (!fwrite(
$conn,
'MAIL FROM:<'.$addr.
'>'.$_smtp->CRLF)) $_RESULT[320] =
'can not write';
367 $_RESULT[322] = $resp;
374 function to(
$conn = null, $addr = null, $debug = null) {
377 $_RESULT = $err = array();
378 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
379 if (!is_string($addr)) $err[] =
'invalid to address type';
380 else if (!($addr !=
'' &&
FUNC4::is_mail($addr))) $err[] =
'invalid to address value';
381 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
385 if (!fwrite(
$conn,
'RCPT TO:<'.$addr.
'>'.$_smtp->CRLF)) $_RESULT[323] =
'can not write';
388 $_RESULT[325] = $resp;
398 $_RESULT = $err = array();
399 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
400 if (!(is_string(
$mess) &&
$mess !=
'')) $err[] =
'invalid message value';
401 if (count($err) > 0)
FUNC4::trace($debug, implode(
', ', $err));
405 if (!fwrite(
$conn,
'DATA'.$_smtp->CRLF)) $_RESULT[326] =
'can not write';
409 foreach (explode($_smtp->CRLF,
$mess) as $line) {
410 if ($line !=
'' && $line[0] ==
'.') $line =
'.'.$line;
411 if (!fwrite(
$conn, $line.$_smtp->CRLF)) {
412 $_RESULT[328] =
'can not write';
418 if (!fwrite(
$conn,
'.'.$_smtp->CRLF)) $_RESULT[329] =
'can not write';
421 $_RESULT[331] = $resp;
437 else if (!fwrite(
$conn,
'RSET'.$_smtp->CRLF)) $_RESULT[332] =
'can not write';
440 $_RESULT[334] = $resp;
446 function recv(
$conn = null, $code1 = null, $code2 = null, $debug = null) {
453 $_RESULT[336] = $resp;