Php Send mail method 21. (SMTP transport SMTP Class) _php tutorial

Source: Internet
Author: User
Tags php define php send mail
[HTML]
Define (' smtp_status_not_connected ', 1, TRUE);
Define (' smtp_status_connected ', 2, TRUE);

Class P8_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;

/**
* parameter is an array
* Host SMTP server default: localhost
* Port SMTP Server default: 25
* helo send HELO command name default: localhost
* Username default: null for user SMTP server
* Pass SMTP Server login password default: null value
* Timeout Connection time-out default: 5
* @return BOOL
*/

function p8_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 ();
foreach ($params as $key = $value)
{
$this $key = $value;
}

$this->helo = $this->host;
Does not validate if no user name is set
$this->auth = (' = = $this->user)? False:true;
}
Function Connect ($params = Array ())
{
if (!isset ($this->status))
{
$obj = new P8_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 server: '. $errstr;
return FALSE;
}
}
}

/**
* Parameter array
* Recipients Receiver's array
* From the address of the sender, also as a reply to the address
* Headers array of header information
* Body of Body message
*/

function Send ($params = Array ())
{
foreach ($params as $key = $value)
{
$this->set ($key, $value);
}
if ($this->is_connected ())
{
Whether the server requires validation
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))
{
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;
}
return $return;
}
Else
{
return FALSE;
}
}
function set ($var, $value)
{
$this $var = $value;
return TRUE;
}
}//End of Class



Class SMTP
{
var $debug;
var $host;
var $port;
var $auth;
var $user;
var $pass;

function smtp ($host = "", $port = $auth = False, $user, $pass) {
$this->host= $host;
$this->port= $port;
$this->auth= $auth;
$this->user= $user;
$this->pass= $pass;
}

function SendMail ($to, $from, $subject, $content, $T =0) {
Global $webdb;
$name, $email, $subject, $content, $type =0
$type = 1;
$name =array ("{$webdb [WebName]} member");
$email =array ($to);
$_cfg[' smtp_host ']= $this->host;
$_cfg[' smtp_port ']= $this->port;
$_cfg[' Smtp_user ']= $this->user;
$_cfg[' Smtp_pass ']= $this->pass;
$_cfg[' name ']= $webdb [webname];
$_cfg[' Smtp_mail ']= $from;

$name = "=? UTF-8? B? ". Base64_encode ($name). " ==?=";
$subject = "=?GBK?" B? ". Base64_encode ($subject). " ==?=";
$content = Base64_encode ($content);
$headers [] = "TO:=?GBK?" B? ". Base64_encode ($name [0]). "? = < $email [0]> ";
$headers [] = "FROM:=?GBK?" B? ". Base64_encode ($_cfg[name]). "? = <$_CFG[smtp_mail]> ";
$headers [] = "Mime-version:blueidea v1.0";
$headers [] = "X-mailer:9gongyu Mailer v1.0";
$headers [] = "from:=?" UTF-8? B? ". Base64_encode ($_cfg[' shop_name '). " ==?=<$_cfg[smtp_mail]> ";
$headers [] = "Subject: $subject";
$headers [] = ($type = = 0)? "Content-type:text/plain; CHARSET=GBK; Format=flowed ":" content-type:text/html; CHARSET=GBK; Format=flowed ";
$headers [] = "content-transfer-encoding:base64";
$headers [] = "Content-disposition:inline";
SMTP Server information
$params [' host '] = $_cfg[' smtp_host '];
$params [' port '] = $_cfg[' smtp_port '];
$params [' user '] = $_cfg[' Smtp_user '];
$params [' pass '] = $_cfg[' Smtp_pass ');
if (Empty ($params [' Host ']) | | empty ($params [' Port ']))
{
If the host and port are not set directly return False
return false;
}
Else
{
Send mail
$send _params[' recipients ') = $email;
$send _params[' headers ') = $headers;
$send _params[' from '] = $_cfg[' smtp_mail '];
$send _params[' body '] = $content;
/*
echo "



echo "
";
Exit
*/
$SMTP = new P8_smtp ($params);
if ($smtp->connect () and $smtp->send ($send _params))
{
return TRUE;
}
Else
{
return FALSE;
}//End If
}
}
}

?>
Author: vericlongmore

http://www.bkjia.com/PHPjc/478151.html www.bkjia.com true http://www.bkjia.com/PHPjc/478151.html techarticle [HTML] PHP define (smtp_status_not_connected, 1, true); define (smtp_status_connected, 2, true); class P8_smtp {var $c onnection; var $recipients; var $headers; var $timeout; var $er ...

  • 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.