Php a simple smtp class

Source: Internet
Author: User

Smtp. class. php
Copy codeThe Code is as follows:
<? Php

Define ('smtp _ STATUS_NOT_CONNECTED ', 1, TRUE );
Define ('smtp _ status_connected', 2, TRUE );

Class smtp
{

Var $ connection;
Var $ recipients;
Var $ headers;
Var $ timeout;
Var $ errors;
Var $ status;
Var $ body;
Var $ from;
Var $ host;
Var $ port;
Var $ helo;
Var $ auth;
Var $ user;
Var $ pass;
Var $ debug;

/**
* The parameter is an array.
* Host SMTP Server default host: localhost
* Port SMTP server port default: 25
* The default name of the helo command sent by HELO is localhost.
* User SMTP server user name default: NULL
* Default logon password for the pass SMTP server: NULL
* Default timeout time for timeout connections: 5
* @ Return bool
*/

Function smtp ($ params = array ())
{

If (! Defined ('crlf ') define ('crlf', "\ r \ n", TRUE );

$ This-> timeout = 5;
$ This-> status = SMTP_STATUS_NOT_CONNECTED;
$ This-> host = 'localhost ';
$ This-> port = 25;
$ This-> auth = FALSE;
$ This-> user = ";
$ This-> pass = ";
$ This-> errors = array ();
$ This-> debug = false;
Foreach ($ params as $ key => $ value)
{
$ This-> $ key = $ value;
}

$ This-> helo = $ this-> host;

// Do not verify if no user name is set
$ This-> auth = ("= $ this-> user )? FALSE: TRUE;
}

Function connect ($ params = array ())
{

If (! Isset ($ this-> status ))
{
$ Obj = new smtp ($ params );

If ($ obj-> connect ())
{
$ Obj-> status = SMTP_STATUS_CONNECTED;
}

Return $ obj;

}
Else
{

$ This-> connection = fsockopen ($ this-> host, $ this-> port, $ errno, $ errstr, $ this-> timeout );
Socket_set_timeout ($ this-> connections, 0, 250000 );

$ Greeting = $ this-> get_data ();

If (is_resource ($ this-> connection ))
{
$ This-> status = 2;
Return $ this-> auth? $ This-> ehlo (): $ this-> helo ();
}
Else
{
$ This-> errors [] = 'failed' to connect to server: '. $ errstr;
Return FALSE;
}
}
}

/**
* The parameter is an array.
* Array of recipients
* The address of the from sender will also be used as the reply address.
* Array of headers Information
* Body
*/

Function send ($ params = array ())
{

Foreach ($ params as $ key => $ value)
{
$ This-> set ($ key, $ value );
}

If ($ this-> is_connected ())
{
// Whether the server needs verification
If ($ this-> auth)
{
If (! $ This-> auth () return FALSE;
}

$ This-> mail ($ this-> from );

If (is_array ($ this-> recipients ))
{
Foreach ($ this-> recipients as $ value)
{
$ This-> rcpt ($ value );
}
}
Else
{
$ This-> rcpt ($ this-> recipients );
}

If (! $ This-> data () return FALSE;

$ Headers = str_replace (CRLF. '.', CRLF. '...', trim (implode (CRLF, $ this-> headers )));
$ Body = str_replace (CRLF. '.', CRLF. '...', $ this-> body );
$ Body = $ body [0] = '.'? '.'. $ Body: $ body;

$ This-> send_data ($ headers );
$ This-> send_data (");
$ This-> send_data ($ body );
$ This-> send_data ('.');

Return (substr (trim ($ this-> get_data (), 0, 3) === '000000 ′);
}
Else
{
$ This-> errors [] = 'not ccted! ';
Return FALSE;
}
}

Function helo ()
{
If (is_resource ($ this-> connection)
AND $ this-> send_data ('HELO'. $ this-> HELO)
AND substr (trim ($ error = $ this-> get_data (), 0, 3) = '000000 ′)
{
Return TRUE;

}
Else
{
$ This-> errors [] = 'helo command failed, output: '. trim (substr (trim ($ error), 3 ));
Return FALSE;
}
}

Function ehlo ()
{
If (is_resource ($ this-> connection)
AND $ this-> send_data ('EHLO'. $ this-> helo)
AND substr (trim ($ error = $ this-> get_data (), 0, 3) = '000000 ′)
{
Return TRUE;
}
Else
{
$ This-> errors [] = 'ehlo command failed, output: '. trim (substr (trim ($ error), 3 ));
Return FALSE;
}
}

Function auth ()
{
If (is_resource ($ this-> connection)
AND $ this-> send_data ('auth login ')
AND substr (trim ($ error = $ this-> get_data (), 0, 3) = '000000 ′
AND $ this-> send_data (base64_encode ($ this-> user) // Send username
AND substr (trim ($ error = $ this-> get_data (), 334) = '000000 ′
AND $ this-> send_data (base64_encode ($ this-> pass) // Send password
AND substr (trim ($ error = $ this-> get_data (), 235) = '000000 ′)
{
Return TRUE;
}
Else
{
$ This-> errors [] = 'auth command failed: '. trim (substr (trim ($ error), 3 ));
Return FALSE;
}
}

Function mail ($ from)
{

If ($ this-> is_connected ()
AND $ this-> send_data ('mail FROM: <'. $ from.'> ')
AND substr (trim ($ this-> get_data (), 0, 2) === '000000 ′)
{
Return TRUE;
}
Else
{
Return FALSE;
}
}

Function rcpt ($)
{
If ($ this-> is_connected ()
AND $ this-> send_data ('RCPT TO: <'. $ to.'> ')
AND substr (trim ($ error = $ this-> get_data (), 0, 2) = '25 ′)
{
Return TRUE;
}
Else
{
$ This-> errors [] = trim (substr (trim ($ error), 3 ));
Return FALSE;
}
}

Function data ()
{

If ($ this-> is_connected ()
AND $ this-> send_data ('data ')
AND substr (trim ($ error = $ this-> get_data (), 0, 3) = '000000 ′)
{
Return TRUE;
}
Else
{
$ This-> errors [] = trim (substr (trim ($ error), 3 ));
Return FALSE;
}
}

Function is_connected ()
{
Return (is_resource ($ this-> connection) AND ($ this-> status === SMTP_STATUS_CONNECTED ));
}

Function send_data ($ data)
{
If (is_resource ($ this-> connection ))
{
If ($ this-> debug)
Echo nl2br ($ data. CRLF );
Return fwrite ($ this-> connection, $ data. CRLF, strlen ($ data) + 2 );
}
Else
{
Return FALSE;
}
}

Function & get_data ()
{

$ Return = ";
$ Line = ";

If (is_resource ($ this-> connection ))
{
While (strpos ($ return, CRLF) === false or substr ($ line, 3,1 )! = '')
{
$ Line = fgets ($ this-> connection, 512 );
$ Return. = $ line;
}
If ($ this-> debug === true)
Echo nl2br ($ return. CRLF );
Return $ return;

}
Else
{
Return FALSE;
}
}

Function set ($ var, $ value)
{
$ This-> $ var = $ value;
Return TRUE;
}
} // End of 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.