$ Smtp-> host_name = "mail.xiaocui.com ";
$ Smtp-> localhost = "localhost ";
$ From = "webmaster@xiaocui.com ";
$ To = "root@xiaocui.com ";
If ($ smtp-> SendMessage (
$ From,
Array (
$
),
Array (
"From: $ from ",
"To: $ ",
"Subject: Testing Manuel Lemos 'smtp class"
),
"Hello $ to, \ n \ nIt is just to let you know that your SMTP class is working just fine. \ n \ nBye. \ n "))
Echo "Message sent to $ to OK. \ n ";
Else
Echo "Cound not send the message to $ 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 ,"
\ N ";
}
Function GetLine ()
{
For ($ line = "";;)
{
If (feof ($ this-> connection ))
{
$ This-> error = "reached the end of stream while reading from socket ";
Return (0 );
}
If ($ data = fgets ($ this-> connection, 100) = false)
{
$ This-> error = "it was 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 was 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 was not possible to write data to socket ";
Return (0 );
}
}
Return (1 );
}
Function MailFrom ($ sender)
{
If (strcmp ($ this-> state, "Connected "))
{
$ This-> error = "connection is not in the initial state ";
Return (0 );
}
$ This-> error = "";
If (! $ This-> PutLine ("mail from: <". $ sender. "> "))
Return (0 );
If (! IsSet ($ this-> esmtp_extensions ["PIPELINING"])
& $ This-> VerifyResultLines ("250") <= 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 not 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 ("250", "251") <= 0)
Return (0 );
}
$ This-> state = "RecipientSet ";
Return (1 );
}
Function StartData ()
{
If (strcmp ($ this-> state, "RecipientSet "))
{
$ This-> error = "connection is not in the 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 not in the sending data state ";
Return (0 );
}
$ This-> error = "";
Return ($ this-> PutData (& $ data ));
}
Function EndSendingData ()
{
If (strcmp ($ this-> state, "SendingData "))
{
$ This-> error = "connection is not in the sending data state ";
Return (0 );
}
$ This-> error = "";
If (! $ This-> PutLine ("\ r \ n .")
| $ This-& gt; VerifyResultLines ("250") <= 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-& gt; VerifyResultLines ("250") <= 0)
Return (0 );
$ This-> state = "Connected ";
Return (1 );
}
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.