📄 class.smtp.php
字号:
<?
class smtp
{
var $SMTP_PORT = 25;
var $CRLF = '
';
var $do_debug = null;
var $smtp_conn = null;
var $error = null;
var $helo_rply = null;
function smtp ()
{
$this->smtp_conn = 0;
$this->error = null;
$this->helo_rply = null;
$this->do_debug = 0;
}
function connect ($host, $port = 0, $tval = 30)
{
$this->error = null;
if ($this->connected ())
{
$this->error = array ('error' => 'Already connected to a server');
return false;
}
if (empty ($port))
{
$port = $this->SMTP_PORT;
}
$this->smtp_conn = fsockopen ($host, $port, $errno, $errstr, $tval);
if (empty ($this->smtp_conn))
{
$this->error = array ('error' => 'Failed to connect to server', 'errno' => $errno, 'errstr' => $errstr);
if ((1 <= $this->do_debug))
{
echo ((('SMTP -> ERROR: '.$this->error['error']).(((((''.': ').$errstr).' (').$errno).')')).$this->CRLF);
}
return false;
}
if ((substr (PHP_OS, 0, 3) != 'WIN'))
{
socket_set_timeout ($this->smtp_conn, $tval, 0);
}
$announce = $this->get_lines ();
if ((2 <= $this->do_debug))
{
echo (('SMTP -> FROM SERVER:'.$this->CRLF).$announce);
}
return true;
}
function authenticate ($username, $password)
{
fputs ($this->smtp_conn, ('AUTH LOGIN'.$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if (($code != 334))
{
$this->error = array ('error' => 'AUTH not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
fputs ($this->smtp_conn, (base64_encode ($username).$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if (($code != 334))
{
$this->error = array ('error' => 'Username not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
fputs ($this->smtp_conn, (base64_encode ($password).$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if (($code != 235))
{
$this->error = array ('error' => 'Password not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
return true;
}
function connected ()
{
if (!empty ($this->smtp_conn))
{
$sock_status = socket_get_status ($this->smtp_conn);
if ($sock_status['eof'])
{
if ((1 <= $this->do_debug))
{
echo (('SMTP -> NOTICE:'.$this->CRLF).'EOF caught while checking if connected');
}
$this->Close ();
return false;
}
return true;
}
return false;
}
function close ()
{
$this->error = null;
$this->helo_rply = null;
if (!empty ($this->smtp_conn))
{
fclose ($this->smtp_conn);
$this->smtp_conn = 0;
}
}
function data ($msg_data)
{
$this->error = null;
if (!$this->connected ())
{
$this->error = array ('error' => 'Called Data() without being connected');
return false;
}
fputs ($this->smtp_conn, ('DATA'.$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if ((2 <= $this->do_debug))
{
echo (('SMTP -> FROM SERVER:'.$this->CRLF).$rply);
}
if (($code != 354))
{
$this->error = array ('error' => 'DATA command not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
$msg_data = str_replace ('
', '
', $msg_data);
$msg_data = str_replace ('
', '
', $msg_data);
$lines = explode ('
', $msg_data);
$field = substr ($lines[0], 0, strpos ($lines[0], ':'));
$in_headers = false;
if ((!empty ($field) AND !strstr ($field, ' ')))
{
$in_headers = true;
}
$max_line_length = 998;
$line = each ($lines)[1];
if ()
{
$lines_out = null;
if ((($line == '') AND $in_headers))
{
$in_headers = false;
}
while (($max_line_length < strlen ($line)))
{
$pos = strrpos (substr ($line, 0, $max_line_length), ' ');
if (!$pos)
{
$pos = ($max_line_length - 1);
}
$lines_out[] = substr ($line, 0, $pos);
$line = substr ($line, ($pos + 1));
if ($in_headers)
{
$line = (' '.$line);
continue;
}
}
$lines_out[] = $line;
$line_out = each ($lines_out)[1];
if ()
{
if ((0 < strlen ($line_out)))
{
if ((substr ($line_out, 0, 1) == '.'))
{
$line_out = ('.'.$line_out);
}
}
fputs ($this->smtp_conn, ($line_out.$this->CRLF));
}
}
fputs ($this->smtp_conn, (($this->CRLF.'.').$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if ((2 <= $this->do_debug))
{
echo (('SMTP -> FROM SERVER:'.$this->CRLF).$rply);
}
if (($code != 250))
{
$this->error = array ('error' => 'DATA not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
return true;
}
function expand ($name)
{
$this->error = null;
if (!$this->connected ())
{
$this->error = array ('error' => 'Called Expand() without being connected');
return false;
}
fputs ($this->smtp_conn, (('EXPN '.$name).$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if ((2 <= $this->do_debug))
{
echo (('SMTP -> FROM SERVER:'.$this->CRLF).$rply);
}
if (($code != 250))
{
$this->error = array ('error' => 'EXPN not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
$entries = explode ($this->CRLF, $rply);
while (true)
{
$l = each ($entries)[1];
if ()
{
$list[] = substr ($l, 4);
continue;
}
else
{
return $list;
return null;
}
}
}
function hello ($host = '')
{
$this->error = null;
if (!$this->connected ())
{
$this->error = array ('error' => 'Called Hello() without being connected');
return false;
}
if (empty ($host))
{
$host = 'localhost';
}
if (!$this->SendHello ('EHLO', $host))
{
if (!$this->SendHello ('HELO', $host))
{
return false;
}
}
return true;
}
function sendhello ($hello, $host)
{
fputs ($this->smtp_conn, ((($hello.' ').$host).$this->CRLF));
$rply = $this->get_lines ();
$code = substr ($rply, 0, 3);
if ((2 <= $this->do_debug))
{
echo (('SMTP -> FROM SERVER: '.$this->CRLF).$rply);
}
if (($code != 250))
{
$this->error = array ('error' => ($hello.' not accepted from server'), 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
if ((1 <= $this->do_debug))
{
echo (((('SMTP -> ERROR: '.$this->error['error']).': ').$rply).$this->CRLF);
}
return false;
}
$this->helo_rply = $rply;
return true;
}
function help ($keyword = '')
{
$this->error = null;
if (!$this->connected ())
{
$this->error = array ('error' => 'Called Help() without being connected');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -