Moderators see the use of the best php-written SMTP class, supporting attachments (multiple), supporting HTML. Come on, use it! (Author: Boss

Source: Internet
Author: User
Tags format array arrays empty fread split strlen version
SMTP.?
/***********************************
PHP MIME SMTP ver 1.0 powered by Boss_ch, Unigenius Soft ware Co. Ltd
All rights reserved, Copyright 2000;
This class sends MIME-type messages using PHP through the SMTP sock operation, and can send
HTML format body, accessories, using base64 code
This version is for the individual to send, and the number of mass version is different, each sent to a person, on the code again, in the receiver's view, just send him a person.
For many people, send only once, through multiple RCPT to command sent to a different people's mailbox,
Description
Please set $hostname to the default SMTP server you have permission for or to specify when new
Change the $charset to your default character set
If there is a picture in the Html body, please use the absolute path reference "Image.gif";
and connected to the Internet to ensure that the program can read the image of the data information
If the Html body is submitted through the form, first use Stripslashes ($html _body) to preprocess the body content.
Style sheet files used in Html, do not use references such as <link > to directly place the stylesheet definition in the
<style></style> tags in

Reprint please retain this copyright information, Bugs report:boss_ch@china.com
*************************************/
if (!isset ($__smtp_class__)) {
$__smtp_class__=1;

Class SMTP
{
var $hostname = "";
var $port = 25;
var $connection = 0;
var $debug = 1;

var $timeout = 30;
var $err _str;
var $err _no;

var $autocode =true;
var $charset = "GB2312";
var $subject = "";
var $body = "";
var $attach = "";
var $temp _text_body;
var $temp _html_body;
var $temp _body_images;

var $bound _begin= "=====powered_by_boss_chen_";
var $bound _end= "_046484063883_=====";

Function smtp ($server = "smtp.china.com" ", $port =25, $time _out=20)
{$this->hostname= $server;
$this->port= $port;
$this->timeout= $time _out;
return true;
}

Function Outdebug ($message)
{
echo Htmlspecialchars ($message). " <br>\n ";
}


function command ($command, $return _lenth=1, $return _code= ' 2 ')
{
if ($this->connection==0)
{
$this->err_str= "Not connected to any server, check your network connections";
return false;
}
if ($this->debug)
$this->outdebug (">>> $command");
if (!fputs ($this->connection, "$command \ r \ n"))
{
$this->err_str= "Cannot send command". $command;
return false;
}
Else
{
$resp =fgets ($this->connection,256);
if ($this->debug)
$this->outdebug ("$resp");
if (substr ($resp, 0, $return _lenth)!= $return _code)
{
$this->err_str= $command. "Command server returned invalid:". $resp;
return false;
}
Else
return true;
}
}


Function Open ()
{
if ($this->hostname== "")
{$this->err_str= "Invalid host name!!";
return false;
}

if ($this->debug) echo $this->hostname, $this->port,& $err _no, & $err _str, $this->timeout<br > ";
if (! $this->connection=fsockopen ($this->hostname, $this->port,& $err _no, & $err _str, $this-> Timeout))
{
$this->err_str= "Connection to SMTP Server failed, error message:". $err _str. " Error number: ". $err _no;
return false;
}
Else
{
$resp =fgets ($this->connection,256);
if ($this->debug)
$this->outdebug ("$resp");
if (substr ($RESP, 0, 1)!= "2")
{$this->err_str= server returned invalid information: ". $resp." Please check that the SMTP server is correct.
return false;
}
return true;
}
}


Function Close ()
{
if ($this->connection!=0)
{
Fclose ($this->connection);
$this->connection=0;
}
}

Function Buildhead ($from _name, $to _name, $from _mail, $to _mail, $subject)
{
if (Empty ($from _name))
$from _name= $from _mail;
if (Empty ($to _name)) $to _name= $to _mail;
$this->subject= "From: = $this->charset?" B? ". Base64_encode ($from _name). "? =< $from _mail>\r\n ";
$this->subject.= "To: = $this->charset? B? ". Base64_encode ($to _name). "? =< $to _mail>\r\n ";
$subject =ereg_replace ("\ n", "", $subject);
$this->subject.= "Subject: = $this->charset? B? ". Base64_encode ($subject). "? =\r\n ";
if ($this->debug) echo nl2br (Htmlspecialchars ($this->subject));
return true;
}


Function parse_html_body ($html _body=null)
{
$passed = "";
$image _count=0;
$this->temp_body_images=array ();
while (Eregi ("\<*img" ([^\>]+) src[[:space:]]*=[[:space:]]* ([^]+), $html _body, $reg))
{

$pos = @strpos ($html _body, $reg [0]);
$passed. =substr ($html _body,0, $pos);
$html _body=substr ($html _body, $pos +strlen ($reg [0]));
$image _tag= $reg [2];
$image _att= $reg [1];
$tag _len=strlen ($image _tag);
if ($image _tag[0]== "" "or $image _tag[0]== '")
$image _tag=substr ($image _tag,1);
if (substr ($image _tag,strlen ($imgage _tag) -1,1 = = "" "or substr ($image _tag,strlen ($imgage _tag) -1,1) = = '"]
$image _tag=substr ($image _tag,0,strlen ($imgage _tag)-1);
echo $image _tag. " <br> ";
$cid =md5 (rand ()) (uniqid);
$cid =substr ($cid, 0,15). " @unigenius. com ";
$passed. = "$end _pos= @strpos ($html _body, ' > ');
$passed. =substr ($html _body,0, $end _pos);
$html _body=substr ($html _body, $end _pos);
Read the picture data and save it to a data;

$img _file_con=fopen ($image _tag, "R");
unset ($image _data);
while ($tem _buffer=addslashes (fread ($img _file_con,16777216))
$image _data.= $tem _buffer;
Fclose ($img _file_con);
$image _exe_name=substr ($image _tag,strrpos ($image _tag, '. ') +1,3);
Switch (Strtolower ($image _exe_name))
{
Case "JPG":
Case "JPEG":
$content _type= "Image/jpeg";
Break
Case "GIF":
$content _type= "Image/gif";
Break
Case "PNG":
$content _type= "Image/x-png";
Break
Case "TIF":
$content _type= "Image/tif";
Break
Default
$content _type= "image/";
Break
}

$this->temp_body_images[$image _count][name]=basename ($image _tag);
$this->temp_body_images[$image _count][type]= $content _type;
$this->temp_body_images[$image _count][cid]= $cid;
$this->temp_body_images[$image _count][data]= $image _data;
$image _count++;
}
$this->temp_html_body= $passed. $html _body;
return true;

}

function Build_content ($bound _level=0, $text _body, $html _body, $hava _att=false)
{
if ($html _body)
{
if (eregi ("\<*img[[:space:]]+src[[:space:]]*=[[:space:]]*" ([^]+), $html _body, $reg))
{
$bound _level++;
if ($text _body)
{
$this->body.= "content-type:multipart/related;
Type=\ "Multipart/alternative\";
Boundary=\ "";
$this->body.= $this->bound_begin. $bound _level. $this->bound_end. " \ "\r\n\r\n";
}
Else
{
$this->body.= "content-type:multipart/related;
Boundary=\ "";
$this->body.= $this->bound_begin. $bound _level. $this->bound_end. " \ "\r\n\r\n";

//For whether text body, HTML body has no, must have different MIME headers
if (! $hava _att) $this->body.= "This is a multi-part message in MIME format.\r\n\r\n";
Body identification, if the attachment is already encoded, then in the body does not need this sentence
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->parse_html_body ($html _body);
if ($text _body)
{
$this->body.= "content-type:multipart/alternative;
Boundary=\ "";
$bound _level++;
$this->body.= $this->bound_begin. $bound _level. $this->bound_end. " \ "\r\n\r\n";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "content-type:text/plain;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($text _body)). " \ r \ n ";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "content-type:text/html;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($this->temp_html_body)). " \ r \ n ";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " --\r\n\r\n ";
$bound _level--;
}
Else
{
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "content-type:text/html;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($this->temp_html_body)). " \ r \ n ";
//Body code, with or without text, in a different format.
for ($i =0; $i <count ($this->temp_body_images); $i + +)
{
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "Content-type:" $this->temp_body_images[$i][type]. "
Name=\ "";
$this->body.= $this->temp_body_images[$i][name]. " \ "\ r \ n";
$this->body.= "content-transfer-encoding:base64\r\n";
$this->body.= "Content-id: <". $this->temp_body_images[$i][cid]. " >\r\n ";
$this->body.= "\ r \ n". Chunk_split (Base64_encode (stripslashes ($this->temp_body_images[$i][data))). " \ r \ n ";
}
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " --\r\n\r\n ";
$bound _level--;
}
else//There is or no picture, above is the processing of the picture, the following is no picture of the processing
{
$this->temp_html_body= $html _body;
if ($text _body)
{
$bound _level++;
$this->body.= "content-type:multipart/alternative;
Boundary=\ "";
$this->body.= $this->bound_begin. $bound _level. $this->bound_end. " \ "\r\n\r\n";

if (! $hava _att) $this->body.= "\r\nthis is a multi-part message in MIME format.\r\n\r\n";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "content-type:text/plain;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($text _body)). " \ r \ n ";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "content-type:text/html;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($this->temp_html_body)). " \ r \ n ";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " --\r\n\r\n ";
$bound _level--;
}
Else
{
$this->body.= "content-type:text/html;\r\n";
$this->body.= "charset=\" $this->charset\ "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($this->temp_html_body)). " \ r \ n ";
//Body code, with or without text, in a different format.

}//End Else
}
else//If there is no HTML body, only the text body
{
$this->body.= "Content-type:text/plain;
Charset=\ "$this->charset\" "\ r \ n";
$this->body.= "content-transfer-encoding:base64 \ r \ n";
$this->body.= "\ r \ n" Chunk_split (Base64_encode stripslashes ($text _body)). " \ r \ n ";
}
}//End Function default


Function buildbody ($text _body=null, $html _body=null, $att =null)
{
$this->body= "mime-version:1.0\r\n";
if (null== $att or (@count ($att) ==0)//If no attachment is available, view the type of the body;
{
$encode _level=0;
$this->build_content ($encode _level, $text _body, $html _body);
//If there are no attachments,
// ********************************************************
else//If there is an attachment,
{
$bound _level=0;
$this->body.= "content-type:multipart/mixed;
Boundary=\ "";
$bound _level++;

$this->body.= $this->bound_begin. $bound _level. $this->bound_end. " \ "\r\n\r\n";
$this->body.= "This are a multi-part message in MIME format.\r\n\r\n";
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->build_content ($bound _level, $text _body, $html _body,true); Into the body part

$num =count ($att);
for ($i =0; $i < $num; $i + +)
{
$file _name= $att [$i][name];
$file _source= $att [$i][source];
$file _type= $att [$i][type];
$file _size= $att [$i][size];

if (file_exists ($file _source))
{
$file _data=addslashes (Fread ($fp =fopen ($file _source, "R"), FileSize ($file _source));
$file _data=chunk_split (Base64_encode stripslashes ($file _data));
$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " \ r \ n ";
$this->body.= "Content-type: $file _type;\r\n name=\" $file _name\ "\r\ncontent-transfer-encoding:base64\r\n";
$this->body.= "content-disposition:attachment; Filename=\ "$file _name\" \r\n\r\n ";
$this->body.= $file _data. " \ r \ n ";
}
}//end for

$this->body.= "--" $this->bound_begin. $bound _level $this->bound_end. " --\r\n\r\n ";
}//End Else

if ($this->debug) echo nl2br (Htmlspecialchars ($this->body));

return true;
}


function Send ($from _name, $to _name, $from _mail, $to _mail, $subject, $text _body=false, $html _body=false, $att =false)
{

if (Empty ($from _mail) or empty ($to _mail))
{
$this->err_str= "did not specify the correct mail address:". $from _mail. " Receiver: ". $to _mail;
return false;
}

if (GetType ($to _mail)!= "Array")
$to _mail=split (",", $to _mail); If it is not an array, convert the arrays, even if there is only one sending object;
if (GetType ($to _name)!= "Array")
$to _name=split (",", $to _name); If it is not an array, convert the arrays, even if there is only one sending object;

$this->buildbody ($text _body, $html _body, $att);
The contents of all letters are the same, can only be edited once, and for different addressee, need different head


if (! $this->open ()) return false;
if (! $this->command ("HELO $this->hostname", 3, ")" return false;
Establish a link with the server
if (! $this->open ()) return false;
if (! $this->command ("HELO $this->hostname", 3, ")" return false;

for ($i =0; $i <count ($to _mail); $i + +)
{
$this->buildhead ($from _name, $to _name[$i], $from _mail, $to _mail[$i], $subject);
if (! $this->command ("RSET", 3, ")" return false;
if (! $this->command ("MAIL from:". $from _mail,3, "a") return false;
if (! $this->command ("RCPT to:". $to _mail[$i],3, "a") return false;
if (! $this->command ("DATA", 3, "354") return false;
Preparing to send mail
if ($this->debug) $this->outdebug ("sending subject;");
if (!fputs ($this->connection, $this->subject)) {$this->err_str= "error sending headers!" "; return false;}
if ($this->debug) $this->outdebug ("Sending body;");
if (!fputs ($this->connection, $this->body)) {$this->err_str= "error sending body!" "; return false;}
if (!fputs ($this->connection, ". \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \")) {$this->err_str= "error sending body "; return false;} Text sent out, exit;
$resp =fgets ($this->connection,256);
if ($this->debug)
$this->outdebug ("$resp");
if (substr ($RESP, 0, 1)!= "2")
{
$this->err_str= "After sending, the server does not respond!! ";
return false;
}
Send mail
}
if (! $this->command ("QUIT", 3, "221") return false;
$this->close ();
return true;
}

}//end class Define
}//end if (!isset ($__smtp_class__))
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.