📄 class.smtp.php
字号:
{
$this->cc = array( );
}
function clearbccs( )
{
$this->bcc = array( );
}
function clearreplytos( )
{
$this->ReplyTo = array( );
}
function clearallrecipients( )
{
$this->to = array( );
$this->cc = array( );
$this->bcc = array( );
}
function clearattachments( )
{
$this->attachment = array( );
}
function clearcustomheaders( )
{
$this->CustomHeader = array( );
}
function error_handler( $msg )
{
$this->ErrorAlerts[] = $msg;
if ( $this->MailerDebug == true )
{
print "<h3>Mailer Error</h3>";
print "Description:<br>";
printf( "<font color=\"FF0000\">%s</font>", $msg );
}
}
function addcustomheader( $custom_header )
{
$this->CustomHeader[] = $custom_header;
}
function usemsmailheaders( )
{
$MSHeader = "";
if ( $this->Priority == 1 )
{
$MSPriority = "High";
}
else if ( $this->Priority == 5 )
{
$MSPriority = "Low";
}
else
{
$MSPriority = "Medium";
}
$MSHeader .= sprintf( "X-MSMail-Priority: %s\r\n", $MSPriority );
$MSHeader .= sprintf( "Importance: %s\r\n", $MSPriority );
return $MSHeader;
}
}
class smtp
{
var $SMTP_PORT = 25;
var $CRLF = "\r\n";
var $smtp_conn;
var $error;
var $helo_rply;
var $do_debug;
function smtp( )
{
$this->smtp_conn = 0;
$this->error = null;
$this->helo_rply = null;
$this->do_debug = 0;
}
function connected( )
{
if ( !empty( $this->smtp_conn ) )
{
$sock_status = socket_get_status( $this->smtp_conn );
if ( $sock_status['eof'] )
{
$this->close( );
return false;
}
return true;
}
return false;
}
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
);
return false;
}
$announce = $this->get_lines( );
return true;
}
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;
}
$this->send_line( "DATA" );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 354 )
{
$this->error = array(
"error" => "DATA command not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
$msg_data = str_replace( "\r\n", "\n", $msg_data );
$msg_data = str_replace( "\r", "\n", $msg_data );
$lines = explode( "\n", $msg_data );
$field = substr( $lines[0], 0, strpos( $lines[0], ":" ) );
$in_headers = false;
if ( !empty( $field ) && !strstr( $field, " " ) )
{
$in_headers = true;
}
$max_line_length = 998;
while ( list( , $line ) = each( $lines ) )
{
$lines_out = null;
if ( $line == "" && $in_headers )
{
$in_headers = false;
}
while ( $max_line_length < strlen( $line ) )
{
$pos = strrpos( substr( $line, 0, $max_line_length ), " " );
$lines_out[] = substr( $line, 0, $pos );
$line = substr( $line, $pos + 1 );
if ( $in_headers )
{
$line = "\t".$line;
}
}
$lines_out[] = $line;
while ( list( , $line_out ) = each( $lines_out ) )
{
if ( $line_out[0] == "." )
{
$line_out = ".".$line_out;
}
$tmpdata .= $line_out.$this->CRLF;
}
}
$this->send_line( $tmpdata.$this->CRLF."." );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 250 )
{
$this->error = array(
"error" => "DATA not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
return true;
}
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";
}
$this->send_line( "HELO ".$host );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 250 )
{
$this->error = array(
"error" => "HELO not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
$this->helo_rply = $rply;
return true;
}
function authhello( $host = "", $user = "", $pass = "" )
{
$this->error = null;
if ( !$this->connected( ) )
{
$this->error = array( "error" => "Called Hello() without being connected" );
return false;
}
if ( empty( $host ) )
{
$host = "localhost";
}
$this->send_line( "EHLO ".$host );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 250 )
{
$this->error = array(
"error" => "EHLO not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
$this->helo_rply = $rply;
$this->send_line( "AUTH LOGIN" );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 334 )
{
$this->error = array(
"error" => "AUTH LOGIN not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
$this->send_line( base64_encode( $user ) );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 334 )
{
$this->error = array(
"error" => "USER not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
$this->send_line( base64_encode( $pass ) );
$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 )
);
return false;
}
return true;
}
function mailfrom( $from )
{
$this->error = null;
if ( !$this->connected( ) )
{
$this->error = array( "error" => "Called Mail() without being connected" );
return false;
}
$this->send_line( "MAIL FROM:".$from );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 250 )
{
$this->error = array(
"error" => "MAIL not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
return true;
}
function quit( $close_on_error = true )
{
$this->error = null;
if ( !$this->connected( ) )
{
$this->error = array( "error" => "Called Quit() without being connected" );
return false;
}
$this->send_line( "QUIT" );
$byemsg = $this->get_lines( );
$rval = true;
$e = null;
$code = substr( $byemsg, 0, 3 );
if ( $code != 221 )
{
$e = array(
"error" => "SMTP server rejected quit command",
"smtp_code" => $code,
"smtp_rply" => substr( $byemsg, 4 )
);
$rval = false;
}
if ( empty( $e ) || $close_on_error )
{
$this->close( );
}
return $rval;
}
function recipient( $to )
{
$this->error = null;
if ( !$this->connected( ) )
{
$this->error = array( "error" => "Called Recipient() without being connected" );
return false;
}
$this->send_line( "RCPT TO:".$to );
$rply = $this->get_lines( );
$code = substr( $rply, 0, 3 );
if ( $code != 250 && $code != 251 )
{
$this->error = array(
"error" => "RCPT not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr( $rply, 4 )
);
return false;
}
return true;
}
function get_lines( )
{
$data = "";
while ( $str = fgets( $this->smtp_conn, 515 ) )
{
$data .= $str;
if ( substr( $str, 3, 1 ) == " " )
{
break;
}
}
if ( $this->do_debug )
{
$tmp = ereg_replace( "(\r|\n)", "", $data );
echo "<font style=\"font-size:12px; font-family: Courier New; background-color: white; color: black;\"><- <b>".htmlspecialchars( $tmp )."</span><br>\r\n";
flush( );
}
return $data;
}
function send_line( $data )
{
fputs( $this->smtp_conn, $data.$this->CRLF );
if ( $this->do_debug )
{
$data = htmlspecialchars( $data );
echo "<font style=\"font-size:12px; font-family: Courier New; background-color: white; color: black;\">-> ".nl2br( $data )."</font><br>\r\n";
flush( );
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -