📄 class.smtp.php
字号:
<?php
class phpmailer
{
var $Priority = 3;
var $CharSet = "iso-8859-1";
var $ContentType = "text/plain";
var $Encoding = "8bit";
var $From = "root@localhost";
var $FromName = "root";
var $Subject = "";
var $Body = "";
var $WordWrap = true;
var $MailerDebug = false;
var $UseMSMailHeaders = true;
var $IPAddress = "unknown";
var $timezone = "+0800";
var $Host = "localhost";
var $Port = 25;
var $Helo = "";
var $Timeout = 10;
var $version = "";
var $to = array( );
var $cc = array( );
var $bcc = array( );
var $ReplyTo = array( );
var $attachment = array( );
var $CustomHeader = array( );
var $boundary = false;
var $ErrorAlerts = array( );
var $blUseAuthLogin = false;
var $AuthUser = "";
var $AuthPass = "";
function useauthlogin( $user, $pass )
{
$this->blUseAuthLogin = true;
$this->AuthUser = $user;
$this->AuthPass = $pass;
}
function ishtml( $bool )
{
if ( $bool == true )
{
$this->ContentType = "text/html";
}
else
{
$this->ContentType = "text/plain";
}
}
function start( )
{
global $appname;
global $appversion;
$this->Version = $appname." ".$appversion;
$this->Helo = ereg_replace( "[^A-Za-z0-9]", "", $appname );
}
function addaddress( $address, $name = "" )
{
$cur = count( $this->to );
$this->to[$cur][0] = trim( $address );
$this->to[$cur][1] = $name;
}
function addcc( $address, $name = "" )
{
$cur = count( $this->cc );
$this->cc[$cur][0] = trim( $address );
$this->cc[$cur][1] = $name;
}
function addbcc( $address, $name = "" )
{
$cur = count( $this->bcc );
$this->bcc[$cur][0] = trim( $address );
$this->bcc[$cur][1] = $name;
}
function addreplyto( $address, $name = "" )
{
$cur = count( $this->ReplyTo );
$this->ReplyTo[$cur][0] = trim( $address );
$this->ReplyTo[$cur][1] = $name;
}
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 )
{
if ( $this->sendmail_send( $header, $body ) === false )
{
return false;
}
}
else
{
if ( $this->smtp_send( $header, $body ) === false )
{
return false;
}
}
return sprintf( "%s%s", $header, $body );
}
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;
}
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 == false )
{
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] ) ) )
{
$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] ) ) )
{
$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] ) ) )
{
$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( );
}
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";
}
else
{
$addr_str .= "\r\n";
}
return $addr_str;
}
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;
}
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";
}
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 );
}
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;
}
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 ) ) ) )
{
return false;
}
}
$mime .= sprintf( "\r\n----=%s--\r\n", $this->boundary );
return $mime;
}
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;
}
function clearaddresses( )
{
$this->to = array( );
}
function clearccs( )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -