📄 class.smtp.php
字号:
}
public function ClearBCCs( )
{
$this->bcc = array( );
}
public function ClearReplyTos( )
{
$this->ReplyTo = array( );
}
public function ClearAllRecipients( )
{
$this->to = array( );
$this->cc = array( );
$this->bcc = array( );
}
public function ClearAttachments( )
{
$this->attachment = array( );
}
public function ClearCustomHeaders( )
{
$this->CustomHeader = array( );
}
public function error_handler( $msg )
{
$this->ErrorAlerts[] = $msg;
if ( $this->MailerDebug )
{
echo "<h3>Mailer Error</h3>Description:<br>";
printf( "<font color=\"FF0000\">%s</font>", $msg );
}
}
public function AddCustomHeader( $custom_header )
{
$this->CustomHeader[] = $custom_header;
}
public 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
{
public $SMTP_PORT = 25;
public $CRLF = "\r\n";
public $smtp_conn = NULL;
public $error = NULL;
public $helo_rply = NULL;
public $do_debug = NULL;
public function SMTP( )
{
$this->smtp_conn = 0;
$this->error = NULL;
$this->helo_rply = NULL;
$this->do_debug = 0;
}
public 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;
}
public 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;
}
public function Close( )
{
$this->error = NULL;
$this->helo_rply = NULL;
if ( !empty( $this->smtp_conn ) )
{
fclose( $this->smtp_conn );
$this->smtp_conn = 0;
}
}
public 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;
}
public 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;
}
public 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;
}
public 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;
}
public 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;
}
public 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;
}
public function get_lines( )
{
$data = "";
while ( $str = fgets( $this->smtp_conn, 515 ) )
{
$data .= $str;
if ( !( substr( $str, 3, 1 ) == " " ) )
{
continue;
}
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;
}
public 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 + -