SMTP mail send a case _php Foundation

Source: Internet
Author: User
Tags strcmp strtok
test_smtp.php

?
Require ("smtp.php");

$SMTP =new Smtp_class;

$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);

while (($line = $this->getline ($this->connection))
{
if (IsSet ($match _code))
{
if (strcmp (Strtok ($line, "-"), $match _code))
{
$this->error= $line;
return (0);
}
}
Else
{
$match _code=strtok ($line, "-");
if (GetType ($code) = = "Array")
{
For ($codes =0 $codes <count ($code) && strcmp ($match _code, $code [$codes]); $codes + +);
if ($codes >=count ($code))
{
$this->error= $line;
return (0);
}
}
Else
{
if (strcmp ($match _code, $code))
{
$this->error= $line;
return (0);
}
}
}
$responses []=strtok ("");
if (!strcmp ($match _code,strtok ($line, ""))
return (1);
}
Return (-1);
}

Function flushrecipients ()
{
if ($this->pending_sender)
{
if ($this->verifyresultlines ("<=0")
return (0);
$this->pending_sender=0;
}
for (; $this->pending_recipients; $this->pending_recipients--)
{
if ($this->verifyresultlines (Array ("251")) <=0)
return (0);
}
return (1);
}

/* Public Methods * *

Function Connect ()
{
$this->error= $error = "";
$this->esmtp_host= "";
$this->esmtp_extensions=array ();
if (!) ( $this->connection= ($this->timeout fsockopen ($this->host_name, $this->host_port,& $errno, &$ Error, $this->timeout): Fsockopen ($this->host_name, $this->host_port)))
{
Switch ($error)
{
Case-3:
$this->error= "-3 socket could not being created";
return (0);
Case-4:
$this->error= "-4 DNS lookup on hostname \" ". $host _name." \ "Failed";
return (0);
Case-5:
$this->error= "-5 connection refused or timed out";
return (0);
Case-6:
$this->error= "-6 Fdopen () call failed";
return (0);
Case-7:
$this->error= "-7 setvbuf () call failed";
return (0);
Default
$this->error= $error. "Could not connect to the host \" ". $this->host_name." \"";
return (0);
}
}
Else
{
if (!strcmp ($localhost = $this->localhost, "")
&&!strcmp ($localhost =getenv ("SERVER_NAME"), "")
&&!strcmp ($localhost =getenv ("HOST"), "")
$localhost = "localhost";
$success = 0;
if ($this->verifyresultlines (">0")
{
if ($this->esmtp)
{
$responses =array ();
if ($this->putline ("EHLO $localhost")
&& $this ("->verifyresultlines",& $responses) >0)
{
$this->esmtp_host=strtok ($responses [0], "");
for ($response =1; $response <count ($responses); $response + +)
{
$extension =strtoupper (Strtok ($responses [$response], ""));
$this->esmtp_extensions[$extension]=strtok ("");
}
$success = 1;
}
}
if (! $success
&& $this->putline ("HELO $localhost")
&& $this->verifyresultlines (">0")
$success = 1;
}
if ($success)
{
$this->state= "Connected";
return (1);
}
Else
{
Fclose ($this->connection);
$this->connection=0;
$this->state= "Disconnected";
return (0);
}
}
}

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);
}

};

?>


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.