$SMTP->host_name= "mail.xiaocui.com";
$smtp->localhost= "localhost";
$from = "webmaster@xiaocui.com";
$to = "root@xiaocui.com";
if ($smtp->sendmessage (
$from,
Array
$to
),
Array
"From: $from",
"To: $to",
"Subject:testing Manuel Lemos ' SMTP class"
),
"Hello $to, \n\nit is just to we know that your SMTP class is working just fine.\n\nbye.\n"))
echo "message sent to $to ok.\n";
Else
echo "Cound not send" $to. \nerror: ". $smtp->error." \ n "
?>
smtp.php
?
Class Smtp_class
{
var $host _name= "";
var $host _port=25;
var $localhost = "";
var $timeout = 0;
var $error = "";
var $debug = 1;
var $esmtp = 1;
var $esmtp _host= "";
var $esmtp _extensions=array ();
var $maximum _piped_recipients=100;
/* Private variables-do not ACCESS * *
var $state = "Disconnected";
var $connection = 0;
var $pending _recipients=0;
/* Private methods-do not call * *
Function Outputdebug ($message)
{
Echo $message, "<br>\n";
}
Function Getline ()
{
for ($line = "";;)
{
if (feof ($this->connection))
{
$this->error= "reached the end of the stream while reading from socket";
return (0);
}
if (($data =fgets ($this->connection,100)) ==false)
{
$this->error= "It is not possible to read line from socket";
return (0);
}
$line. = $data;
$length =strlen ($line);
if ($length >=2
&& substr ($line, $length -2,2) = = "\ r \ n")
{
$line =substr ($line, 0, $length-2);
if ($this->debug)
$this->outputdebug ("< $line");
return ($line);
}
}
}
Function Putline ($line)
{
if ($this->debug)
$this->outputdebug ("> $line");
if (!fputs ($this->connection, "$line \ r \ n"))
{
$this->error= "It is not possible to write line to socket";
return (0);
}
return (1);
}
Function PutData ($data)
{
if (strlen ($data))
{
if ($this->debug)
$this->outputdebug ("> $data");
if (!fputs ($this->connection, $data))
{
$this->error= "It is not possible to write data to socket";
return (0);
}
}
return (1);
}
Function verifyresultlines ($code, $responses = "")
{
if (GetType ($responses)!= "Array")
$responses =array ();
Unset ($match _code);
Function Mailfrom ($sender)
{
if (strcmp ($this->state, "Connected"))
{
$this->error= "Connection is isn't in the initial state";
return (0);
}
$this->error= "";
if (! $this->putline ("MAIL from: <". $sender. " > "))
return (0);
if (! IsSet ($this->esmtp_extensions["pipelining"])
&& $this->verifyresultlines ("<=0")
return (0);
$this->state= "Senderset";
if (IsSet ($this->esmtp_extensions["pipelining"])
$this->pending_sender=1;
$this->pending_recipients=0;
return (1);
}
Function setrecipient ($recipient)
{
Switch ($this->state)
{
Case "Senderset":
Case "Recipientset":
Break
Default
$this->error= "Connection is isn't in the recipient setting state";
return (0);
}
$this->error= "";
if (! $this->putline ("RCPT to:<". $recipient. " > "))
return (0);
if (IsSet ($this->esmtp_extensions["pipelining"])
{
$this->pending_recipients++;
if ($this->pending_recipients>= $this->maximum_piped_recipients)
{
if (! $this->flushrecipients ())
return (0);
}
}
Else
{
if ($this->verifyresultlines (Array ("251")) <=0)
return (0);
}
$this->state= "Recipientset";
return (1);
}
Function Startdata ()
{
if (strcmp ($this->state, "Recipientset"))
{
$this->error= "Connection is isn't in" Start sending data state ";
return (0);
}
$this->error= "";
if (! $this->putline ("DATA")
return (0);
if ($this->pending_recipients)
{
if (! $this->flushrecipients ())
return (0);
}
if ($this->verifyresultlines ("354") <=0)
return (0);
$this->state= "Sendingdata";
return (1);
}
Function preparedata ($data,& $output)
{
$length =strlen (& $data);
for ($output = "", $position =0 $position < $length;)
{
$next _position= $length;
for ($current = $position; $current < $length; $current + +)
{
Switch ($data [$current])
{
Case "\ n":
$next _position= $current +1;
Break 2;
Case "\ r":
$next _position= $current +1;
if ($data [$next _position]== "\ n")
$next _position++;
Break 2;
}
}
if ($data [$position]== ".")
$output. = ".";
$output. =substr (& $data, $position, $current-$position). " \ r \ n ";
$position = $next _position;
}
}
Function SendData ($data)
{
if (strcmp ($this->state, "Sendingdata"))
{
$this->error= "Connection is isn't in the sending data state";
return (0);
}
$this->error= "";
Return ($this->putdata (& $data));
}
Function Endsendingdata ()
{
if (strcmp ($this->state, "Sendingdata"))
{
$this->error= "Connection is isn't in the sending data state";
return (0);
}
$this->error= "";
if (! $this->putline ("\ r \ n.")
|| $this->verifyresultlines ("<=0")
return (0);
$this->state= "Connected";
return (1);
}
Function resetconnection ()
{
Switch ($this->state)
{
Case "Connected":
return (1);
Case "Sendingdata":
$this->error= "Can not reset the connection while sending data";
return (0);
Case "Disconnected":
$this->error= "Can not reset the connection before it is established";
return (0);
}
$this->error= "";
if (! $this->putline ("RSET")
|| $this->verifyresultlines ("<=0")
return (0);
$this->state= "Connected";
return (1);
}
Function Disconnect ($quit =1)
{
if (!strcmp ($this->state, "Disconnected"))
{
$this->error= "It is not previously established a SMTP connection";
return (0);
}
$this->error= "";
if (!strcmp ($this->state, "Connected")
&& $quit
&& (! $this->putline ("QUIT")
|| $this->verifyresultlines ("221") <=0))
return (0);
Fclose ($this->connection);
$this->connection=0;
$this->state= "Disconnected";
return (1);
}
Function SendMessage ($sender, $recipients, $headers, $body)
{
if (($success = $this->connect ()))
{
if (($success = $this->mailfrom ($sender)))
{
for ($recipient =0; $recipient <count ($recipients); $recipient + +)
{
if (!) ( $success = $this->setrecipient ($recipients [$recipient]))
Break
}
if ($success
&& ($success = $this->startdata ()))
{
for ($header _data= "" ", $header =0; $header <count ($headers); $header + +)
$header _data.= $headers [$header]. " \ r \ n ";
if ($success = $this->senddata ($header _data.) \ r \ n ")))
{
$this->preparedata ($body,& $body _data);
$success = $this->senddata ($body _data);
}
if ($success)
$success = $this->endsendingdata ();
}
}
$disconnect _success= $this->disconnect ($success);
if ($success)
$success = $disconnect _success;
}
return ($success);
}
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.