PHP A simple SMTP class_php instance

Source: Internet
Author: User
Tags auth
smtp.class.php
Copy Code code 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;

/**
* parameter is an array
* Host SMTP server default: localhost
* Ports of Port SMTP server default: 25
* Helo the name of the HELO command is sent by default: localhost
* User SMTP server username default: null value
* Pass SMTP Server login password default: null value
* Timeout Connection Timeout time default: 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 validate 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->connection, 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 the to server: '. $errstr;
return FALSE;
}
}
}

/**
* parameter is an array
* Recipients array of recipients
* FROM Sender's address, also as reply address
* Headers array of header information
* Body of the message
*/

function Send ($params = Array ())
{

foreach ($params as $key => $value)
{
$this->set ($key, $value);
}

if ($this->is_connected ())
{
Whether the server requires authentication
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) = = ' 250′ ');
}
Else
{
$this->errors[] = ' not connected! ';
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) = = ' 250′ ')
{
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) = = ' 250′ ')
{
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) = = ' 334′
and $this->send_data (Base64_encode ($this->user))/Send username
and substr (Trim ($error = $this->get_data ()), 0, 3) = = ' 334′
and $this->send_data (Base64_encode ($this->pass))/Send password
and substr (Trim ($error = $this->get_data ()), 0, 3) = = ' 235′ ')
{
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) = = ' 250′ ')
{
return TRUE;
}
Else
{
return FALSE;
}
}

Function Rcpt ($to)
{
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) = = ' 354′ ')
{
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.