📄 class.smtp.php
字号:
<?php
class phpmailer
{
public $Priority = 3;
public $CharSet = "iso-8859-1";
public $ContentType = "text/plain";
public $Encoding = "8bit";
public $From = "root@localhost";
public $FromName = "root";
public $Subject = "";
public $Body = "";
public $WordWrap = TRUE;
public $MailerDebug = FALSE;
public $UseMSMailHeaders = TRUE;
public $IPAddress = "unknown";
public $timezone = "+0800";
public $Host = "localhost";
public $Port = 25;
public $Helo = "";
public $Timeout = 10;
public $version = "";
public $to = array( );
public $cc = array( );
public $bcc = array( );
public $ReplyTo = array( );
public $attachment = array( );
public $CustomHeader = array( );
public $boundary = FALSE;
public $ErrorAlerts = array( );
public $blUseAuthLogin = FALSE;
public $AuthUser = "";
public $AuthPass = "";
public function UseAuthLogin( $user, $pass )
{
$this->blUseAuthLogin = TRUE;
$this->AuthUser = $user;
$this->AuthPass = $pass;
}
public function IsHTML( $bool )
{
if ( $bool )
{
$this->ContentType = "text/html";
}
else
{
$this->ContentType = "text/plain";
}
}
public function Start( )
{
global $appname;
global $appversion;
$this->Version = $appname." ".$appversion;
$this->Helo = ereg_replace( "[^A-Za-z0-9]", "", $appname );
}
public function AddAddress( $address, $name = "" )
{
$cur = count( $this->to );
$this->to[$cur][0] = trim( $address );
$this->to[$cur][1] = $name;
}
public function AddCC( $address, $name = "" )
{
$cur = count( $this->cc );
$this->cc[$cur][0] = trim( $address );
$this->cc[$cur][1] = $name;
}
public function AddBCC( $address, $name = "" )
{
$cur = count( $this->bcc );
$this->bcc[$cur][0] = trim( $address );
$this->bcc[$cur][1] = $name;
}
public function AddReplyTo( $address, $name = "" )
{
$cur = count( $this->ReplyTo );
$this->ReplyTo[$cur][0] = trim( $address );
$this->ReplyTo[$cur][1] = $name;
}
public function Send( )
{
global $use_sendmail;
if ( count( $this->to ) + count( $this->cc ) + count( $this->bcc ) == 0 )
{
$this->error_handler( "You must provide at least one recipient email address" );
return FALSE;
}
$header = $this->create_header( );
if ( ( $body = $this->create_body( ) ) === FALSE )
{
return FALSE;
}
if ( $use_sendmail )
{
return FALSE;
}
if ( $this->sendmail_send( $header, $body ) === FALSE && $this->smtp_send( $header, $body ) === FALSE )
{
return FALSE;
}
return sprintf( "%s%s", $header, $body );
}
public function sendmail_send( $header, $body )
{
global $path_to_sendmail;
if ( strtoupper( substr( PHP_OS, 0, 3 ) ) == "WIN" )
{
$this->error_handler( "Sendmail is not supported under Win32 systems" );
return FALSE;
}
$sendmail = sprintf( "%s -t", $path_to_sendmail );
if ( !( $mail = @popen( $sendmail, "w" ) ) )
{
$this->error_handler( sprintf( "Could not execute %s", $path_to_sendmail ) );
return FALSE;
}
fputs( $mail, $header );
fputs( $mail, $body );
pclose( $mail );
return TRUE;
}
public function smtp_send( $header, $body )
{
global $enable_debug;
( );
$smtp = new SMTP( );
$smtp->do_debug = $enable_debug;
$hosts = explode( ";", $this->Host );
$index = 0;
$connection = FALSE;
while ( $index < count( $hosts ) && !$connection )
{
if ( $smtp->Connect( $hosts[$index], $this->Port, $this->Timeout ) )
{
$connection = TRUE;
}
++$index;
}
if ( !$connection )
{
$this->error_handler( "SMTP Error: could not connect to SMTP host server(s)" );
return FALSE;
}
if ( $this->blUseAuthLogin )
{
if ( !$smtp->AuthHello( $this->Helo, $this->AuthUser, $this->AuthPass ) )
{
$this->error_handler( "SMTP Error: Invalid username/password" );
return FALSE;
}
}
else
{
$smtp->Hello( $this->Helo );
}
$smtp->MailFrom( sprintf( "<%s>", $this->From ) );
$i = 0;
for ( ; $i < count( $this->to ); ++$i )
{
if ( $smtp->Recipient( sprintf( "<%s>", $this->to[$i][0] ) ) )
{
continue;
}
$this->error_handler( "SMTP Error: Recipient not accepted. Verify your relay rules" );
return FALSE;
}
$i = 0;
for ( ; $i < count( $this->cc ); ++$i )
{
if ( $smtp->Recipient( sprintf( "<%s>", $this->cc[$i][0] ) ) )
{
continue;
}
$this->error_handler( "SMTP Error: Recipient not accepted. Verify your relay rules" );
return FALSE;
}
$i = 0;
for ( ; $i < count( $this->bcc ); ++$i )
{
if ( $smtp->Recipient( sprintf( "<%s>", $this->bcc[$i][0] ) ) )
{
continue;
}
$this->error_handler( "SMTP Error: Recipient not accepted. Verify your relay rules" );
return FALSE;
}
if ( !$smtp->Data( sprintf( "%s%s", $header, $body ) ) )
{
$this->error_handler( "SMTP Error: Data not accepted" );
return FALSE;
}
$smtp->Quit( );
}
public function addr_append( $type, $addr )
{
$addr_str = "";
if ( trim( $addr[0][1] ) != "" )
{
$addr_str .= sprintf( "%s: \"%s\" <%s>", $type, $addr[0][1], $addr[0][0] );
}
else
{
$addr_str .= sprintf( "%s: %s", $type, $addr[0][0] );
}
if ( 1 < count( $addr ) )
{
$i = 1;
for ( ; $i < count( $addr ); ++$i )
{
if ( trim( $addr[$i][1] ) != "" )
{
$addr_str .= sprintf( ", \r\n\t\"%s\" <%s>", $addr[$i][1], $addr[$i][0] );
}
else
{
$addr_str .= sprintf( ", \r\n\t\"%s\"", $addr[$i][0] );
}
}
$addr_str .= "\r\n";
return $addr_str;
}
$addr_str .= "\r\n";
return $addr_str;
}
public function wordwrap( $message, $length )
{
$line = explode( "\r\n", $message );
$message = "";
$i = 0;
for ( ; $i < count( $line ); ++$i )
{
$line_part = explode( " ", trim( $line[$i] ) );
$buf = "";
$e = 0;
for ( ; $e < count( $line_part ); ++$e )
{
$buf_o = $buf;
if ( $e == 0 )
{
$buf .= $line_part[$e];
}
else
{
$buf .= " ".$line_part[$e];
}
if ( !( $length < strlen( $buf ) ) && !( $buf_o != "" ) )
{
$message .= $buf_o."\r\n";
$buf = $line_part[$e];
}
}
$message .= $buf."\r\n";
}
return $message;
}
public function create_header( )
{
global $use_sendmail;
$this->Start( );
$header = array( );
$header[] = sprintf( "Received: from client %s for UebiMiau2.7 (webmail client); %s %s\r\n", $this->IPAddress, date( "D, j M Y G:i:s" ), $this->timezone );
$header[] = sprintf( "Date: %s %s\r\n", date( "D, j M Y G:i:s" ), $this->timezone );
$header[] = sprintf( "From: \"%s\" <%s>\r\n", $this->FromName, trim( $this->From ) );
if ( 0 < count( $this->to ) )
{
$header[] = $this->addr_append( "To", $this->to );
}
if ( 0 < count( $this->cc ) )
{
$header[] = $this->addr_append( "Cc", $this->cc );
}
if ( 0 < count( $this->bcc ) && $use_sendmail )
{
$header[] = $this->addr_append( "Bcc", $this->bcc );
}
if ( 0 < count( $this->ReplyTo ) )
{
$header[] = $this->addr_append( "Reply-to", $this->ReplyTo );
}
$header[] = sprintf( "Subject: %s\r\n", trim( $this->Subject ) );
$header[] = sprintf( "X-Priority: %d\r\n", $this->Priority );
$header[] = sprintf( "X-Mailer: %s\r\n", $this->Version );
$header[] = sprintf( "X-Original-IP: %s\r\n", $this->IPAddress );
$header[] = sprintf( "Content-Transfer-Encoding: %s\r\n", $this->Encoding );
$header[] = sprintf( "Return-Path: %s\r\n", trim( $this->From ) );
$index = 0;
for ( ; $index < count( $this->CustomHeader ); ++$index )
{
$header[] = sprintf( "%s\r\n", $this->CustomHeader[$index] );
}
if ( $this->UseMSMailHeaders )
{
$header[] = $this->UseMSMailHeaders( );
}
if ( 0 < count( $this->attachment ) )
{
$header[] = sprintf( "Content-Type: multipart/mixed; charset=\"%s\";\r\n", $this->CharSet );
$header[] = sprintf( "\tboundary=\"--=%s\"\r\n", $this->boundary );
}
else
{
$header[] = sprintf( "Content-Type: %s; charset=\"%s\";\r\n", $this->ContentType, $this->CharSet );
}
$header[] = "MIME-Version: 1.0\r\n";
return join( "", $header )."\r\n";
}
public function create_body( )
{
if ( $this->WordWrap )
{
$this->Body = $this->wordwrap( $this->Body, $this->WordWrap );
}
if ( 0 < count( $this->attachment ) )
{
if ( !( $body = $this->attach_all( ) ) )
{
return FALSE;
}
}
else
{
$body = $this->Body;
}
return trim( $body );
}
public function AddAttachment( $path, $name = "", $type = "application/octet-stream" )
{
if ( !@is_file( $path ) )
{
$this->error_handler( sprintf( "Could not find %s file on filesystem", $path ) );
return FALSE;
}
$filename = basename( $path );
if ( $name == "" )
{
$name = $filename;
}
$this->boundary = "_b".md5( uniqid( time( ) ) );
$cur = count( $this->attachment );
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $type;
return TRUE;
}
public function attach_all( )
{
$mime = sprintf( "----=%s\r\n", $this->boundary );
$mime .= sprintf( "Content-Type: %s\r\n", $this->ContentType );
$mime .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$mime .= sprintf( "%s\r\n", $this->Body );
$i = 0;
for ( ; $i < count( $this->attachment ); ++$i )
{
$path = $this->attachment[$i][0];
$filename = $this->attachment[$i][1];
$name = $this->attachment[$i][2];
$type = $this->attachment[$i][3];
$mime .= sprintf( "----=%s\r\n", $this->boundary );
$mime .= sprintf( "Content-Type: %s; name=\"%s\"\r\n", $type, $name );
$mime .= "Content-Transfer-Encoding: base64\r\n";
$mime .= sprintf( "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n", $name );
if ( $mime .= sprintf( "%s\r\n\r\n", $this->encode_file( $path ) ) )
{
continue;
}
return FALSE;
}
$mime .= sprintf( "\r\n----=%s--\r\n", $this->boundary );
return $mime;
}
public function encode_file( $path )
{
if ( !( $fd = @fopen( $path, "rb" ) ) )
{
$this->error_handler( "File Error: Could not open file ".$path );
return FALSE;
}
$file = fread( $fd, filesize( $path ) );
$encoded = chunk_split( base64_encode( $file ) );
fclose( $fd );
return $encoded;
}
public function ClearAddresses( )
{
$this->to = array( );
}
public function ClearCCs( )
{
$this->cc = array( );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -